将功能分配给按钮不起作用

时间:2015-10-16 10:13:05

标签: python user-interface tkinter

所以我有这个代码,在tkinter中运行多个GUI窗口,我还有第二个代码包含分配给按钮的某个功能,这似乎不起作用。我非常累,无法找到解决方案,我确信它是一些基本的东西。我的意思是PageOne类中的toggle_text1命令。我会感激你的每一个帮助,谢谢!

import Tkinter as tk

TITLE_FONT = ("Helvetica", 18, "bold")

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=False)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo, Page3, Page4):
            frame = F(container, self)
            self.frames[F] = frame
            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, c):
        '''Show a frame for the given class'''
        frame = self.frames[c]
        frame.tkraise()

    def toggle_text1():

        if button1["text"] == "WL":
            button1["text"] = "WYL"
            label1["bg"] = "green"
            #wiringpi.pinMode(91,0)
            #wiringpi.digitalWrite(91,0)

        else:

            button1["text"] = "WL"
            label1["bg"] = "red"
            #wiringpi.pinMode(91,1)
            #wiringpi.digitalWrite(91,0)


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="left", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame(PageOne))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Go to Page 3",
                            command=lambda: controller.show_frame(Page3))
        button4 = tk.Button(self, text="Go to Page 4",
                            command=lambda: controller.show_frame(Page4))
        button1.pack(pady=10)
        button2.pack(pady=10)
        button3.pack(pady=10)
        button4.pack(pady=10)

哦,废话,我早些时候删除了小部件,忘记再次粘贴它,我的坏。现在怎么样?

class PageOne(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame(StartPage))
        button1 = tk.Button(self, text='WL', command=toggle_text1)
        button.pack()
        button1.pack()
        label1.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

1 个答案:

答案 0 :(得分:1)

如果你想在另一个类中使用一个函数,你的函数和按钮在不同的类中,那么你需要将它传递给你的构造函数。这就是你所做的。

for F in (StartPage, PageOne, PageTwo, Page3, Page4):
    frame = F(container, self)

您已将其作为控制器传递

class PageOne(tk.Frame):
    def __init__(self, parent, controller):

因此,在分配功能时,您需要在其前面添加controller

button1 = tk.Button(self, text='WL', command=controller.toggle_text1)

您已为所有lambda函数执行了哪些操作。您还需要将self添加为函数的参数