[Tutor] Tkinter Q's
Michael Lange
klappnase at freenet.de
Tue Jul 12 22:51:09 CEST 2005
On Mon, 11 Jul 2005 17:22:57 +0000
Joseph Quigley <cpu.crazy at gmail.com> wrote:
Hi, Joseph,
> Hi first off, here's my code:
>
> # -*- coding: utf-8 -*-
> from Tkinter import *
> import random
> import time
> import about
> import quotes
>
>
> def closeprog():
> raise SystemExit
>
> class main:
> root = Tk()
> frame = Frame()
> root.title("Quoter %s" % (about.ver))
> root.minsize(300, 50)
>
> showquote = Label(root, text=random.choice(quotes.quote))
> showquote.pack()
>
> exit = Button(root, text="Exit", command=closeprog)
> exit.pack(side=LEFT)
>
> aboutprg = Button(root, text="About", command=about.main)
> aboutprg.pack(side=LEFT)
>
>
> totalq = Label(root, text=quotes.qts)
> totalq.pack(side=BOTTOM)
>
> root.mainloop()
>
> (I'd appreciate some suggestions, or notifications on how bad something is)
>
I think you should change the way you define the main class, so you keep references to the class attributes;
it looks like your main class fires up a Tk() window, so it's probably best to subclass Tk() :
class Main(Tk):# this doesn't really matter, but upper case letters are generally preferrred for class names
def __init__(self, *args, **kw):
Tk.__init__(self, *args, **kw)
# at this point the Main() class practically is a Tk(), so it can be handled just like a regular
# Tk() window from the outside; the "*args, **kw" construct allows to pass an arbitrary amount of
# arguments and keyword arguments to the parent class. "self" is a placeholder for the class instance
# that will be actually used in the code.
# To get a benefit over a normal Tk() window you can now start adding attributes:
self.showquote = Label(self, text=random.choice(quotes.quote))
self.showquote.pack()
< etc. >
# of course you can use the parent classes methods on "self", too:
self.title("Quoter %s" % (about.ver))
self.minsize(300, 50)
# now you can add a button which uses a "class-specific" command:
self.switchbutton = Button, text="Switch quote", command=self.switch_quote)
self.switchbutton.pack()
# finally the class method has to be defined:
def switch_quote(self):
newquote = get_the_new_quote()# it's up to you how to do this of course
self.showquote.configure(text=newquote)
Now the Main() class can be used like a regular Tk() :
root = Main()
root.mainloop()
And for something completely different:
be careful mixing pack(side = LEFT / RIGHT) with pack(side = BOTTOM / TOP),
you might not get the results you expected. For complex layouts you are probably better off
using grid() ( or you will find that you have to use extra Frames to pack() your widgets in.
I hope this helps
Michael
> I have a small problem: I don't know how to make a button that would
> redisplay another quote in the same window, ie I need a button that
> says: Show Another Quote. (Actually I don't know how to make it show
> another quote even in a new window!!). I got the interface from Catfood
> Fortune Cookie.
>
> Here's a tid-bit of the quotes module:
> # Brian Kernighan
> bk1 = """Controlling complexity is the essence of computer programming.
>
> -- Brian Kernighan"""
>
> yadayada = """Foo/bar"""
>
> quote = [bk1, yadayada]
>
> Thanks,
> Joe
>
> --
> Unix Love, Linux Pride
>
>
More information about the Tutor
mailing list