October 31, 2014
 
 
RSSRSS feed

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

Bringing Up New Windows

  • March 26, 2009
  • By Akkana Peck

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.)

padx and 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.

Links:

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

Sitemap | Contact Us