[Tutor] Tkinter questions

Alan Gauld alan.gauld at freenet.co.uk
Mon Dec 20 15:12:08 CET 2004

> > I find the easiest way is to create an PhotoImage object attach
> > the graphic file(jpg,bmp,gif) to that and assign the PhotoImage
> > object to the Button.image property. You can "animate" the image
> > by simply reassigning the file to the underlying PhotoImage

> Thanks, but a practical explanation will be more helpful.

OK, I assume you mean code? :-)

Here is an example modified from some experiments using
the >>> prompt.  It should work but might need a tweak
along the way...

from Tkinter import *

state = 0

def changeIt(img, fname):
   global state
   if state == 0:
       state = 1
       img['file'] = 'img1.gif'

top = Tk()

# now create an image object and a button
theImg = PhotoImage(file='spam.gif')
b = Button(top, command = changeIt, image=theImg)


You'll need to provide your own image files :-)
You can also see this in action with a Text box in
my book chapter on building a games framework,
the commented code for which is on the Useless
Python website as hmgui.zip

> > You mean print as in to a printer?
> > Personally I tend to generate an HTML file and use the
> > native OS Tools to print that. You can get fancy and
> > use the native OS printing libraries but since its
> > very OS specific I find HTML is easier! OUtside Tkinter
> > you may well find the GUI libraries have done the cross
> > platform stuff for you, but not in Tkinter.
> Again, a practical explanation will be more helpful...

HTML generation is just a case of creating a text file with
appropriate HTML tags inserted. There are some modules around
that can help.

Printing the file then becomes a matter of finding the
appropriate commands in your OS and using the os.system()
call to execute them. For example Windows explorer uses
this command to print HTML on my system:

"D:\msoffice\OFFICE11\msohtmed.exe" /p %1

You'll need to look to find whats in use on yours...

> > Define a full window? You mean full screen?
> > Thats usually better done as a parameter that the user can
> > set unless there is a very good reason not to. (Personally
> > I refuse to use any program that insists on opening full screen!)

> Full window or full screen is when the window of the program is all
> over the screen except the start bar

Nope, Full Window means that something fills the current window,
which could be any size. Since that didn't fit your explanation
I asked for clarity. Full screen is where a single window fills
the currently available screen.

> And why you refuse to use any program that insists on opening full
screen ?
> If it does then there must be a good reason for that...

No, quite often its simply a fascist minded programmer who thinks
his application is so important that I won't want to do anything
else while it's running - games programmers are bad for this.
But since I very rarely do only one thing at a time on my PC
I want control of the window size thankyou very much - that's
why I have a 21 inch monitor running at 1600x1200!

There are very few valid situations for an application taking
over the whole screen univited! Normally the user should be able
to make that choice. (For example when editing video I usually
do go full screen, but I decide, not the program!)


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list