2013-12-12 103 views
0

我想第二個'Enter'按鈕允許用戶從這個窗口中退出。命令是什麼?我相信self.quit會退出一切,但我用過的命令不起作用。如何在不退出程序的情況下退出tkinter中的窗口?

import tkinter as tk 

class Enter_Name_Window(tk.Toplevel): 
    '''A simple instruction window''' 
    def __init__(self, parent): 
     tk.Toplevel.__init__(self, parent) 
     self.text = tk.Label(self, width=40, height=2, text= "Please enter your name and class.") 
     self.text.pack(side="top", fill="both", expand=True) 

     enter_name = Entry(self) 
     enter_name.pack() 
     enter_name.focus_set() 


     def callback(): 
      self.display_name = tk.Label(self, width=40, height=2, text = "Now please enter your tutor group.") 
      self.display_name.pack(side="top", fill="both", expand=True) 
      tutor = Entry(self) 
      tutor.pack() 
      tutor.focus_set() 
      Enter_0.config(state="disabled") 

      Enter_0_2 = Button(self, text="Enter", width=10, command=Enter_Name_Window.quit) 
      Enter_0_2.pack() 

     Enter_0 = Button(self, text="Enter", width=10, command=callback) 
     Enter_0.pack() 
+0

您的代碼格式化混亂了,很難讀取代碼。 –

+0

「退出此窗口」是什麼意思?你問如何摧毀窗戶,或只是隱藏它? –

回答

1

有很多的錯誤與最值得注意的是開始:

command=Enter_Name_Window.quit 

應該從使用quit()方法,其不穩定的

command=self.destroy 

副歌,並通過類的實例self而不是新的類對象

Anywhere Anywhere e的修改後的代碼:

class Enter_Name_Window(tk.Toplevel): 
    '''A simple instruction window''' 
    def __init__(self, parent): 
     tk.Toplevel.__init__(self, parent) 
     self.parent = parent 
     self.text = tk.Label(self.parent, width=40, height=2, text= "Please enter your name and class.") 
     self.text.pack(side="top", fill="both", expand=True) 

     enter_name = tk.Entry(self) 
     enter_name.pack() 
     enter_name.focus_set() 


     def callback(): 
      self.display_name = tk.Label(self.parent, width=40, height=2, text = "Now please enter your tutor group.") 
      self.display_name.pack(side="top", fill="both", expand=True) 
      tutor = tk.Entry(self.parent) 
      tutor.pack() 
      tutor.focus_set() 
      Enter_0.config(state="disabled") 

      Enter_0_2 = tk.Button(self.parent, text="Enter", width=10, command=self.destroy) 
      Enter_0_2.pack() 

     Enter_0 = tk.Button(self.parent, text="Enter", width=10, command=callback) 
     Enter_0.pack() 
相關問題