Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 为什么tkinter QUIT2小部件需要我按两次才能激活?_Python_Tkinter - Fatal编程技术网

Python 为什么tkinter QUIT2小部件需要我按两次才能激活?

Python 为什么tkinter QUIT2小部件需要我按两次才能激活?,python,tkinter,Python,Tkinter,出于某种原因,当我按下QUIT小部件时,它可以工作,但位于SecondPage类中的QUIT2小部件需要我单击两次。让它这样做的代码有什么问题 干杯,Marc您启动了两个主循环,因此需要两次呼叫“退出”才能退出您的程序 要么从解释器(隐式mainloop)启动脚本,要么在主程序中调用root或appmainloop(在示例中不可见,但需要以某种方式才能看到第一个窗口) 在StartPage中启动第二个主循环。说“嗨” 我不确定预期的行为,但它确实会导致两个mailoop,并且需要两个对quit的

出于某种原因,当我按下QUIT小部件时,它可以工作,但位于SecondPage类中的QUIT2小部件需要我单击两次。让它这样做的代码有什么问题


干杯,Marc

您启动了两个主循环,因此需要两次呼叫“退出”才能退出您的程序

  • 要么从解释器(隐式mainloop)启动脚本,要么在主程序中调用
    root
    app
    mainloop(在示例中不可见,但需要以某种方式才能看到第一个窗口)
  • StartPage中启动第二个主循环。说“嗨”

  • 我不确定预期的行为,但它确实会导致两个mailoop,并且需要两个对
    quit
    的调用才能同时离开这两个mailoop。如果您从
    开始页中删除
    test.mainloop()
    。说“嗨”
    ,您的问题就消失了。

    您启动了两个mainloop,因此需要两次调用“退出”才能离开您的程序

  • 要么从解释器(隐式mainloop)启动脚本,要么在主程序中调用
    root
    app
    mainloop(在示例中不可见,但需要以某种方式才能看到第一个窗口)
  • StartPage中启动第二个主循环。说“嗨”

  • 我不确定预期的行为,但它确实会导致两个mailoop,并且需要两个对
    quit
    的调用才能同时离开这两个mailoop。如果您从
    起始页删除
    test.mainloop()
    ,那么您的问题就消失了。

    一般来说,您应该在tkinter程序中准确调用
    mainloop()
    一次。一般来说,您应该在tkinter程序中准确调用
    mainloop()
    一次。
    from openpyxl import *
    from tkinter import *
    
    def inputGetter(str):
        print(str, end="")
        return input()
    
    
    
    class StartPage(Frame):
        global app
        def say_hi(self):
            test = SecondPage(master=root)
            app.destroy()
            test.mainloop()
    
        def createWidgets(self):
            self.QUIT = Button(self)
            self.QUIT["text"] = "QUIT"
            self.QUIT["fg"]   = "red"
            self.QUIT["command"] =  self.quit
    
            self.QUIT.pack({"side": "left"})
    
            self.hi_there = Button(self)
            self.hi_there["text"] = "Hello",
            self.hi_there["command"] = self.say_hi
    
            self.hi_there.pack({"side": "left"})
    
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.pack()
            self.createWidgets()
    
    class SecondPage(Frame):
        global app
        def say_hi(self):
            print('test')
    
        def createWidgets(self):
            self.QUIT2 = Button(self)
            self.QUIT2["text"] = "QUIT2"
            self.QUIT2["fg"]   = "red"
            self.QUIT2["command"] =  self.quit
    
            self.QUIT2.pack({"side": "left"})
    
            self.hi_there = Button(self)
            self.hi_there["text"] = "Second",
            self.hi_there["command"] = self.say_hi
    
            self.hi_there.pack({"side": "left"})
    
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.pack()
            self.createWidgets()
    
    root = Tk()
    app = StartPage(master=root)
    
    app.mainloop()
    root.destroy()