Author Topic: Python tkinter error  (Read 2592 times)

0 Members and 1 Guest are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 487
Python tkinter error
« on: October 21, 2015, 03:23:23 pm »
1. I have written below code. It shows error.
Code & error are below.
2. What does this statement mean:
self.master=master

Code: [Select]
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();

Code: [Select]
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'
>>>
 

Offline Maxlor

  • Frequent Contributor
  • **
  • Posts: 565
  • Country: ch
Re: Python tkinter error
« Reply #1 on: October 21, 2015, 03:57:21 pm »
- Your Menu1 class should probably inherit from a default tkinter class.
- mainloop() should be called on the root object.
- self.master = master causes the method parameter called master to be stored in the member variable (self.master) so that it can be accessed later, in another method.
 

Offline krish2487

  • Frequent Contributor
  • **
  • Posts: 500
  • Country: dk
Re: Python tkinter error
« Reply #2 on: October 21, 2015, 06:52:30 pm »
It means you are trying to bite more than you can chew.
Not to discourage you, but maybe you should gain some experience in working more on python before you can get started with tkinter
Tkinter, if memory serves correctly, is python's answer to Qt.


And yes, you should also read up and understand OOPs concepts and general programming methodologies.
Wiki is a good place to start and you can always buy books based on what you would like to pursue.
(Mike McGrath - pythons book comes to mind) It is a good read for a beginner.
If god made us in his image,
and we are this stupid
then....
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8275
Re: Python tkinter error
« Reply #3 on: October 22, 2015, 03:48:22 am »
...and what does this have to do with electronics? I was hoping you'd at least mention "Arduino".
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf