Mailing List Archive

Stupid Tkinter question
Hi,

I want to display a button in my application that shows a bitmap (.xbm).
I'm going crazy. Why does this fragment work (adapted from imageview.py
in Guido's Tkinter demos):

[snip]
#!/usr/bin/env python

from Tkinter import *
import sys

def main():
filename = sys.argv[1]
root = Tk()
img = BitmapImage(file=filename)
label = Button(root, text=filename, image=img, command=sys.exit)
label.pack()
root.mainloop()

main()
[snip]

.... but this doesn't:

[snip]
#!/usr/bin/env python

from Tkinter import *
from sys import *

def makeToolbar(aFrame):
toolbar = Frame(aFrame)
markimage = BitmapImage(file = "Mark.xbm")
markbutton = Button(toolbar,
image = markimage,
command = exit)

markbutton.pack()
return toolbar

def makeUI(anApp):
frame = Frame(anApp)
toolbar = makeToolbar(frame)

toolbar.pack()
frame.pack()

app = Tk()
makeUI(app)
app.mainloop()
[snip]

As far as I can see, the two are doing exactly the same. The first
example displays the bitmap, and the button is clickable, the second
displays a button the size of the bitmap, but which has nothing in it,
and which isn't clickable.

HELP!

TIA,
--Bjoern
PS Python 1.5.1 on Linux, if that is important.
--
+ thanks for your time, worship the antichrist, and have a nice day /\
+----------------------------------------------------------------- /()\
+ support privacy on the 'net: mail me for pgp public key /____\
Stupid Tkinter question [ In reply to ]
Hi,

On Mon, 12 Apr 1999 I wrote myself:
[snip]
>[snip]
>#!/usr/bin/env python
>
>from Tkinter import *
>from sys import *
>
>def makeToolbar(aFrame):
> toolbar = Frame(aFrame)
> markimage = BitmapImage(file = "Mark.xbm")
> markbutton = Button(toolbar,
> image = markimage,
> command = exit)
>
> markbutton.pack()
> return toolbar
>
>def makeUI(anApp):
> frame = Frame(anApp)
> toolbar = makeToolbar(frame)
>
> toolbar.pack()
> frame.pack()
>
>app = Tk()
>makeUI(app)
>app.mainloop()
>[snip]

I've just found out why the example above doesn't work: If I add a
"global markimage" to the function makeToolbar, it works. Now my
question: Why does markimage have to be global, since it's only used by
markbutton? And why, by analogy, doesn't markbutton have to be global?

--Bjoern
--
+ thanks for your time, worship the antichrist, and have a nice day /\
+----------------------------------------------------------------- /()\
+ support privacy on the 'net: mail me for pgp public key /____\
Stupid Tkinter question [ In reply to ]
Bjoern Giesler writes:

> I'm going crazy. Why does this fragment work (adapted from imageview.py
> in Guido's Tkinter demos):
> [snip]
> .... but this doesn't:
> [snip]
>
> def makeToolbar(aFrame):
> toolbar = Frame(aFrame)
> markimage = BitmapImage(file = "Mark.xbm")
> markbutton = Button(toolbar,
> image = markimage,
> command = exit)
>
> markbutton.pack()
> return toolbar

The "markimage" object goes out of scope when makeToolbar exits, and
storage for the image automatically gets cleared up. You need to
cause there to be a persistent reference to the image object.
Fortunately this is pretty easy to do. Try this:

def makeToolbar(aFrame):
toolbar = Frame(aFrame)
toolbar.markimage = BitmapImage(file = "Mark.xbm")
markbutton = Button(toolbar,
image = toolbar.markimage,
command = exit)

markbutton.pack()
return toolbar