Colored button causing a message pop-up

Buttons can be given different visual properties and complex behaviors. Here we create a blue raised button that changes appearance when clicked with a mouse. A message box widget is made to pop up when the button is pushed.

How to do it...

Execute the program shown in the normal way.

# button_message_1.py
#>>>>>>>>>>>>>>>>>>>
from Tkinter import *
import tkMessageBox
root = Tk()
root.title("Message Button")
def callback_button():
tkMessageBox.showinfo( "Certificate of Button Pushery",  "Are glowing pixels a reality?")
message_button = Button(root,
bd=6, # border width
relief = RAISED, # raised appearance # to button border
bg = "blue", # normal background
# color
fg = "green", # normal foreground
# (text) color
font = "Arial 20 bold",
text ="Push me", # text on button
activebackground = "red", # background when
# button is clicked
activeforeground = "yellow", # text color when
# clicked
command = callback_button) # name of event
# handler
message_button.grid(row=0, column=0)
root.mainloop()

How it works...

How it works...
How it works...

What we see now is that buttons are highly customizable, as are many Tkinter widgets. This recipe illustrates another term that you are bound to come across as a GUI programmer and that is the word focus.

Focus is the idea that when there are several widgets on a graphic container only one of them can be given attention or listened to at a time. Each button is programmed to respond to the click of a mouse but when the mouse is clicked, only one button should respond. The widget responding is the one that the program focuses on. In our example, you actually see the focus being given to the button when the mouse pointer moves across it the focus is used to change the button's coloring in a Linux operating system. It is like the chairman offering the floor to someone wanting to address a meeting group. The aspirant talker can only do so when the chairman offers them the floor (gives them focus). When this happens, everyone else is expected to be quiet and listen courteously.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123