【发布时间】:2019-04-29 07:48:08
【问题描述】:
我试图在MainMenu 类中设置一个recordBn 按钮,以更改RecordTime 类中dateLb 标签的文本。但我不断收到如下错误消息。
AttributeError: 'RecordTime' object has no attribute 'dateLb'
使用GFPTime类的empcbxlist()
我已尝试通过此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()
【问题讨论】:
-
这里的代码太多了。请尽可能多地删除代码,同时仍然保留问题的本质。哦,还要格式化它,以便 Python 程序员可以阅读。
-
这是因为
dateLb是RecordTime类的__init__(...)内部的局部变量,所以不能在类外访问。在RecordTime类中将dateLb替换为self.dateLb。 -
@acw1668:这不会解决问题,因为他没有从他保存在列表
self.frame中的类中获取属性,而是直接从没有的类中调用它有一个实例。
标签: python python-3.x tkinter widget tk