Reputation: 443
I'm trying to write a python program and I want to be able to update a button from a another class when it is clicked. eg:
The gui.py file:
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)
the m.py file:
class mode():
def switch():
if b1["text"] == "Mode = Setup":
b1["text"] = "Mode = Run"
else:
b1["text"] = "Mode = Setup"
Could someone please explain how I could go about doing this? All my (very alpha) code is on github: https://github.com/codefail/PyCue if you want to view the whole thing.
Upvotes: 0
Views: 138
Reputation: 310307
I think you need to make switch
a staticmethod:
class mode():
@staticmethod
def switch(b1):
if b1["text"] == "Mode = Setup":
b1["text"] = "Mode = Run"
else:
b1["text"] = "Mode = Setup"
You also need some way of passing the button to the callback -- which you can't do by passing callback
to the constructor. You'll need to do it in 2 lines:
b1 = Button(toolbar, text="Mode = Setup")
b1.config(command=lambda : m.mode.switch(b1))
Although, I really don't see the need for a class (mode
) here. You don't seem to be using an instance, and therefore you're not really sharing any data ... It seems like you could just use a switch
function and get rid of the mode
class entirely.
Upvotes: 2