2013-02-12 24 views
5

Mặc dù tôi là một loại lập trình thử nghiệm trong các ngôn ngữ khác, tôi rất mới trong Python. Tôi đã cố gắng để làm một điều rất đơn giản đó là để thoát khỏi mainloop sau khi bắt đầu. Có vẻ như đó là một vấn đề lớn. Chương trình dưới đây chỉ tạo ra một chuỗi sự kiện. Mọi thứ dường như đang hoạt động, nhưng tôi không thể đóng cửa sổ cuối cùng ... Tôi phải làm gì?bỏ mainloop trong python

from Tkinter import * 

root=Tk() 
theMainFrame=Frame(root) 
theMainFrame.pack() 



class CloseAfterFinishFrame1(Frame): # Diz que herda os parametros de Frame 
    def __init__(self): 
     Frame.__init__(self,theMainFrame) # Inicializa com os parametros acima!! 
     Label(self,text="Hi",font=("Arial", 16)).pack() 
     button = Button (self, text = "I am ready", command=self.CloseWindow,font=("Arial", 12)) 
     button.pack()    
     self.pack() 

    def CloseWindow(self): 
     self.forget() 
     CloseAfterFinishFrame2() 



class CloseAfterFinishFrame2(Frame): # Diz que herda os parametros de Frame 
    def __init__(self): 
     Frame.__init__(self,theMainFrame) # Inicializa com os parametros acima!! 
     Label(self,text="Hey",font=("Arial", 16)).pack() 
     button = Button (self, text = "the End", command=self.CloseWindow,font=("Arial", 12)) 
     button.pack() 
     self.pack()   
    def CloseWindow(self): 
     self.forget() 
     CloseEnd() 


class CloseEnd(): 
    theMainFrame.quit() 



CloseAfterFinishFrame1() 

theMainFrame.mainloop() 
+0

bạn có thể sử dụng 'root .withdraw()' – user19911303

Trả lời

4

Gọi root.quit(), không theMainFrame.quit:

import Tkinter as tk 

class CloseAfterFinishFrame1(tk.Frame): # Diz que herda os parametros de Frame 
    def __init__(self, master): 
     self.master = master 
     tk.Frame.__init__(self, master) # Inicializa com os parametros acima!! 
     tk.Label(self, text="Hi", font=("Arial", 16)).pack() 
     self.button = tk.Button(self, text="I am ready", 
          command=self.CloseWindow, font=("Arial", 12)) 
     self.button.pack() 
     self.pack() 

    def CloseWindow(self): 
     # disable the button so pressing <SPACE> does not call CloseWindow again 
     self.button.config(state=tk.DISABLED) 
     self.forget() 
     CloseAfterFinishFrame2(self.master) 

class CloseAfterFinishFrame2(tk.Frame): # Diz que herda os parametros de Frame 
    def __init__(self, master): 
     tk.Frame.__init__(self, master) # Inicializa com os parametros acima!! 
     tk.Label(self, text="Hey", font=("Arial", 16)).pack() 
     button = tk.Button(self, text="the End", 
          command=self.CloseWindow, font=("Arial", 12)) 
     button.pack() 
     self.pack() 

    def CloseWindow(self): 
     root.quit() 

root = tk.Tk() 
CloseAfterFinishFrame1(root) 
root.mainloop() 

Ngoài ra, không có nhu cầu để thực hiện một lớp CloseEnd nếu tất cả các bạn muốn làm là gọi hàm root.quit.

+0

Cảm ơn bạn! Nhưng sau khi nhấn nút theEnd, chương trình sẽ bị kẹt! Tôi đang sử dụng python 2.7.3! Nhân tiện, lợi thế của việc xác định Tkinter là tk là gì? Tôi thấy những người khác cũng làm điều đó, nhưng tôi không hiểu lý do! – DanielTheRocketMan

+0

Bạn đang sử dụng IDE? Nếu vậy, hãy thử chạy tập lệnh từ dòng lệnh: 'python script.py'. Tôi nghĩ rằng nó sẽ hoạt động tốt. – unutbu

+1

Mặc dù một số người có thể cho rằng 'import * from Tkinter' là được rồi - thực sự, tôi nghĩ tác giả của' Tkinter' đã thiết kế nó được nhập theo cách đó - * nói chung * chỉ nên sử dụng 'import * ...' trong các phiên tương tác, không phải trong tập lệnh. Bằng cách sử dụng 'Tkinter nhập khẩu có giá trị hơn như tk', bạn làm rõ chính xác nơi mọi đối tượng đến từ đó. Điều đó giúp khi gỡ lỗi hoặc đọc mã của người khác, và nó ngăn chặn xung đột tên khi hai mô-đun sử dụng cùng tên. Ví dụ, 'numpy.sum' khác với hàm dựng sẵn' tổng hợp 'của Python. 'import * from numpy' sẽ là khủng khiếp. – unutbu

Các vấn đề liên quan