Python; screen exit error

Discussion in 'Programmer's Corner' started by Vindhyachal Takniki, Jul 16, 2015.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    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 (Text):
    1.  
    2. Exception in Tkinter callback
    3. Traceback (most recent call last):
    4.   File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    5.     return self.func(*args)
    6.   File "E:\main_menu.py", line 51, in suicide
    7.     self.screen.destroy()
    8.   File "C:\Python27\lib\lib-tk\Tkinter.py", line 1898, in __getattr__
    9.     return getattr(self.tk, attr)
    10. AttributeError: screen
    11.  
    3. main.py
    Code (Text):
    1.  
    2. import main_menu
    3.  
    4. while True:
    5.     val = main_menu.screen()
    6.     if(1 == val):
    7.         //do something
    8.     elif(2 == val):
    9.         //do something
    10.     elif(3 == val):
    11.         //do something
    12.  
    13.     #print(val)    
    14.  

    4. main_screen.py
    Code (Text):
    1.  
    2. from Tkinter import *
    3. import Tkinter
    4.  
    5.  
    6.  
    7. class menu1(Tkinter.Tk):
    8.  
    9.     def __init__(self, master):
    10.         Tkinter.Tk.__init__(self,master)
    11.         self.master = master
    12.  
    13.         self.protocol("WM_DELETE_WINDOW", self.suicide)
    14.  
    15.         #clear value
    16.         self.val1 = 1
    17.  
    18.         #default selection
    19.         self.v = IntVar()
    20.         self.v.set(1)
    21.  
    22.         #create a label
    23.         self.x1 = Label(self,text="Choose a function:",justify = CENTER,padx = 100)
    24.         self.x1.pack()
    25.  
    26.         #first radiobutton
    27.         self.x2 = Radiobutton(self,text="text1",padx = 100,variable=self.v,value=1)
    28.         self.x2.pack(anchor=W)
    29.  
    30.         #second radiobutton
    31.         self.x3 = Radiobutton(self,text="text2",padx = 100,variable=self.v,value=2)      
    32.         self.x3.pack(anchor=W)
    33.  
    34.         #third radiobutton
    35.         self.x4 = Radiobutton(self,text="text3",padx = 100,variable=self.v,value=3)    
    36.         self.x4.pack(anchor=W)
    37.  
    38.         #fourth radiobutton
    39.         self.x5 = Radiobutton(self,text="text4",padx = 100,variable=self.v,value=4)    
    40.         self.x5.pack(anchor=W)
    41.  
    42.         #create button
    43.         self.x6 = Button(self,text="OK",command=self.submit)    
    44.         self.x6.pack()
    45.  
    46.     def submit(self):
    47.         self.val1 = self.v.get()
    48.         self.destroy()
    49.  
    50.     def suicide(self):
    51.         self.val1  = 0
    52.         self.screen.destroy()        
    53.  
    54. def screen():
    55.     #create a root object
    56.     root = menu1(None)
    57.     root.title("Option")
    58.     root.geometry("480x320")
    59.     root.mainloop()
    60.     return root.val1      
    61.  
     
  2. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    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.
     
  3. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
  4. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
Loading...