2013-02-12 3 views
5

में मेनलोप से बाहर निकलें हालांकि मैं अन्य भाषाओं में एक प्रयोगात्मक प्रोग्रामर हूं, मैं पाइथन में बहुत नया हूं। मैं शुरू करने के बाद मेनलोप छोड़ने के लिए एक बहुत ही सरल चीज करने की कोशिश कर रहा हूं। ऐसा लगता है कि यह एक बड़ा सौदा है। नीचे दिया गया कार्यक्रम केवल घटनाओं का अनुक्रम बनाता है। सबकुछ काम कर रहा है, लेकिन मैं अंतिम खिड़की को बंद करने में सक्षम नहीं हूं ... मुझे क्या करना चाहिए?पायथन

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

आप 'रूट .withdraw() ' – user19911303

उत्तर

4

कॉल root.quit(), 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() 

इसके अलावा, वहाँ अगर समारोह root.quit कहते हैं सब आप क्या करना चाहते एक वर्ग CloseEnd बनाने के लिए कोई जरूरत नहीं है।

+0

का उपयोग कर सकते हैं धन्यवाद! लेकिन बटन द एंड दबाने के बाद, कार्यक्रम अटक गया! मैं अजगर 2.7.3 का उपयोग कर रहा हूँ! वैसे, टीकेटर को टीके के रूप में परिभाषित करने का क्या फायदा है? मैंने देखा कि अन्य लोग भी ऐसा कर रहे हैं, लेकिन मुझे कारण समझ में नहीं आया! – DanielTheRocketMan

+0

क्या आप एक आईडीई का उपयोग कर रहे हैं? यदि ऐसा है, तो स्क्रिप्ट को कमांड लाइन से चलाने का प्रयास करें: 'python script.py'। मुझे लगता है कि इसे ठीक काम करना चाहिए। – unutbu

+1

हालांकि कुछ लोग तर्क दे सकते हैं कि 'टिंकर' से आयात * ठीक है - वास्तव में, मुझे लगता है कि 'टिंकर' के लेखक ने इसे इस तरह आयात करने के लिए डिज़ाइन किया है - * सामान्य रूप से * केवल 'आयात * ...' इंटरैक्टिव सत्र में, स्क्रिप्ट में नहीं। अधिक verbose 'tkter के रूप में tkinter आयात करें, आप स्पष्ट रूप से स्पष्ट करते हैं कि प्रत्येक ऑब्जेक्ट कहां से आ रहा है। इससे अन्य लोगों के कोड को डीबग करने या पढ़ने में मदद मिलती है, और यह नाम टकराव को रोकता है जब दो मॉड्यूल एक ही नाम का उपयोग करते हैं। उदाहरण के लिए, 'numpy.sum' बिल्टिन पायथन 'sum' से अलग है। 'numpy' से आयात * भयानक होगा। – unutbu