+ 3
Please help me with my program(tkinter)
I wrote a code....It is not working. Please Help!!! I am using python 3.6 on my computer. from tkinter import * root = Tk() root.geometry("250x150") root.title("Image") def image1 (event): photo = PhotoImage(file="dice1.gif") label = Label(root, image = photo) label.pack() root.bind("<Return>", image1) root.mainloop()
4 ответов
+ 1
# It's not clear what you are trying to achieve?
# If it just to experiment with user event response and
# making images come and go there is command
# available on button widgets you can link to a method
import tkinter as tk
class MyApp(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
ROOT.title("Image")
ROOT.geometry('250x150')
self.create_widgets()
def create_widgets(self):
self.photo = tk.PhotoImage(master=self,file="dice1.gif")
self.button = tk.Button(master=self,
text="press to see image",
command=self.show_image)
self.button.grid(row=0,column=0)
self.photo_label = tk.Label(master=self,
image=self.photo)
def show_image(self):
self.photo_label.grid(row=1,column=0)
if __name__ == '__main__':
ROOT = tk.Tk()
APP = MyApp(master=ROOT)
APP.mainloop()
+ 3
Oh i am sorry. I 've got 2 (or more) images and I want to change them by clicking on a button or an image...
+ 3
thanks
+ 1
# there are many ways to exchange images
# here is just one to experiment with using
# .cget(property) and .configure(property)
# to detect and then change a widgets
# image attribute.
# .cget() returns a string referencing the specific image
# so to find out the string just print it out for the property
# you are interested in
# print(self.photo_label.cget('image'))
# that is if you need to develop any conditional logic
# as you experiment
import tkinter as tk
class MyApp(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
ROOT.title("Image")
ROOT.geometry('250x150')
self.create_widgets()
def create_widgets(self):
self.photo = tk.PhotoImage(master=self,file="dice1.gif")
self.photo2 = tk.PhotoImage(master=self,file="glit2.gif")
self.photo3 = tk.PhotoImage(master=self,file="glit3.gif")
self.button = tk.Button(master=self,
text="press to see image",
command=self.show_image)
self.button.grid(row=0,column=0)
self.photo_label = tk.Label(master=self,
image=self.photo)
self.photo_label.grid(row=1,column=0)
def show_image(self):
if self.photo_label.cget('image')=='pyimage1':
self.photo_label.configure(image=self.photo2)
elif self.photo_label.cget('image')=='pyimage2':
self.photo_label.configure(image=self.photo3)
else:
self.photo_label.configure(image=self.photo)
if __name__ == '__main__':
ROOT = tk.Tk()
APP = MyApp(master=ROOT)
APP.mainloop()