Images on buttons and button packing

By placing GIF format images onto buttons, we can create any appearance desirable. The images can convey information about the function of the button. Image size has to be taken into account and the geometry manager has to be used thoughtfully.

How to do it...

Execute the program shown in exactly the same way as usual.

# image_button_1.py
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title("Image Sized Buttons")
go_image = PhotoImage(file = "/constr/pics1/go_on.gif")
fireman_image = PhotoImage(file = "/constr/pics1/fireman_1.gif")
winged_lion_image = PhotoImage(file = "/constr/pics1/winged_lion.gif")
earth_image = PhotoImage(file = "/constr/pics1/earth.gif")
def callback_go():
print "Go has been pushed to no purpose"
def callback_fireman():
print "A little plastic fireman is wanted"
def callback_lion():
print "A winged lion rampant would look cool on a t-shirt"
def callback_earth():
print "Think of the children (and therefore also of their parents)"
btn_go= Button(root, image = go_image, 
command=callback_go ).grid(row=0, column=0)
btn_firmean= Button(root, image = fireman_image, 
command=callback_fireman).grid(row=0, column=1)
btn_lion= Button(root, image = winged_lion_image, 
command=callback_lion ).grid(row=0, column=2)
btn_earth= Button(root, image = earth_image, 
command=callback_earth ).grid(row=0, column=3)
root.mainloop()

How it works...

How it works...

The thing to notice here is that the grid geometry manager packs all the widgets together as neatly as it can regardless of widget size.

There's more...

One of the wonderful thoughts behind the design of Python modules is that their actions should be kind and tolerant. This means that if attributes are coded with unsuitable values then defaults will be selected by the interpreter as at least some choice that is likely to work. This is an enormous boon to coders. If you ever come across one of the inner circle of Python developers they deserve an affectionate hug for this reason alone.

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

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