I have two tkinter Frame
s through which I'm trying to pass values to determine which button was pressed. Here is what I have:
class GUIHandler(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frames = {}
for F in (MainFrame, GraphsTask):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(MainFrame)
class MainFrame(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.button_text = tk.StringVar()
tk.Button(self, text="Task 2a",
command=lambda: self.button_controller(GraphsTask, 'task_2a'))
def button_controller(self, class_name, btn_text):
self.controller.show_frame(class_name)
self.button_text.set(btn_text)
def get_button_text(self):
print(self.button_text.get())
return self.button_text.get()
class GraphsTask(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.bind("<<Show>>", self.get_graph)
def get_graph(self, event):
if self.controller.frames[MainFrame].get_button_text() == 'task_2a':
....
When I try to compare the result I get from get_button_text()
function from MainFrame
class, it returns empty even though the print statement inside that function prints the text I passed onto button_controller()
function. I do not understand what is going on here.
You should update self.button_text
first and then switch page:
def button_controller(self, class_name, btn_text):
self.button_text.set(btn_text)
self.controller.show_frame(class_name)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.