Home | Hardware | Internet News |Web Hosting |IT Management |Network Storage
LinuxPlanet
Search 
  Power Search | Tips 

 Front Door
 Discussion
 LinuxEngine
 Opinions
 Reports
 Reviews
 Tutorials
 News
 Technology Jobs

 Browse by subject.
Free Newsletter

Java/Open Source Daily
Linux Today
More Free Newsletters

Be a Commerce Partner


















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

Print this article
Email this article

   LinuxPlanet / Tutorials







GUI Programming in Python For Beginners: Create a Timer in 30 Minutes
Bringing Up New Windows

Akkana Peck
Thursday, March 26, 2009 11:54:38 AM

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

« Back: Saving Absent-Minded Geeks From Soggy Gardens and Burned Food

Skip Ahead

1 Saving Absent-Minded Geeks From Soggy Gardens and Burned Food
2 Packing Them In
3 Bringing Up New Windows
Figure 6
Figure 6





Linux is a trademark of Linus Torvalds.


internet.com home | search | help! | about us

Jupiter Online Media

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers