如何通过按钮配置另一个类别的tk标签

时间:2019-08-23 18:41:20

标签: python tkinter

我为为什么单击按钮时什么都没有发生感到困惑。没有引发任何错误。另外,当尝试通过self.controller.get_page(“ Page2).Method()引用控制器时,什么也没发生。

试图使用lambda,但无济于事。

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()

0 个答案:

没有答案