[Tutor] GUI-trouble

Liam Clarke cyresse at gmail.com
Sat Apr 9 05:29:52 CEST 2005


Hi Øyvind, 

A few errors in this here function - 

> def message():
> print New_Toplevel_1.v.get()
> # print self.v.get() This doesn't seem to work either

Your class New_Toplevel_1 is not instantiated as New_Toplevel_1 - you 
created an instance of it as 

>w = New_Toplevel_1 (root)

So def message() should have this line - 

>print w.v.get()

That wouldn't work either, however, and I'll explain why shortly. 
I just wanted to point out for the moment that 
>#print self.v.get()

will never work. You can only call self.attributes within a class i.e. 

>>> class Foo:
... def __init__(self):
... self.name <http://self.name> = "Bob"
...
... def changeName(self):
... self.name <http://self.name> = "Dave"
...
... def getThenChangeName(self):
... print self.name <http://self.name>
... self.changeName()
... print 'Name changed to', self.name <http://self.name>
... 
>>> h = Foo()
>>> h.getThenChangeName()

Bob
Name changed to Dave

Whereas, trying to use self in a function will always result in an error. 

>>> def trySelfInAFunction():
... self.bob = 20
... print 'Did it work?'
... print self.bob
... 
>>> trySelfInAFunction()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in trySelfInAFunction
NameError: global name 'self' is not defined


Ok... so you need your function message to be inside your class....

I've made your code work, I'll paste it up and highlight the important bits. 


from Tkinter import *
import Tix, sys

def vp_start_gui():
global w
global root
root = Tix.Tk()
root.title('New_Toplevel_1')
#root.geometry('200x205+1157+142')
w = New_Toplevel_1 (root)
#init()
root.mainloop()



class New_Toplevel_1:
def __init__(self, master=None):

self.but26 = Button (master)
self.but26.place(in_=master,x=60,y=30)
self.but26.configure(command=self.message)
self.but26.configure(text="button")
self.but26.pack()

self.che27 = Checkbutton(master)
self.che27.place(in_=master,x=50,y=120)
self.che27.configure(text="check")
self.v = IntVar()
self.che27.configure(variable=self.v)
self.che27.pack()

def message(self):
#print New_Toplevel_1.v.get()
print self.v.get() # This doesn't seem to work either

if __name__ == '__main__':
vp_start_gui()


Here are the main changes - 

Line 9 - root.geometry('200x205+1157+142') 

I had to comment this out to get your window to show. Having poked at 
root.geometry, I must say I think that that one was implemented funny. 
Passing a string for window parameters.... try passing root.geometry('200 x 
205 + 1157 + 142') and see the errors you get from that. Highly unpythonic, 
in my arrogant opinion.

So, anyway, for the time being, comment out that line.

line 21 - self.but26.configure(command=message) changed to 
self.but26.configure(command = self.message)

As you'll be calling a class method, you need to explicitly call self.method
.

line 23 - inserted self.but26.pack()

AFAIK, you need to do this to display your widget.

line 28 v = IntVar() changed to self.v = IntVar()

Within a class, if you want a variable to act as a global, you need to make 
it global. 

class Foo():

def __init__(self):
x = "Hi"

def getX(self):
print x

>>>w = Foo()
>>> print w.x
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: Foo instance has no attribute 'x'

Would need to be - 

class Foo:

def __init__(self):
self.x = 'Hi'

def getX(self):
print self.x


line 29 self.che27.configure(variable=v) changed to self.che27.configure
(variable=self.v)

line 30 self.che27.pack() added

line 33 - 34 - Following method added to class - 

def message(self):
print self.v.get() 

Remember, you are calling class methods from within your class, not external 
functions.


You aren't having GUI trouble, quite frankly, you're having object trouble, 
classes are quite an integral part of GUI toolkits, so you need to learn a 
bit more about class methods, attributes etc. 

I recommend http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/index.htm, 

Alan Gauld's wonderful programming tutorial.

Good luck.

Liam Clarke

On Apr 8, 2005 11:11 PM, Øyvind <python at kapitalisten.no> wrote:
> Hello.
> 
> I am trying to make a little gui. It has a button and a checkbox. If I
> push the button, it is supposed to print the value of the checkbox. But, I
> can not seem to get it right. How should I define the variable? Where
> should I put it? I have tried every way possible, and gotten lots of
> different errormessages.
> 
> Thanks in advance
> Øyvind
> 
> from Tkinter import *
> import Tix, sys
> 
> def vp_start_gui():
> global w
> global root
> root = Tix.Tk()
> root.title('New_Toplevel_1')
> root.geometry('200x205+1157+142')
> w = New_Toplevel_1 (root)
> init()
> root.mainloop()
> 
> def message():
> print New_Toplevel_1.v.get()
> # print self.v.get() This doesn't seem to work either
> 
> class New_Toplevel_1:
> def __init__(self, master=None):
> pass
> 
> self.but26 = Button (master)
> self.but26.place(in_=master,x=60,y=30)
> self.but26.configure(command=message)
> self.but26.configure(text="button")
> 
> self.che27 = Checkbutton (master)
> self.che27.place(in_=master,x=50,y=120)
> self.che27.configure(text="check")
> v = IntVar()
> self.che27.configure(variable=v)
> 
> if __name__ == '__main__':
> vp_start_gui()
> 
> --
> This email has been scanned for viruses & spam by Decna as - www.decna.no<http://www.decna.no>
> Denne e-posten er sjekket for virus & spam av Decna as - www.decna.no<http://www.decna.no>
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well 
please.
And with it comes the only basic human duty, to take the consequences.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050409/4f6b06ee/attachment.htm


More information about the Tutor mailing list