我想在recordBn
类中有一个MainMenu
按钮,它改变了dateLb
类中RecordTime
标签的文本。但我一直收到如下错误信息。
AttributeError: 'RecordTime' object has no attribute 'dateLb'
使用empcbxlist()
类的GFPTime
我试图从这个post和其他几个帖子解决我的问题同样的错误信息(AttributeError
),但找不到解决方案。帮我解决我的错误
#!/usr/bin/python
import os.path
import sys
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
# from PIL import Image, ImageTk
# from dbfunctions import *
import datetime
class GFPTime(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
tk.Tk.wm_title(self,"GFP Employee Timecard System")
tk.Tk.wm_geometry(self,"800x480+0+0")
container =tk.Frame(self)
container.pack(side='top',fill='both',expand= True)
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames = {}
for F in (MainMenu,RecordTime):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0,column=0,stick='nsew')
self.show_frame(MainMenu)
# print(self.frames.values())
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
def empcbxlist(self, widget_name, criteria, output):
# db = TimeCardDB()
# cbvalue = db.listActiveEmployees()
# db.dbclose()
widget = getattr(RecordTime,'dateLb')
widget[criteria] = output
###
class MainMenu(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
#tk.Frame.configure(self,background='red')
font9 = "-family {Minion Pro} -size 14 -weight bold"
self.controller = controller
recordBn = tk.Button(self,command=lambda: [self.controller.show_frame(RecordTime), self.controller.empcbxlist('self.dateLb','text','yessss')])
recordBn.place(relx=0.269, rely=0.646, height=50, width=180)
recordBn.configure(activebackground="#ececec")
recordBn.configure(activeforeground="#000000")
recordBn.configure(background="#d9d9d9")
recordBn.configure(disabledforeground="#a3a3a3")
recordBn.configure(font=font9)
recordBn.configure(foreground="#000000")
recordBn.configure(highlightbackground="#d9d9d9")
recordBn.configure(highlightcolor="black")
recordBn.configure(pady="0")
recordBn.configure(text='''Record Time''')#!/usr/bin/python
class RecordTime(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
dateLb = tk.Label(self)
dateLb.place(relx=0.213, rely=0.021, height=38, width=144)
dateLb.configure(activebackground="#f9f9f9")
dateLb.configure(activeforeground="black")
dateLb.configure(disabledforeground="#a3a3a3")
dateLb.configure(font="-family {Minion Pro} -size 14 -weight bold")
dateLb.configure(foreground="#000000")
dateLb.configure(highlightbackground="#d9d9d9")
dateLb.configure(highlightcolor="black")
dateLb.configure(text='''SHOW DATE''')
GFPTime().mainloop()
您的代码几乎没有问题。
self.frames
并用它们的类名保存每个帧的对象然后使用字典来获取他们的方法和属性不使用像widget = getattr(RecordTime,'dateLb')
这样的类名来访问它们而不是RecordTime
使用你保存的类在字典里。self.dateLb
,只需dateLb
即可。
更改:
self.controller.empcbxlist('self.dateLb','text','yessss')
至
self.controller.empcbxlist('dateLb','text','yessss')
dateLb
替换为self.dateLb
类中的RecordTime
”因此将dateLb
标签更改为self.dateLb
全球化。我将empcbxlist()
中的代码块更改为此。
def empcbxlist(self, widget_name, criteria, output):
# db = TimeCardDB()
# cbvalue = db.listActiveEmployees()
# db.dbclose()
# Get the frame.
ob = self.frames.get( RecordTime )
widget = getattr(ob,widget_name)
widget[criteria] = output
# can use this to change the text to be on safe side.
# for i, j in self.frames.items():
# if i.__name__ == RecordTime.__name__:
# widget = getattr(j,widget_name)
# widget[criteria] = output
完整代码:
#!/usr/bin/python
import os.path
import sys
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
# from PIL import Image, ImageTk
# from dbfunctions import *
import datetime
class GFPTime(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
tk.Tk.wm_title(self,"GFP Employee Timecard System")
tk.Tk.wm_geometry(self,"800x480+0+0")
container =tk.Frame(self)
container.pack(side='top',fill='both',expand= True)
container.grid_rowconfigure(0,weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames = {}
for F in (MainMenu,RecordTime):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0,column=0,stick='nsew')
self.show_frame(MainMenu)
# print(self.frames.values())
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
def empcbxlist(self, widget_name, criteria, output):
# db = TimeCardDB()
# cbvalue = db.listActiveEmployees()
# db.dbclose()
ob = self.frames.get( RecordTime )
widget = getattr(ob,widget_name)
widget[criteria] = output
###
class MainMenu(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
#tk.Frame.configure(self,background='red')
font9 = "-family {Minion Pro} -size 14 -weight bold"
self.controller = controller
recordBn = tk.Button(self,command=lambda: [self.controller.show_frame(RecordTime), self.controller.empcbxlist('dateLb','text','yessss')])
recordBn.place(relx=0.269, rely=0.646, height=50, width=180)
recordBn.configure(activebackground="#ececec")
recordBn.configure(activeforeground="#000000")
recordBn.configure(background="#d9d9d9")
recordBn.configure(disabledforeground="#a3a3a3")
recordBn.configure(font=font9)
recordBn.configure(foreground="#000000")
recordBn.configure(highlightbackground="#d9d9d9")
recordBn.configure(highlightcolor="black")
recordBn.configure(pady="0")
recordBn.configure(text='''Record Time''')#!/usr/bin/python
class RecordTime(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
self.dateLb = tk.Label(self)
self.dateLb.place(relx=0.213, rely=0.021, height=38, width=144)
self.dateLb.configure(activebackground="#f9f9f9")
self.dateLb.configure(activeforeground="black")
self.dateLb.configure(disabledforeground="#a3a3a3")
self.dateLb.configure(font="-family {Minion Pro} -size 14 -weight bold")
self.dateLb.configure(foreground="#000000")
self.dateLb.configure(highlightbackground="#d9d9d9")
self.dateLb.configure(highlightcolor="black")
self.dateLb.configure(text='''SHOW DATE''')
GFPTime().mainloop()
希望这可以帮助。
你的代码有点混乱,而且过于复杂。我把它缩小到一个更容易消化的大小 - 装饰只会增加代码的混乱。
请盯着它,直到你了解哪些元素正在做什么,并且可能从那里重建。
import tkinter as tk
import datetime
class GFPTime(tk.Tk):
def __init__(self):
super().__init__()
super().wm_title("GFP Employee Timecard System")
self.container = tk.Frame(self)
self.container.pack(side='top',fill='both',expand= True)
self.frames = {}
for F in (MainMenu, RecordTime):
self.frames[F] = F(self.container, self)
self.frames[F].grid(row=0,column=0,stick='nsew')
self.show_frame(MainMenu)
def show_frame(self, container):
self.frames[container].tkraise()
def empcbxlist(self, text_value):
self.show_frame(RecordTime)
self.frames[RecordTime].date_lbl.configure(text=text_value)
class MainMenu(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
record_btn = tk.Button(self, text='Record Time',
command=lambda text_value=str(datetime.date.today()): self.controller.empcbxlist(text_value))
record_btn.pack()
class RecordTime(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.date_lbl = tk.Label(self, text='SHOW DATE')
self.date_lbl.pack()
GFPTime().mainloop()