【问题标题】:Changing image on button click单击按钮更改图像
【发布时间】:2017-01-21 18:40:00
【问题描述】:

这是我的第一个 python 编程代码。我正在尝试通过单击按钮更改图像。我有 2 个按钮,Start ConversationStop 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


【解决方案1】:

最重要的问题是在创建新图像之前没有清除画布。使用以下命令启动您的(按钮)功能:

canvas.delete("all")

但是,您的画布也是如此;你继续创造它。更好地拆分画布的创建和设置图像:

from Tkinter import *

root = Tk()
prompt = StringVar()
root.title("AVATAR")
label = Label(root, fg="dark green")
label.pack()

frame = Frame(root,background='red')
frame.pack()

# Function definition

# first create the canvas
canvas = Canvas(height=200,width=200)
canvas.pack()

def Image1():
    canvas.delete("all")
    image1 = PhotoImage(file = "C:\Python27\Agent.gif")
    canvas.create_image(0,0,anchor='nw',image=image1)
    canvas.image = image1

def Image2():
    canvas.delete("all")
    image1 = PhotoImage(file = "C:\Python27\Hydrangeas.gif")
    canvas.create_image(0,0,anchor='nw',image=image1)
    canvas.image = image1

#Invoking through button
TextWindow = Label(frame,anchor = NW, justify = 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()

这也防止了按钮按下时窗口有些奇怪的扩展。 要使代码适合python3,请将from Tkinter import* 替换为from tkinter import*

【讨论】:

  • 非常感谢。放置 canvas.delete("all") 后,它现在工作得很好。再次感谢:)
  • 当然,雅各布。我按照上述步骤接受了,再次感谢
猜你喜欢
  • 2015-05-09
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 2012-07-07
  • 2014-04-17
  • 2014-01-04
相关资源
最近更新 更多