Home | Hardware | Internet News |Web Hosting |IT Management |Network Storage
LinuxPlanet
Search 
  Power Search | Tips 

 Front Door
 Discussion
 LinuxEngine
 Opinions
 Reports
 Reviews
 Tutorials
 News
 Technology Jobs

 Browse by subject.
Free Newsletter

Java/Open Source Daily
Linux Today
More Free Newsletters

Be a Commerce Partner


















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

Print this article
Email this article

   LinuxPlanet / Tutorials







GUI Programming in Python For Beginners: Create a Timer in 30 Minutes
Packing Them In

Akkana Peck
Thursday, March 26, 2009 11:54:38 AM

Packing them in

The pack commands for each of these widget -- button.pack(), label.pack(side=LEFT), and so forth -- tell Tkinter to add the widget in the window that contains it.

But pack doesn't really give you much control over where widgets go, so most Tkinter programmers use another method for placing widgets: grid.

Think of your window as a grid, with some number of rows and columns. Each time you add a new widget, you can specify exactly where in the grid it goes. For example, try removing all the pack lines from your script, and replace them with these grid calls:

minutes.grid(row=0, column=0)

scale.grid(row=0, column=1)

button.grid(row=1, column=1, pady=5, sticky=E)

Now the window looks like Figure 4 (Listing 4).

Doing something useful: setting the timer

Now you have a nice window ... but it still doesn't do anything yet. How do you make it start a timer when you click Start?

The answer is that command=quit when you created the Button. quit is the name of the function that's called when the user clicks the button. You can replace it with your own function. That function will need to get the number of minutes the user chose from the Scale widget. You do that with scale.get(). So your function might look like this:

def start_timer() :
    root.after(scale.get() * 60000, show_alert)
scale.get() gets the value the user set with the slider. root.after() sets a timeout -- but it's in milliseconds, not minutes, so you have to multiply the scale's value by 60,000.

Here's a tip, though: when you're debugging, you might want to use 1000 rather than 60000, so the slider represents seconds, not minutes and you don't have to wait so long to see if your code works.

Now all that's left is the show_alert() function that grabs your attention when the timer fires. Here's one that beeps and then shows an "info" dialog:

def show_alert() :
    root.bell()
    tkMessageBox.showinfo("Ready!", "DING DING DING!")
    quit()

bell() makes a noise, while tkMessageBox.showinfo() will bring up a little dialog (Figure 5, Listing 5).

======================================================
# Listing 4
#!/usr/bin/env python

from Tkinter import *

root = Tk()

minutes = Label(root, text="Minutes:")
minutes.grid(row=0, column=0)

scale = Scale(root, from_=1, to=45, orient=HORIZONTAL, length=300)
scale.grid(row=0, column=1)

button = Button(root, text="Start timing", command=quit)
button.grid(row=1, column=1, pady=5, sticky=E)

root.mainloop()
======================================================

====================================================== # Listing 5 #!/usr/bin/env python from Tkinter import * import tkMessageBox def show_alert() : root.bell() tkMessageBox.showinfo("Ready!", "DING DING DING!") quit() def start_timer() : # Next line should be * 60000, but use 1000 to speed debugging: root.after(scale.get() * 1000, show_alert) root = Tk() minutes = Label(root, text="Minutes:") minutes.grid(row=0, column=0) scale = Scale(root, from_=1, to=45, orient=HORIZONTAL, length=300) scale.grid(row=0, column=1) button = Button(root, text="Start timing", command=start_timer) button.grid(row=1, column=1, pady=5, sticky=E) root.mainloop() ======================================================

Next: Bringing Up New Windows »

Skip Ahead

1 Saving Absent-Minded Geeks From Soggy Gardens and Burned Food
2 Packing Them In
3 Bringing Up New Windows
Figure 4
Figure 4

Figure 5
Figure 5





Linux is a trademark of Linus Torvalds.


internet.com home | search | help! | about us

Jupiter Online Media

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers