几天来我正在研究这个小型测试程序,但找不到自己的解决方案。该程序只是用不同的文本重新配置相同的标签。
在Final课程中,我有这行
print (self.parent.grid_defaultdict)
它告诉我“ defaultdict(,{0:,...“,我知道这些标签分别位于“工作表”(类),“ one_weekly_notebook(类)”和“最终”(类)中。
当我尝试从父类更改此标签时,
def display_schedule(self, button):
self.upper_tabs_dict["Final"].grid_defaultdict[0].configure(text = "John")
它告诉我“ AttributeError:“最终”对象没有属性“ grid_defaultdict””。
由于我知道我需要从Final类访问实例才能到达标签,所以我不断收到“属性错误”。请帮助我解决此代码的麻烦。我确定我没有正确地引用该对象,但是由于我现在正在看这几天,所以看来我找不到自己的错误。谢谢您的帮助。这是完整的代码:
import tkinter as tk
from tkinter import ttk
from collections import defaultdict
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("notbook my own try")
self.geometry("1200x650")
self.config(bg="tan")
self.style = ttk.Style()
self.lnb = Sheets(self)
class Sheets(ttk.Notebook):
def __init__(self, parent):
parent.style.configure("down.TNotebook", tabposition="sw")
super().__init__(parent, style = "down.TNotebook")
self.pack(fill = tk.BOTH, expand =1)
self.parent = parent
self.week_index = ['Week 1', ]
self.sheet_dict = {}
self.listbox_dict = {}
for num, week in enumerate(self.week_index, 1):
self.week = One_Weekly_Notebook(self, num)
self.week.pack()
self.add(self.week, text = week)
self.pack()
self.sheet_dict[week] = self.week
class One_Weekly_Notebook(ttk.Notebook):
def __init__(self, parent, index):
super().__init__(parent)
self.pack(fill = tk.BOTH, expand =1)
self.index = index
self.parent = parent
self.Week={}
self.upper_tabs_dict = {}
self.grid_defaultdict = defaultdict(list)
self.object_final = Final(self)
self.object_requests = Requests(self)
tab1 = self.object_final
tab2 = self.object_requests
tab1.pack()
tab2.pack()
self.add(tab1, text = "Final")
self.pack()
self.add(tab2, text = 'Requests')
self.pack()
self.upper_tabs_dict["Final"] = tab1
self.upper_tabs_dict["Requests"] = tab2
def display_results(self, button):
self.display_schedule(button)
def display_schedule(self, button):
self.upper_tabs_dict["Final"].label.configure(text = "J")
####--------------------Final Tab------------------------------####
class Final(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.pack(fill=tk.BOTH, expand=1)
self.parent = parent
self.Grid_Widget()
def Grid_Widget(self):
for i in range (7):
self.label = tk.Label(self, relief="ridge", width=11)
self.label.grid(row=0, column=i, sticky='nsew')
self.parent.grid_defaultdict[i] = self.label
self.parent.grid_defaultdict[0].configure(text = "Paul")
print (self.parent.grid_defaultdict)
self.buttons()
def buttons(self):
value = self.parent.index
self.button = tk.Button(self, text = "Caluculate")
self.button.configure(command= lambda button=value:
self.parent.display_results(button))
self.button.grid(row=10, column=8)
class Requests(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.pack(fill=tk.BOTH, expand=1)
self.parent = parent
self.Grid_Widget()
def Grid_Widget(self):
for i in range (7):
self.label = tk.Label(self, relief="ridge", width=11,)
self.label.grid(row=0, column=i, sticky='nsew')
self.parent.grid_defaultdict[i] = self.label
if __name__ == '__main__':
Application().mainloop()