【问题标题】:How can I prevent my main window from running with a Toplevel window in python and Tkinter?如何防止我的主窗口在 python 和 Tkinter 中使用 Toplevel 窗口运行?
【发布时间】:2021-07-07 00:49:53
【问题描述】:

我试图阻止主窗口运行,直到在单独的顶层窗口上按下按钮。

例子:

from tkinter import *

let_user_through = False

window = Tk()

def activate_main_window():
    global let_user_through
    let_user_through = True

frame = Toplevel()
b = Button(frame, text="Enter", command=activate_main_window).pack()


if let_user_through == True:
    lbl = Label(window, text="Hello")
    #bunch of code
    #bunch of code


window.mainloop()

在这个例子中,在主窗口中有一个标签,上面写着:“你好”。 但我不希望人们如果没有按下框架上的按钮就可以看到它

一旦用户按下按钮,框架将自行销毁,主窗口将继续执行一堆代码。

我是 tkinter 的初学者,所以我不确定答案是否显而易见。谢谢!

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您可以使用frame.wait_window() 等到frame 被销毁。您还需要在activate_main_window() 中调用frame.destroy()

    from tkinter import *
    
    let_user_through = False
    
    window = Tk()
    
    def activate_main_window():
        global let_user_through
        let_user_through = True
        frame.destroy() # need to destroy frame
    
    # wait for root window becomes visible
    # otherwise "frame" may be open behind root window
    window.wait_visibility()
    frame = Toplevel()
    Button(frame, text="Enter", command=activate_main_window).pack()
    
    frame.grab_set() # capture keyboard/mouse events
    frame.wait_window() # wait for "frame" to be destroyed
    
    if let_user_through:
        Label(window, text="Hello").pack()
        #bunch of code
        #bunch of code
    
    # should it be within the above for loop?
    window.mainloop()
    

    【讨论】:

      【解决方案2】:

      使用window.withdrawwindow.deiconify 对您的代码进行小改动对我有用。 @acw1668 正确指出了我原始代码中的一个错误,所以这里是修复。

      在用户按下按钮之前,您的主窗口是不可见的。

      
      import tkinter as tk
      
      let_user_through = False
      
      window = tk.Tk()
      window.withdraw()
      
      def activate_main_window():
          global let_user_through
          let_user_through = True
          frame.destroy() # need to destroy frame
      
      frame = tk.Toplevel()
      tk.Button(frame, text="Enter", command=activate_main_window).pack()
      
      frame.wait_window() # wait for "frame" to be destroyed
      
      if let_user_through:
          tk.Label(window, text="Hello").pack()
          window.update()
          window.deiconify()
          
          #bunch of code
          #bunch of code
      
      window.mainloop()
      
      

      我创建了一个class,不再需要let_user_through,并为任何后续步骤设置代码。

      
      import tkinter as tk
      
      class invisible:
      
          def __init__( self ):
      
              self.window = tk.Tk()
              self.window.withdraw() # make window invisible
      
              self.frame = tk.Toplevel()
              tk.Button(
                  self.frame, text = "Enter", command = self.activate_main_window ).pack( fill='both' )
              self.frame.wait_window( ) # wait for "frame"
      
              self.button = tk.Button( self.window, text = "Hello", command = self.remove_next )
              self.button.pack( fill = 'both')
              self.window.update()
              self.window.deiconify() # make window visible
      
          def activate_main_window( self ):
      
              self.frame.destroy() # need to destroy frame
      
          def remove_next( self ):
      
              self.button.destroy()
      
              tk.Label( self.window, text = "Bunch of codeA" ).pack( fill = 'both' )
              tk.Label( self.window, text = "Bunch of codeB" ).pack( fill = 'both' )
              tk.Label( self.window, text = "Bunch of codeC" ).pack( fill = 'both' )
              # continue code initialization
      
      if __name__ == '__main__':
      
          make = invisible()
          tk.mainloop()
      
      

      【讨论】:

      • 您的代码中有两个问题:1)if let_user_through: 将在用户单击Enter 按钮之前进行评估,因此它将是False,并且不会创建“Hello”标签即使在用户点击了Enter 按钮之后; 2) 如果用户使用标题栏上的关闭按钮关闭frame 窗口,根窗口将不可见。
      • 哇,谢谢大家!两个答案都有效,尽管我尝试运行 @Derek 的代码并且第一部分有效,但是在第二段代码中,带有按钮的窗口甚至从未打开过,无论如何现在它可以工作了,这很好。
      猜你喜欢
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      相关资源
      最近更新 更多