我试图编写一个python程序,我希望能够在单击时更新另一个类的按钮。例如:
gui.py文件:
class main(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.initUI()
#menu bar
menu = Menu(parent)
parent.config(menu=menu)
parent.option_add('*tearOff', FALSE)
#file menu
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="New Show", command=shows.saves.open)
fileMenu.add_command(label="Save Show", command=shows.saves.save)
fileMenu.add_command(label="Save Show As", command=shows.saves.saveas)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=self.quit)
#edit menu
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Copy (ctrl+c)", command=shows.saves.open)
editMenu.add_command(label="Cut (ctrl+x)", command=shows.saves.save)
editMenu.add_command(label="Paste (ctrl+v)", command=shows.saves.saveas)
#insert menu
insrtMenu = Menu(menu)
menu.add_cascade(label="Insert", menu=insrtMenu)
insrtMenu.add_command(label="Add Audo Cue (ctrl + 1)", command=shows.saves.open)
insrtMenu.add_command(label="Add Memo Cue (ctrl + 2)", command=shows.saves.save)
#toolbar
toolbar = Frame(parent)
b1 = Button(toolbar, text="Mode = Setup", command=m.mode.switch)
b1.pack(side=LEFT, padx=2, pady=2)
b2 = Button(toolbar, text="Preview", command=shows.saves.open)
b2.pack(side=LEFT, padx=2, pady=2)
b3 = Button(toolbar, text="Edit", command=shows.saves.open)
b3.pack(side=LEFT, padx=2, pady=2)
b4 = Button(toolbar, text="Edit", command=shows.saves.open)
b4.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
#main table
screen_width = parent.winfo_screenwidth()
screen_height = parent.winfo_screenheight()
listbox = Listbox(parent, width=screen_width, height=screen_height, bg="black", fg="white")
listbox.pack()
listbox.insert(END, "Cue1 - Rain")
def initUI(self):
self.parent.title("PyCue ALPHA 0.1")
#self.pack(fill=BOTH, expand=1)
m.py文件:
class mode():
def switch():
if b1["text"] == "Mode = Setup":
b1["text"] = "Mode = Run"
else:
b1["text"] = "Mode = Setup"
有人可以解释我怎么能这样做吗? 我的所有(非常alpha)代码都在github上:https://github.com/codefail/PyCue如果你想查看整个内容。
答案 0 :(得分:2)
我认为你需要使switch
成为一种静态方法:
class mode():
@staticmethod
def switch(b1):
if b1["text"] == "Mode = Setup":
b1["text"] = "Mode = Run"
else:
b1["text"] = "Mode = Setup"
您还需要一些方法将按钮传递给回调 - 通过将callback
传递给构造函数,您无法做到这一点。你需要分两行:
b1 = Button(toolbar, text="Mode = Setup")
b1.config(command=lambda : m.mode.switch(b1))
虽然,我真的不认为这里需要一个班级(mode
)。您似乎没有使用实例,因此您并没有真正共享任何数据......似乎您可以使用switch
函数并摆脱完全mode
课。