GUI Programming in Python For Beginners: Create a Timer in 30 Minutes - page 3
Bringing Up New Windows
That works fine. But you know what? That little dialog is awfully easy to miss, especially if you have a big screen with lots of other windows on it. It would be better to bring up a huge window that covers the whole screen.
Unfortunately, you can't do much about what tkMessageBox.showinfo() does. But you can make your own dialog quite easily:
def messageWindow() : win = Toplevel() b = Button(win, text='DING DING DING', bg="blue", fg="yellow", activebackground="red", activeforeground="white", command=quit) b.pack(ipadx=root.winfo_screenwidth()/2, ipady=root.winfo_screenheight()/2) root.mainloop()
What's new here?
First, the button colors: you can set any widget's colors to anything you like. For a button, the active colors are the ones it shows when the mouse is over the button, while the base colors apply when the mouse is somewhere else. (Move your mouse into the window's title bar to see the color change.)
pady specify how much padding the button should leave around the text in the middle. If the X padding is set to half the screen width on either side, then the button will be at least fill the screen, and likewise with height. Actually this requests a size slightly bigger than the screen, because the button's text and the window's titlebar take up some space too.
That's it! You can get more information on Tkinter programming from the links below.
Akkana Peck is a freelance programmer and writer and a Python fan; you can find some of her other Python hacks at http://shallowsky.com/software/ She's also the author of Beginning GIMP: From Novice to Professional.
====================================================== # Listing 6 #!/usr/bin/env python from Tkinter import * def messageWindow() : win = Toplevel() b = Button(win, text='DING DING DING', bg="blue", fg="yellow", activebackground="red", activeforeground="white", padx=root.winfo_screenwidth()/2, pady=root.winfo_screenheight()/2, command=quit) b.pack() root.mainloop() def show_alert() : root.bell() messageWindow() quit() def start_timer() : root.after(scale.get() * 60000, 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 timer", 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: GNOME 3.12 and New Betas for Ubuntu 14.04 and OpenMandriva Lx 2014.0
- 2Linux Top 3: Linus Lashes out, Linux 3.14 Gets PIE and Ubuntu One is Done.
- 3Linux Top 3: Ubuntu 14.04, Debian Gives Squeeze More Life and Red Hat Goes Atomic
- 4Linux Top 3: RHEL 6.5, Debian 7.2 and EOL for Linux 3.0.x
- 5Linux Top 3: CoreOS, Oracle Enterprise Linux 7 and Ubuntu 14.10