libreria instalar examples image button python-3.x tkinter ttk

image - instalar - tkinter python 3 pdf



¿Cómo puedo mostrar una imagen en Python 3 usando tkinter/ttk? (2)

El error parece apuntar al argumento myButton pasado a PhotoImage . Como señaló en su comentario, PhotoImage estaba tratando el objeto widget como una cadena (hay varias opciones de tipo cadena, vea una lista de opciones de PhotoImage aquí ).

Su código funcionará si implementa esa línea sin hacer referencia al objeto myButton :

myImage = PhotoImage(file=''myPicture.gif'')

No estoy seguro de que necesite modificar el constructor PhotoImage . Mire los documentos de PhotoImage para determinar las opciones válidas (es decir, nombres de recursos) para esa clase. Citando el archivo de ayuda:

Ayuda en la clase PhotoImage en el módulo tkinter:

clase FotoImagen (Imagen)

| Widget which can display colored images in GIF, PPM/PGM format. | | Method resolution order: | PhotoImage | Image | builtins.object | | Methods defined here: | | __getitem__(self, key) | # XXX config | | __init__(self, name=None, cnf={}, master=None, **kw) | Create an image with NAME. | | Valid resource names: data, format, file, gamma, height, palette, | width.

FYI: la forma más fácil de acceder a los documentos desde Python desde la línea de comando o desde IDLE:

from tkinter import PhotoImage help(PhotoImage)

Y, por último, otro enlace útil sobre esta clase es en http://tkinter.unpythonic.net/wiki/PhotoImage .

El quid de la cuestión es, ¿qué estoy haciendo mal en el siguiente fragmento de código?

from tkinter import * from tkinter.ttk import * root = Tk() myButton = Button(root) myImage = PhotoImage(myButton, file=''myPicture.gif'') myButton.image = myImage myButton.configure(image=myImage) root.mainloop()

El mensaje de error que recibo de idle3 es el siguiente:

>>> Traceback (most recent call last): File "/home/bob/Documents/Python/tkImageTest.py", line 9, in <module> myButton.configure(image=myImage) File "/usr/lib/python3.2/tkinter/__init__.py", line 1196, in configure return self._configure(''configure'', cnf, kw) File "/usr/lib/python3.2/tkinter/__init__.py", line 1187, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) TypeError: __str__ returned non-string (type Button) >>>

Este mensaje de error me tiene perplejo, simplemente no entiendo lo que está tratando de decir. ¿Algunas ideas?

También agradecería sugerencias para cambios ...


Probé el ejemplo con Python 2.7.9, 3.2.5, 3.3.5, 3.4.3 en 32 bits y 64 bits. (Win 8.1 64bit)

El código funciona

(En python 3.4.3 64 bits, primero tuve un mensaje de error.

Desinstalé por completo 3.4.3 y luego reinstalé.

Ahora, el ejemplo también funciona con 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()