Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将功能分配给按钮不起作用_Python_User Interface_Tkinter - Fatal编程技术网

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

Python 将功能分配给按钮不起作用,python,user-interface,tkinter,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, **kwa

因此,我有这段代码,在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()

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

for F in (StartPage, PageOne, PageTwo, Page3, Page4):
    frame = F(container, self)
您已将其作为控制器传入

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
因此,在分配函数时,需要在其前面加上控制器

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

您已经为所有lambda函数完成了这些操作。此外,您还需要将self作为参数添加到函数中

我非常累,无法找到解决方案,我相信这是一些基本问题-然后睡觉,早上再试一次。SO不是来为您进行基本调试的。当我搜索toggle_text1时,我看不到您在任何地方实际使用按钮的功能。对不起,我的错,我已经删除了小部件,现在重新加载了它,您现在可以检查它吗?这是当您在不理解的情况下复制/粘贴代码时发生的情况。你的问题的答案就在你要问的按钮代码前一行。不要简单地从stackoverflow复制代码,从中学习。这就是我要做的,也许我应该从零开始编写代码,你是对的,但我正在寻找各种方法来制作这个多窗口GUI,并将功能暗示到按钮上,这就是我喜欢的,谢谢。我按照你说的做了,该函数现在如下所示:def toggle_text1 self:if button1[text]==WL:button1[text]=WYL label1[bg]=green else:button1[text]=WL label1[bg]=红色当我执行代码并继续使用按钮时,如果button1[text]==WL:NameError:global name“button1”未定义,我会在toggle_text1的第36行获得此错误消息文件/Users/user/Desktop/Sky System/untitled/multwind2.py,由于button1是在PageOne类中定义的,我不知道如何指示我看到的功能所需的按钮。您需要将button1指定为PageOne类的一个变量,它看起来像self。_button1=tk.Buttonself,text='WL',command=self.toggle_text1,并在函数中为button1的每个实例加上self.frames[PageOne]前缀,我假定self.frames[PageOne]是一个字典,因此它看起来像self.frames[PageOne]。button1[text]==WL: