【发布时间】:2017-01-21 18:40:00
【问题描述】:
这是我的第一个 python 编程代码。我正在尝试通过单击按钮更改图像。我有 2 个按钮,Start Conversation 和 Stop Conversation。
当表单加载时没有图像。当开始按钮被点击时,将显示ABC图像。单击停止按钮时,应显示 xyz 图像。
我面临的问题是,当我单击开始时,会出现相应的图像,但是当我单击停止时,会出现新图像但之前的图像并没有消失。两个图像一个接一个地显示
我的代码在下面
root = Tk()
prompt = StringVar()
root.title("AVATAR")
label = Label(root, fg="dark green")
label.pack()
frame = Frame(root,background='red')
frame.pack()
函数定义
def Image1():
image = Image.open("C:\Python27\Agent.gif")
photo = ImageTk.PhotoImage(image)
canvas = Canvas(height=200,width=200)
canvas.image = photo # <--- keep reference of your image
canvas.create_image(0,0,anchor='nw',image=photo)
canvas.pack()
def Image2():
image = Image.open("C:\Python27\Hydrangeas.gif")
photo = ImageTk.PhotoImage(image)
canvas = Canvas(height=200,width=200)
canvas.image = photo # <--- keep reference of your image
canvas.create_image(0,0,anchor='nw',image=photo)
canvas.pack()
#Invoking through button
TextWindow = Label(frame,anchor = tk.NW, justify = tk.LEFT, bg= 'white', fg = 'blue', textvariable = prompt, width = 75, height=20)
TextWindow.pack(side = TOP)
conversationbutton = Button(frame, text='Start Conversation',width=25,fg="green",command = Image1)
conversationbutton.pack(side = RIGHT)
stopbutton = Button(frame, text='Stop',width=25,fg="red",command = Image2)
stopbutton.pack(side = RIGHT)
root.mainloop()
【问题讨论】:
-
您好 user6829070,您注意到答案了吗?请提及。
标签: python python-2.7 tkinter