我是python的新手,我试图动态地更改按钮的文本和命令来生成子菜单。 我的代码是:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tkinter
class Display(tkinter.Tk):
def __init__(self,parent):
tkinter.Tk.__init__(self,parent)
self.parent = parent
self.GuiDisplay()
def GuiDisplay(self):
self.grid()
self.geometry("1280x720")
#self.overrideredirect(True) #uncomment for fullscreen
"""Build the GUI"""
MessageDisplay = tkinter.Label(self, text = 'No messages from slaves', anchor = 'nw', justify = 'left', relief = 'ridge', font = ("Courier New", 15), bg = '#e4e7e8', fg = '#2980b9')
MessageDisplay.grid(row = 0, column = 0, columnspan = 3, sticky = 'wens', ipadx = 2, ipady = 2)
ClockDisplay = tkinter.Label(self, text = '00:00', relief = 'ridge', font = ("Courier New", 40), bg = '#e4e7e8', fg = '#2980b9')
ClockDisplay.grid(row = 0, column = 3, sticky = 'wens', ipadx = 2, ipady = 2)
Menu1Button = tkinter.Button(self, text = 'Ring', command = self.Action(Do = 'Rng'), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
Menu1Button.grid(row = 1, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
Menu2Button = tkinter.Button(self, text = 'Toggle holliday mode', command = self.Action(Do = 'SwHolDay'), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
Menu2Button.grid(row = 2, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
Menu3Button = tkinter.Button(self, text = 'Edit time', command = self.SetMenu(MenuIndex = 2), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
Menu3Button.grid(row = 3, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
Menu4Button = tkinter.Button(self, text = 'Add/Edit alarms', command = self.SetMenu(MenuIndex = 3), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
Menu4Button.grid(row = 4, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
Menu5Button = tkinter.Button(self, text = 'Shutdown', command = self.Action(Do = 'Shutdwn'), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
Menu5Button.grid(row = 5, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
MenuDescription = tkinter.Label(self, text = 'No menu selected.', anchor = 'nw', justify = 'left', relief = 'ridge', font = ("Courier New", 15), bg = '#e4e7e8', fg = '#2980b9')
MenuDescription.grid(column = 2, columnspan = 2, row = 1, rowspan = 5, sticky = 'wens', ipadx = 2, ipady = 2)
self.grid_columnconfigure(0, weight = 3)
self.grid_columnconfigure(1, weight = 3)
self.grid_columnconfigure(2, weight = 3)
self.grid_columnconfigure(3, weight = 1)
self.grid_rowconfigure(0, weight = 10)
self.grid_rowconfigure(1, weight = 18)
self.grid_rowconfigure(2, weight = 18)
self.grid_rowconfigure(3, weight = 18)
self.grid_rowconfigure(4, weight = 18)
self.grid_rowconfigure(5, weight = 18)
def SetMenu(self, MenuIndex):
if MenuIndex==1:
self.Menu1Button.configure(text = 'Ring', command = self.Action(Do = 'Rng'))
self.Menu2Button.configure(text = 'Toggle holliday mode', command = self.Action(Do = 'SwHolDay'))
self.Menu3Button.configure(text = 'Edit time', command = self.SetMenu(MenuIndex = 2))
self.Menu4Button.configure(text = 'Add/Edit alarms', command = self.SetMenu(MenuIndex = 3))
self.Menu5Button.configure(text = 'Shutdown', command = self.Action(Do = 'Shutdwn'))
elif MenuIndex==2:
self.Menu1Button.configure(text = 'Edit hours', command = self.Action(Do = 'ModH'))
self.Menu2Button.configure(text = 'Edit minutes', command = self.Action(Do = 'ModM'))
self.Menu3Button.configure(text = 'Edit seconds', command = self.Action(Do = 'ModS'))
self.Menu4Button.configure(text = 'Internet synchronization', command = self.Action(Do = 'Synch'))
self.Menu5Button.configure(text = 'Return to menu', command = self.SetMenu(MenuIndex = 1))
elif MenuIndex==3:
self.Menu1Button.configure(text = 'Edit hours', command = self.Action(Do = 'ModHA'))
self.Menu2Button.configure(text = 'Edit minutes', command = self.Action(Do = 'ModMA'))
self.Menu3Button.configure(text = 'Edit seconds', command = self.Action(Do = 'ModSA'))
self.Menu4Button.configure(text = 'Switch mode', command = self.Action(Do = 'ChMod'))
self.Menu5Button.configure(text = 'Return to menu', command = self.SetMenu(MenuIndex = 1))
def Action(self, Do):
pass
if __name__ == "__main__":
app = Display(None)
app.title('Web Bell')
app.mainloop()
错误的回溯是
^{pr2}$我猜它来自SetMenu函数中的“self”,但是我尝试过不使用它,结果还是不起作用。在
编辑: 更新的代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tkinter
class Display(tkinter.Tk):
def __init__(self,parent):
tkinter.Tk.__init__(self,parent)
self.parent = parent
self.GuiDisplay()
def GuiDisplay(self):
self.grid()
self.geometry("1280x720")
#self.overrideredirect(True) #uncomment for fullscreen
"""Build the GUI"""
self.MessageDisplay = tkinter.Label(self, text = 'No messages from slaves', anchor = 'nw', justify = 'left', relief = 'ridge', font = ("Courier New", 15), bg = '#e4e7e8', fg = '#2980b9')
self.MessageDisplay.grid(row = 0, column = 0, columnspan = 3, sticky = 'wens', ipadx = 2, ipady = 2)
self.ClockDisplay = tkinter.Label(self, text = '00:00', relief = 'ridge', font = ("Courier New", 40), bg = '#e4e7e8', fg = '#2980b9')
self.ClockDisplay.grid(row = 0, column = 3, sticky = 'wens', ipadx = 2, ipady = 2)
self.Menu1Button = tkinter.Button(self, text = 'Ring', command = self.Action(Do = 'Rng'), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
self.Menu1Button.grid(row = 1, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
self.Menu2Button = tkinter.Button(self, text = 'Toggle holliday mode', command = self.Action(Do = 'SwHolDay'), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
self.Menu2Button.grid(row = 2, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
self.Menu3Button = tkinter.Button(self, text = 'Edit time', command = self.SetMenu(MenuIndex = 2), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
self.Menu3Button.grid(row = 3, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
self.Menu4Button = tkinter.Button(self, text = 'Add/Edit alarms', command = self.SetMenu(MenuIndex = 3), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
self.Menu4Button.grid(row = 4, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
self.Menu5Button = tkinter.Button(self, text = 'Shutdown', command = self.Action(Do = 'Shutdwn'), font = ("Courier New", 20), bg = '#3498db', fg = '#e4e7e8', activebackground = '#2980b9', activeforeground = '#e4e7e8')
self.Menu5Button.grid(row = 5, column = 0, columnspan = 2, sticky = 'wens', padx = 2, ipadx = 4)
self.MenuDescription = tkinter.Label(self, text = 'No menu selected.', anchor = 'nw', justify = 'left', relief = 'ridge', font = ("Courier New", 15), bg = '#e4e7e8', fg = '#2980b9')
self.MenuDescription.grid(column = 2, columnspan = 2, row = 1, rowspan = 5, sticky = 'wens', ipadx = 2, ipady = 2)
self.grid_columnconfigure(0, weight = 3)
self.grid_columnconfigure(1, weight = 3)
self.grid_columnconfigure(2, weight = 3)
self.grid_columnconfigure(3, weight = 1)
self.grid_rowconfigure(0, weight = 10)
self.grid_rowconfigure(1, weight = 18)
self.grid_rowconfigure(2, weight = 18)
self.grid_rowconfigure(3, weight = 18)
self.grid_rowconfigure(4, weight = 18)
self.grid_rowconfigure(5, weight = 18)
def SetMenu(self, MenuIndex):
if MenuIndex==1:
self.Menu1Button.configure(text = 'Ring', command = self.Action(Do = 'Rng'))
self.Menu2Button.configure(text = 'Toggle holliday mode', command = self.Action(Do = 'SwHolDay'))
self.Menu3Button.configure(text = 'Edit time', command = self.SetMenu(MenuIndex = 2))
self.Menu4Button.configure(text = 'Add/Edit alarms', command = self.SetMenu(MenuIndex = 3))
self.Menu5Button.configure(text = 'Shutdown', command = self.Action(Do = 'Shutdwn'))
elif MenuIndex==2:
self.Menu1Button.configure(text = 'Edit hours', command = self.Action(Do = 'ModH'))
self.Menu2Button.configure(text = 'Edit minutes', command = self.Action(Do = 'ModM'))
self.Menu3Button.configure(text = 'Edit seconds', command = self.Action(Do = 'ModS'))
self.Menu4Button.configure(text = 'Internet synchronization', command = self.Action(Do = 'Synch'))
self.Menu5Button.configure(text = 'Return to menu', command = self.SetMenu(MenuIndex = 1))
elif MenuIndex==3:
self.Menu1Button.configure(text = 'Edit hours', command = self.Action(Do = 'ModHA'))
self.Menu2Button.configure(text = 'Edit minutes', command = self.Action(Do = 'ModMA'))
self.Menu3Button.configure(text = 'Edit seconds', command = self.Action(Do = 'ModSA'))
self.Menu4Button.configure(text = 'Switch mode', command = self.Action(Do = 'ChMod'))
self.Menu5Button.configure(text = 'Return to menu', command = self.SetMenu(MenuIndex = 1))
def Action(self, Do):
pass
if __name__ == "__main__":
app = Display(None)
app.title('Web Bell')
app.mainloop()
您尚未将
Menu1Button
定义为类属性,因此它只在您的__init__
内限定作用域。应定义为:对于构造函数中定义的所有其他小部件也是如此。在
编辑:
所以现在第一个问题解决了,问题就变得更清楚了。您错误地定义了小部件的命令属性。在
例如,对于
self.Menu3Button
,可以将命令定义为self.SetMenu(MenuIndex = 2)
。在python中,这将尝试在函数2
(侧边栏:您不需要执行MenuIndex=2
,只要2
就可以了)来执行它。这将导致函数SetMenu
在Menu3Button
定义为类属性之前执行。因此python无法找到该属性,因此得到一个AttributeError
。在传递给小部件的
^{pr2}$command
应该是一个函数实例。例如:在有效的情况下,我们传递的是一个部分函数(即参数已经附加的半形式函数):
所以在你的情况下:
变成
这对于所有小部件的
command
关键字都是一样的。在有关lambda函数的详细信息,请参阅此处:https://docs.python.org/2/tutorial/controlflow.html#lambda-expressions
相关问题 更多 >
编程相关推荐