GUI Programming in Python For Beginners: Create a Timer in 30 Minutes - page 2
Packing Them In
Packing them in
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.
packdoesn't really give you much control over where widgets go, so most Tkinter programmers use another method for placing widgets:
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
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() ======================================================
- Skip Ahead
- 1. Saving Absent-Minded Geeks From Soggy Gardens and Burned Food
- 2. Packing Them In
- 3. Bringing Up New Windows
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
- 1Linux Top 3: Ubuntu 14.04, Debian Gives Squeeze More Life and Red Hat Goes Atomic
- 2Linux Top 3: CoreOS, Oracle Enterprise Linux 7 and Ubuntu 14.10
- 3Linux Top 3: Debian Dumps SPARC, Ubuntu Takes Over Linux 3.13 and the Core Infrastructure Initiative
- 4Linux Top 3: Fedora, Ubuntu and Gluster Lose Community Leaders
- 5Red Hat Enterprise Linux 7 Finally Hits the Big Time