Python tkinter error

Discussion in 'Programmer's Corner' started by Vindhyachal Takniki, Oct 21, 2015.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    1. I have written below code. It shows error.
    Code & error are below.
    2. What does this statement mean:
    self.master=master

    Code (Text):
    1. from Tkinter import *
    2. import Tkinter
    3.  
    4.  
    5.  
    6. class menu1():
    7.  
    8.     def __init__(self,master):
    9.         self.master = master
    10.  
    11.         #clear value
    12.         self.val1 = 1
    13.  
    14.         #default selection
    15.         self.v = IntVar()
    16.         self.v.set(1)
    17.  
    18.         #create a label
    19.         self.x1 = Label(self,text="Title",justify = CENTER,padx = 100)
    20.         self.x1.pack()
    21.  
    22.         #first radiobutton
    23.         self.x2 = Radiobutton(self,text="Title1",padx = 100,variable=self.v,value=1)
    24.         self.x2.pack(anchor=W)
    25.  
    26.         #create button
    27.         self.x6 = Button(self,text="OK",command=self.submit)  
    28.         self.x6.pack()
    29.  
    30.     def submit(self):
    31.         self.val1 = self.v.get()
    32.         self.destroy()
    33.  
    34.     def suicide(self):
    35.         self.val1  = 0
    36.         self.destroy()      
    37.  
    38. def screen():
    39.     #create a root object
    40.     root = Tk()
    41.     app = menu1(root)
    42.     app.title("Option")
    43.     app.geometry("480x320")
    44.     app.mainloop()
    45.     return app.val1
    46.  
    47. screen();
    48.  


    error:
    Code (Text):
    1. Traceback (most recent call last):
    2.   File "C:/Users/abc/Desktop/c.py", line 47, in <module>
    3.     screen();
    4.   File "C:/Users/abc/Desktop/c.py", line 41, in screen
    5.     app = menu1(root)
    6.   File "C:/Users/abc/Desktop/c.py", line 19, in __init__
    7.     self.x1 = Label(self,text="Title",justify = CENTER,padx = 100)
    8.   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2591, in __init__
    9.     Widget.__init__(self, master, 'label', cnf, kw)
    10.   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2081, in __init__
    11.     BaseWidget._setup(self, master, cnf)
    12.   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2059, in _setup
    13.     self.tk = master.tk
    14. AttributeError: menu1 instance has no attribute 'tk'
    15. >>>
    16.  
     
  2. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    Sorry for the delay in responding. I corrected the errors in your code:
    Code (Python):
    1. from Tkinter import *
    2. import Tkinter
    3.  
    4.  
    5.  
    6. class menu1():
    7.  
    8.     def __init__(self,master):
    9.         self.master = master
    10.  
    11.         #clear value
    12.         self.val1 = 1
    13.  
    14.         #default selection
    15.         self.v = IntVar()
    16.         self.v.set(1)
    17.  
    18.         #create a label
    19.         self.x1 = Label(master,text="Title",justify = CENTER,padx = 100)
    20.         self.x1.pack()
    21.  
    22.         #first radiobutton
    23.         self.x2 = Radiobutton(master,text="Title1",padx = 100,variable=self.v,value=1)
    24.         self.x2.pack(anchor=W)
    25.  
    26.         #create button
    27.         self.x6 = Button(master,text="OK",command=self.submit)
    28.         self.x6.pack()
    29.  
    30.     def submit(self):
    31.         self.val1 = self.v.get()
    32.         self.master.destroy()
    33.  
    34.     def suicide(self):
    35.         self.val1  = 0
    36.         self.master.destroy()    
    37.  
    38. def screen():
    39.     #create a root object
    40.     root = Tk()
    41.     app = menu1(root)
    42.     root.title("Option")
    43.     root.geometry("480x320")
    44.     root.mainloop()
    45.     return app.val1
    46.  
    47. screen()
    48.  
    The line "self.master = master" makes the "master" variable available for use by the other functions in the "app1" class.

    Please check your code more thoroughly before posting it on the forum. I haven't used Tkinter much (it's probably been a year since I've programmed with it), but I was able to fix your code by looking at this and reading the traceback information carefully. My point is that I'm willing to help you, but please don't ask questions on this forum that you could easily find the answer to yourself.
     
    Last edited: Dec 25, 2015
Loading...