1

I'm confused by why nothing happens when the button is clicked. No error is thrown. Also, when trying to reference the controller via self.controller.get_page("Page2).Method(), nothing happened.

Tried to use lambda, but to no avail.

import tkinter as tk

class Main(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("200x200")

        container = tk.Frame(self)
        container.grid(row=0, column=0)

        self.frames = {}

        for F in (Page1, Page2):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0)

        self.show_frame(Page1)

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

    def get_page(self, page_name):
        return self.frames[page_name]


class Page1(tk.Frame):  # login page

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.p2 = Page2(parent="", controller=self)
        self.button = tk.Button(self, text="Cmd", command=self.otherchange)
        self.button.pack()

    def otherchange(self):
        self.p2.changelbl()

class Page2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.lbltochange = tk.Label(self, text="Label to change", bg="red")
        self.lbltochange.pack()

    def changelbl(self):
        self.lbltochange.config(bg="blue")

app = Main()
app.mainloop()
TrainTirade
  • 106
  • 8
  • Even if the command works, you're not gonna see any change in the UI, because the widget you want to change is not visible. You create 2 `Page2` frames but the second doesn't trigger the `grid` – PRMoureu Aug 23 '19 at 19:09
  • @PRMoureu Aside from the frames, how would I make it work? Oakley referenced an answer but that still doesn't explain where I'm going wrong. – TrainTirade Aug 26 '19 at 19:55
  • in a few words, you need to give the reference of a function `command=my_function`, not an executed function `command=my_function()`. That's why you can use a lambda expression or another solution provided in Bryan's answer. You should create a function that print something to see exactly when it's triggered, and then you switch your logic. – PRMoureu Aug 26 '19 at 20:01
  • @PRMoureu I created the function. The function merely has the .config command in it. I'm still met with the frustrating lack of output and same lack of error. Should I ask another question? – TrainTirade Aug 28 '19 at 17:45
  • @PRMoureu I've updated it - please tell me where I've gone wrong. – TrainTirade Aug 28 '19 at 22:06
  • Step 1 OK, now the command is working (if you print something inside it, you should see the result in the console). Then Step 2, the issue is about the frames as i told you. Delete the loop `for F in (Page1, Page2):` and create only a `Page1`, then add `self.p2.grid()` after `self.p2 = Page2(parent="", controller=self)`. Otherwise create a new question and let me know ;) – PRMoureu Aug 29 '19 at 05:56
  • @PRMoureu What do you mean by create only a `Page1`? – TrainTirade Aug 29 '19 at 10:58
  • Please consider to create another question, i cannot help you very well from the comments without code. Think about what frames are created and shown, and **add some print statements to understand the logic** – PRMoureu Aug 29 '19 at 16:18
  • @PRMoureu I have asked another question. – TrainTirade Aug 29 '19 at 16:25

0 Answers0