August 20, 2014
 
 
RSSRSS feed

GUI Programming in Python For Beginners: Create a Timer in 30 Minutes - page 2

Packing Them In

  • March 26, 2009
  • By Akkana Peck

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() ======================================================

Sitemap | Contact Us