在类1中,如何更改类2(Tkinter)中小部件(标签)的属性?

时间:2019-04-29 07:48:09

标签: python python-3.x tkinter widget tk

我试图在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()

2 个答案:

答案 0 :(得分:0)

您的代码很少出现问题。

  1. 创建字典self.frames并使用其类名保存每个框架的对象之后,请使用字典获取其方法和属性,而不要使用{{1 }}而不是widget = getattr(RecordTime,'dateLb')来访问它们,而是使用您保存在字典中的类。

  2. 在lambda函数中不要给窗口小部件名称RecordTime,只需self.dateLb就足够了。

    更改:

    dateLb

    收件人

    self.controller.empcbxlist('self.dateLb','text','yessss')

  3. 正如@ aw1668所说,“将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

希望这会有所帮助。

答案 1 :(得分:0)

您的代码有点混乱,并且过于复杂。我将其减小为更易于理解的大小-点缀只会增加代码的混乱程度。

请凝视它,直到您了解什么元素在做什么,然后也许从那里重建。

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()