Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > TKinter newbie

Reply
Thread Tools

TKinter newbie

 
 
Gigs_
Guest
Posts: n/a
 
      02-15-2007
Hi Im new to gui programming

from Tkinter import * # get widget classes
from tkMessageBox import askokcancel # get canned std dialog

class Quitter(Frame): # subclass our GUI
def __init__(self, parent=None): # constructor method
Frame.__init__(self, parent)
self.pack()
widget = Button(self, text='Quit', command=self.quit)
widget.pack(side=LEFT)
def quit(self):
ans = askokcancel('Verify exit', "Really quit?")
if ans: Frame.quit(self)

class Demo(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack()
Label(self, text="Basic demos").pack()
for (key, value) in demos.items():
func = (lambda key=key: self.printit(key))
Button(self, text=key, command=func).pack(side=TOP, fill=BOTH)
Quitter(self).pack() # here
def printit(self, name):
print name, 'returns =>', demos[name]()


My problem is in class Demo. How is the best way to use class Quitter in
class Demo?
should it be:
Quitter(self).pack()
Quitter(self)
....
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-15-2007
Gigs_ wrote:

> Hi Im new to gui programming
>
> from Tkinter import * # get widget classes
> from tkMessageBox import askokcancel # get canned std dialog
>
> class Quitter(Frame): # subclass our GUI
> def __init__(self, parent=None): # constructor method
> Frame.__init__(self, parent)
> self.pack()
> widget = Button(self, text='Quit', command=self.quit)
> widget.pack(side=LEFT)
> def quit(self):
> ans = askokcancel('Verify exit', "Really quit?")
> if ans: Frame.quit(self)
>
> class Demo(Frame):
> def __init__(self, parent=None):
> Frame.__init__(self, parent)
> self.pack()
> Label(self, text="Basic demos").pack()
> for (key, value) in demos.items():
> func = (lambda key=key: self.printit(key))
> Button(self, text=key, command=func).pack(side=TOP,
> fill=BOTH)
> Quitter(self).pack() # here
> def printit(self, name):
> print name, 'returns =>', demos[name]()
>
>
> My problem is in class Demo. How is the best way to use class Quitter in
> class Demo?
> should it be:
> Quitter(self).pack()
> Quitter(self)
> ...


You are calling the Quitter's pack() method twice, once in
Quitter.__init__() and then again in Demo.__init__(). I would remove the
call in Quitter.__init__(). If you do that you can use your Quitter class
with other layout managers which require other configuration methods, e. g.
Quitter(...).grid(...).

Peter

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      02-15-2007
Gigs_ wrote:
> Hi Im new to gui programming
>
> from Tkinter import * # get widget classes
> from tkMessageBox import askokcancel # get canned std dialog
>
> class Quitter(Frame): # subclass our GUI
> def __init__(self, parent=None): # constructor method
> Frame.__init__(self, parent)
> self.pack()
> widget = Button(self, text='Quit', command=self.quit)
> widget.pack(side=LEFT)
> def quit(self):
> ans = askokcancel('Verify exit', "Really quit?")
> if ans: Frame.quit(self)
>
> class Demo(Frame):
> def __init__(self, parent=None):
> Frame.__init__(self, parent)
> self.pack()
> Label(self, text="Basic demos").pack()
> for (key, value) in demos.items():
> func = (lambda key=key: self.printit(key))
> Button(self, text=key, command=func).pack(side=TOP, fill=BOTH)
> Quitter(self).pack() # here
> def printit(self, name):
> print name, 'returns =>', demos[name]()
>
>
> My problem is in class Demo. How is the best way to use class Quitter in
> class Demo?
> should it be:
> Quitter(self).pack()
> Quitter(self)
> ...


The Quitter really needs to Destroy its parent, so you will need to have
something like

self.parent = parent

in the __init__() method to keep a reference to the parent frame. Then
the quit() method can call self.parent.destroy() which will also result
in the destruction of the child Quitter.

In this particular case you don't appear to need a reference to the
Quitter object in the main Frame's code, so it's acceptable to use

Quitter(self).pack()

However in the more general case yo umight want to be abel to refer to
some subsidiary object in the Frame's methods, and in that case the
easiest way to do so is to save that reference when you create the
object, than pack the obejct separately, as in

self.quitter = Quitter(self)
self.quitter.pack()

Here's a simple program to show you the difference between quit() and
destroy(). You will notice that you have to press the Quit button twice,
but the Destroy button only once - once the window is destroyed calling
its mainloop() method no longer does anything.

from Tkinter import *
t = Tk()
Button(t, command=t.quit, text="Quit").pack()
Button(t, command=t.destroy, text="Destroy").pack()
t.mainloop()
t.mainloop()

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the differences between tkinter in windows and Tkinter inthe other platform? Hidekazu IWAKI Python 0 12-15-2009 05:58 AM
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
from Tkinter import *,win = Tk() "from Tkinter import *" Pierre Dagenais Python 0 08-03-2008 10:33 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57