Python; screen exit error

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
594
1. I have made a code in two files in python.
2. From main.py a function screen is called which have some radio button.
3. error is when I run the code & main screen is opened, if click "Ok"
button then it work fine. However if I close the window by pressing close button, error appears.
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
3. main.py
Code:
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)

4. main_screen.py
Code:
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
 

tjohnson

Joined Dec 23, 2014
611
The cause of the problem is obvious. In your suicide() function, you are erroneously calling self.screen.destroy, while in your submit() function you correctly call self.destroy.

In the future, please check your code more carefully before posting it.
 
Top