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
- 1Linux Top 3: Fedora 24, Peppermint 7 and Solus 1.2
- 2Linux Top 3: Alpine Linux 3.4, deepin 15.2 and Linux Lite 3.0
- 3Linux 4.7 Set to Boost Live Patching, Security and Power Management
- 4Linux 4.6 Charred Weasel adds USB 3.1 Support
- 5Linux Top 3: OpenIndiana 2016.04, Ubuntu 16.04 and Debian's New Leader