0

PhotoImage: Tkinter PhotoImage objects and their idiosyncracies

http://tkinter.unpythonic.net/wiki/PhotoImage

I tested the example with python 2.7.9, 3.2.5, 3.3.5, 3.4.3 in 32bit and 64bit. (Win 8.1 64bit)

The code works. ( even without pillow )

( in python 3.4.3 64bit I had first an error message.

I've completely uninstalled 3.4.3 and then reinstalled.

Now, the example works also with 3.4.3 64 bit )

# basic code from >>
# http://tkinter.unpythonic.net/wiki/PhotoImage

# extra code -------------------------------------------------------------------------
from __future__ import print_function

try:
    import tkinter as tk
except:
    import Tkinter as tk

import sys
import platform

print ()
print ('python    ', sys.version)
print ('tkinter   ', tk.TkVersion)
print ()
print (platform.platform(),' ',platform.machine())
print ()


# basic code -------------------------------------------------------------------------

root = tk.Tk()

def create_button_with_scoped_image():
    # "w6.gif" >>
    # http://www.inf-schule.de/content/software/gui/entwicklung_tkinter/bilder/w6.gif
    img = tk.PhotoImage(file="w6.gif")  # reference PhotoImage in local variable
    button = tk.Button(root, image=img)
    # button.img = img  # store a reference to the image as an attribute of the widget
    button.image = img  # store a reference to the image as an attribute of the widget
    button.grid()

create_button_with_scoped_image()

tk.mainloop()
frank.p
  • 11
  • 1
  • 3
  • I have edit my post: The code works. ( even without pillow ) ( in python 3.4.3 64bit I had first an error message. I've completely uninstalled 3.4.3 and then reinstalled. Now, the example works also with 3.4.3 64 bit ) – frank.p May 30 '15 at 10:00

3 Answers3

3

This is done by replacing the root.Tk() by root.Toplevel()

Zoe
  • 27,060
  • 21
  • 118
  • 148
himanshu
  • 35
  • 9
1

Your script works fine for me with exactly the same version of Python 3.4 running on windows 7. I suspect the only difference is that I have also installed Pillow by downloading the wheel package from this repository. I think this includes the image support you need.

As a side note: on windows use the ttk package widgets to get buttons that actually look correct for the platform. Just import tkinter.ttk as ttk then use ttk.Button instead of tk.Button.

Update

Given the same code is working on my machine and not yours I thought I would add how I obtained this version. I installed python using chocolatey (choco install python) then added Pillow, lxml, numpy, requests and simplejson from the gohlke site.

Checking the Tk version I see that I get Tk 8.6.1 so I suspect you are picking up a Tcl/Tk installation installed locally and not shipped with your version of Python. Try ensuring no Tk installation is in your PATH and see if that resolves the problem. My output was:

python     3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
tkinter    8.6

Test Tk location:

>>> from tkinter import *
>>> root = Tk()
>>> root.eval('set tk_library')
'C:\\opt\\Python34\\tcl\\tk8.6'

I actually persuaded python to install to c:\opt\Python I think by using choco install python -ia "TARGETDIR=c:\opt\Python" but I doubt that is relevant.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • @ patthoyts Thanks for the quick reply. It's not my code, it is the code of the Tkinter Wiki. I have also installed Pillow (in 2.7.9 and in 3.4.3.) Did you just change the code sample? – frank.p May 29 '15 at 22:12
  • @ patthoyts: For the PhotoImage problem, there is no difference between tk.Button and ttk.Button (2.7.9 works an 3.4.3 works not) – frank.p May 29 '15 at 22:40
  • I did not change your code at all. I even created a small gif with the same name. The comment about using ttk is about the appearence of the button and other controls -- not related to the image handling. Tk buttons look like WIndows 95 while ttk buttons look like they belong. – patthoyts May 30 '15 at 08:35
  • I have edit my post: The code works. ( even without pillow ) ( in python 3.4.3 64bit I had first an error message. I've completely uninstalled 3.4.3 and then reinstalled. Now, the example works also with 3.4.3 64 bit ) – frank.p May 30 '15 at 09:59
0

You must close all open windows, which hang in the memory. If you have an error and did not destroy the window the next time you start not started there several windows. Check!