Python Programming for the Absolute Beginner, 3rd Edition

A Button widget can be activated by the user to perform some action. Since you already know how to create labels, learning how to create buttons will be pretty easy.

Introducing the Lazy Buttons Program

In the Lazy Buttons program, I create several buttons that don't do anything when activated. This is sort of like installing a new light fixture before wiring it. The fixture is put into place, but not yet functional. Figure 10.8 illustrates the program.

Figure 10.8: You can click these lazy buttons all you want; they won't do a thing.

Setting Up the Program

First, I set up the program by importing Tkinter and creating a root window and a frame:

# Lazy Buttons # Demonstrates creating buttons # Michael Dawson - 6/5/03 from Tkinter import * # create a root window root = Tk() root.title("Lazy Buttons") root.geometry("200x85") # create a frame in the window to hold other widgets app = Frame(root) app.grid()

Creating Buttons

You create a Button widget by instantiating an object of the Button class. That's what I did in the next lines:

# create a button in the frame bttn1 = Button(app, text = "I do nothing!") bttn1.grid()

These lines create a new button with the text I do nothing! The button's master is the frame I created earlier, which means that the button is placed in the frame.

The Tkinter module offers flexibility when it comes to creating, defining, and altering widgets. You can create a widget and set all of its options in one line (like I've been doing), or you can create a widget and set or alter its options later. I'll show you what I mean with the next button. First, I create a new button:

# create a second button in the frame bttn2 = Button(app) bttn2.grid()

Notice though that the only value I pass to the object's constructor is app, the button's master. So, all I've done is add a blank button to the frame. However, I can fix that. I can modify a widget after I create it, using the object's configure() method:

bttn2.configure(text = "Me too!")

This line sets the text option of the button to the string "Me too!", which puts the text Me too! on the button.

You can use a widget's configure() method for any widget option (and any type of widget). You can even use the method to change an option that you've already set.

Next, I create a third button:

# create a third button in the frame bttn3 = Button(app) bttn3.grid()

Then, I set the button's text option, using a different interface:

bttn3["text"] = "Same here!"

I access the button's text option through a dictionary-like interface. I set the text option to the string "Same here!", which puts the text Same here! on the button. When you set the value of an option using this type of dictionary-style access, the key for the option is the name of the option as a string.

Entering the Root Window's Event Loop

As always, I invoke the root window's event loop to start up the GUI:

# kick off the window's event loop root.mainloop()

Категории