Python tkinter error

Thread Starter

Vindhyachal Takniki

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

Code:
from Tkinter import *
import Tkinter



class menu1():

    def __init__(self,master):
        self.master = master

        #clear value
        self.val1 = 1

        #default selection
        self.v = IntVar()
        self.v.set(1)

        #create a label
        self.x1 = Label(self,text="Title",justify = CENTER,padx = 100)
        self.x1.pack()

        #first radiobutton
        self.x2 = Radiobutton(self,text="Title1",padx = 100,variable=self.v,value=1)
        self.x2.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.destroy()      

def screen():
    #create a root object
    root = Tk()
    app = menu1(root)
    app.title("Option")
    app.geometry("480x320")
    app.mainloop()
    return app.val1

screen();


error:
Code:
Traceback (most recent call last):
  File "C:/Users/abc/Desktop/c.py", line 47, in <module>
    screen();
  File "C:/Users/abc/Desktop/c.py", line 41, in screen
    app = menu1(root)
  File "C:/Users/abc/Desktop/c.py", line 19, in __init__
    self.x1 = Label(self,text="Title",justify = CENTER,padx = 100)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2591, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2081, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2059, in _setup
    self.tk = master.tk
AttributeError: menu1 instance has no attribute 'tk'
>>>
 

tjohnson

Joined Dec 23, 2014
611
Sorry for the delay in responding. I corrected the errors in your code:
Python:
from Tkinter import *
import Tkinter



class menu1():

    def __init__(self,master):
        self.master = master

        #clear value
        self.val1 = 1

        #default selection
        self.v = IntVar()
        self.v.set(1)

        #create a label
        self.x1 = Label(master,text="Title",justify = CENTER,padx = 100)
        self.x1.pack()

        #first radiobutton
        self.x2 = Radiobutton(master,text="Title1",padx = 100,variable=self.v,value=1)
        self.x2.pack(anchor=W)

        #create button
        self.x6 = Button(master,text="OK",command=self.submit)
        self.x6.pack()

    def submit(self):
        self.val1 = self.v.get()
        self.master.destroy()

    def suicide(self):
        self.val1  = 0
        self.master.destroy()    

def screen():
    #create a root object
    root = Tk()
    app = menu1(root)
    root.title("Option")
    root.geometry("480x320")
    root.mainloop()
    return app.val1

screen()
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:
Top