蟒蛇;屏幕退出错误

时间:2015-07-16 14:43:36

标签: python-2.7 tkinter

  1. 我在python中用两个文件创建了一个代码。
  2. 从main.py调用一个功能屏幕,它有一些单选按钮。
  3. 错误是我运行代码&主屏幕打开,如果点击“确定” 按钮然后它工作正常。但是,如果按关闭按钮关闭窗口,则会出现错误。

    [code]
    
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
        return self.func(*args)
    File "E:\main_menu.py", line 51, in suicide
        self.screen.destroy()
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1898, in __getattr__
        return getattr(self.tk, attr)
    AttributeError: screen
    [/code] 
    
  4. main.py     [代码]

    import main_menu
    
    while True:
        val = main_menu.screen()
        if(1 == val):
            //do something
        elif(2 == val):
            //do something
        elif(3 == val):
            //do something
    
        #print(val)    
    [/code]  
    
  5. main_screen.py     [代码]

    from Tkinter import *
    import Tkinter
    
    
    
    class menu1(Tkinter.Tk):
    
        def __init__(self, master):
            Tkinter.Tk.__init__(self,master)
            self.master = master
    
            self.protocol("WM_DELETE_WINDOW", self.suicide)
    
            #clear value
            self.val1 = 1
    
            #default selection
            self.v = IntVar()
            self.v.set(1)
    
            #create a label
            self.x1 = Label(self,text="Choose a function:",justify = CENTER,padx = 100)
            self.x1.pack()
    
            #first radiobutton
            self.x2 = Radiobutton(self,text="text1",padx = 100,variable=self.v,value=1)
            self.x2.pack(anchor=W)
    
            #second radiobutton
            self.x3 = Radiobutton(self,text="text2",padx = 100,variable=self.v,value=2)      
            self.x3.pack(anchor=W)
    
            #third radiobutton
            self.x4 = Radiobutton(self,text="text3",padx = 100,variable=self.v,value=3)    
            self.x4.pack(anchor=W)
    
            #fourth radiobutton
            self.x5 = Radiobutton(self,text="text4",padx = 100,variable=self.v,value=4)    
            self.x5.pack(anchor=W)
    
            #create button
            self.x6 = Button(self,text="OK",command=self.submit)    
            self.x6.pack()
    
        def submit(self):
            self.val1 = self.v.get()
            self.destroy()
    
        def suicide(self):
            self.val1  = 0
            self.screen.destroy()        
    
    def screen():
        #create a root object
        root = menu1(None)
        root.title("Option")
        root.geometry("480x320")
        root.mainloop()
        return root.val1      
    [/code]  
    

1 个答案:

答案 0 :(得分:2)

self.screen.destroy()会引发异常,因为您没有self.screen

我认为您只需要self.destroy(),就像上面submit()方法

一样