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

How to, in class 1, change the properties of a widget (label) in class 2 (Tkinter)?

我正在尝试在 MainMenu Class 中添加一个 recordBn 按钮来更改 RecordTime class 中 dateLb 标签的文本.但我不断收到如下错误消息。

AttributeError: 'RecordTime' object has no attribute 'dateLb'

使用 empcbxlist()GFPTime Class

我已尝试通过此 和其他几个 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()

您的代码几乎没有问题。

  1. 当你创建了一个字典 self.frames 并用它们的 class 名称保存每个帧的对象然后使用字典来获取它们的方法和属性不要使用 widget = getattr(RecordTime,'dateLb') 之类的 class 名称来访问它们,而不是 RecordTime 使用您保存在字典中的 class。

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

    变化:

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

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

  3. 正如@aw1668 所说 "Replace dateLb to self.dateLb in RecordTime class" 所以通过将 dateLb 标签更改为 self.dateLb.[=25 来全球化=]

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