【发布时间】:2019-10-02 07:42:18
【问题描述】:
这是我较大程序的较小版本。当我在“第 1 周”点击“转发”Button 时,它应该打印 “Hi,从第 2 周开始”,但它仍在打印 “Hi,从第 1 周开始。”
只是为了确保我分配了正确的按钮小部件,因为这个小部件出现在几个地方,所以我使用下面的行来测试它,它适用于我想要的特定小部件。
self.parent.parent.sheet_dict["Week2"].upper_tabs_dict["Final"].button.configure(text = "red")
我不知道为什么我的forward(self): 函数不起作用。
感谢您花时间帮助我。
这是完整的代码:
import tkinter as tk
from tkinter import ttk
from functools import partial
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("notbook my own try")
self.geometry("700x450")
self.config(bg="tan")
self.style = ttk.Style() # instantce of ttk.Style() class.
self.lnb = Sheets(self) #composition (application has child)
class Sheets(ttk.Notebook): #contains the weeks tabs
def __init__(self, parent): #parent is root, which is tk.
parent.style.configure("down.TNotebook", tabposition="sw")
super().__init__(parent, style = "down.TNotebook")# this parent
is Notebook class.
self.pack(fill = tk.BOTH, expand =1)
self.parent = parent
self.week_index = ['Week 1', 'Week 2',"Week 3", "Week 4"] #
self.sheet_dict = {} #holds all the One_Weekly_Notebook's
instances
for week in (self.week_index):
self.week = One_Weekly_Notebook(self)#create instances
self.week.pack()
self.add(self.week, text = week)
self.pack()
self.sheet_dict[week] = self.week
class One_Weekly_Notebook(ttk.Notebook): #contains the tabs
def __init__(self, parent):
super().__init__(parent)
self.pack(fill = tk.BOTH, expand =1)
self.parent = parent
self.upper_tabs_dict = {}
self.calculate_buttons_dict = {} #to store all the calculate
buttons in differnt weeks
self.object_final = Final(self)
self.object_requests = Requests(self)
self.object_control = Control(self)
tab1 = self.object_final #child of lower notebook
tab2 = self.object_requests
tab3 = self.object_control
tab1.pack()
tab2.pack()
tab3.pack()
self.add(tab1, text = "Final")
self.pack()
self.add(tab2, text = 'Requests')
self.pack()
self.add(tab3, text = 'Control')
self.pack()
self.upper_tabs_dict["Final"] = tab1
self.upper_tabs_dict["Requests"] = tab2
self.upper_tabs_dict["Control"] = tab3
def display_results(self, button): #layer 3
self.say_hi(button)
def say_hi(self,button): #layer 3
self.id =
self.parent.parent.lnb.index(self.parent.parent.lnb.select())+1
# button=
self.parent.parent.lnb.index(self.parent.parent.lnb.select())+1
print (f"Hi from {button}")#current tap #)
class Final(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.pack(fill=tk.BOTH, expand=1)
self.parent = parent
self.buttons()
def buttons(self):
self.button = tk.Button(self, text="Say Hi", bg="salmon")
self.button.configure(command= lambda button=self.button:
self.parent.display_results(button))
self.button.pack()
self.parent.calculate_buttons_dict = self.button
self.buttonc = tk.Button(self, text = "Forward", command =
self.forward)
self.buttonc.pack()
print (self.parent.calculate_buttons_dict)
def forward(self):
self.parent.display_results(self.parent.parent.sheet_dict["Week
1"].upper_tabs_dict["Final"].button)
self.parent.display_results(self.parent.parent.sheet_dict["Week
2"].upper_tabs_dict["Final"].button)
self.parent.display_results(self.parent.parent.sheet_dict["Week
3"].upper_tabs_dict["Final"].button)
self.parent.display_results(self.parent.parent.sheet_dict["Week
4"].upper_tabs_dict["Final"].button)
class Requests(Final):
pass
class Control(tk.Frame):
pass
if __name__ == '__main__':
Application().mainloop()
enter code here
【问题讨论】:
-
嗨@Aran-Fey,我从链接中阅读了帖子。我不想传递一个值。我只想运行功能。另外,我在想,我没有将命令分配给右键吗?谢谢。
-
在链接的帖子中清楚地解释了您需要传递对函数的引用,无论您是否需要传递值。您在
self.buttonc上做对了,但您需要修复您的forward方法。 -
@HenryYik 让我再读一遍。也许我错过了什么。感谢您提供有关 forward 方法的提示。
-
嗨 @HenryYik 我又读了几遍,根据 Brian Oakley 的说法“要传递引用,您必须只使用名称,而不使用括号或参数......”我把括号去掉了仍然无法正常工作。我只是困惑。帖子的其他部分在函数中总是有“论据”。我还阅读了 efbot 的链接,我明白了所有这些。它们在函数中有参数。再暗示你可以给我吗?谢谢。
标签: python button tkinter tabs command