Necesito ayuda sobre este progtwig, este progtwig debería abrir la imagen en la nueva ventana tkinter haciendo clic en el botón, pero no abre la ventana nueva sin la imagen. ¿Dónde está el problema?
Utilizando: python 3.3 y tkinter
Este es el progtwig:
import sys from tkinter import * def button1(): novi = Toplevel() canvas = Canvas(novi, width = 300, height = 200) canvas.pack(expand = YES, fill = BOTH) gif1 = PhotoImage(file = 'image.gif') canvas.create_image(50, 10, visual = gif1, anchor = NW) mGui = Tk() button1 = Button(mGui,text ='Sklop',command = button1, height=5, width=20).pack() mGui.mainloop()
create_image
necesita un argumento de image
, no visual
para usar la imagen, así que en lugar de visual = gif1
, necesitas image = gif1
. El siguiente problema es que necesita almacenar la referencia gif1
algún lugar o de lo contrario se recolectará la basura y tkinter no podrá usarla más.
Así que algo como esto:
import sys from tkinter import * #or Tkinter if you're on Python2.7 def button1(): novi = Toplevel() canvas = Canvas(novi, width = 300, height = 200) canvas.pack(expand = YES, fill = BOTH) gif1 = PhotoImage(file = 'image.gif') #image not visual canvas.create_image(50, 10, image = gif1, anchor = NW) #assigned the gif1 to the canvas object canvas.gif1 = gif1 mGui = Tk() button1 = Button(mGui,text ='Sklop',command = button1, height=5, width=20).pack() mGui.mainloop()
Probablemente tampoco sea una buena idea nombrar a su Button
el mismo nombre que la función button1
, que solo causará confusión más adelante.
from tkinter import * root = Tk() root.title("Creater window") def Img(): r = Toplevel() r.title("My image") canvas = Canvas(r, height=600, width=600) canvas.pack() my_image = PhotoImage(file='C:\\Python34\\Lib\idlelib\\Icons\\Baba.gif', master= root) canvas.create_image(0, 0, anchor=NW, image=my_image) r.mainloop() btn = Button(root, text = "Click Here to see creator Image", command = Img) btn.grid(row = 0, column = 0) root.mainloop()