Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单击按钮更改图像_Python_Python 2.7_Tkinter - Fatal编程技术网

Python 单击按钮更改图像

Python 单击按钮更改图像,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,这是我的第一个python编程代码。我正试图改变一个按钮点击图像。我有两个按钮,开始对话和停止对话 加载表单时,没有图像。单击开始按钮时,将显示ABC图像。单击停止按钮时,应显示xyz图像 我面临的问题是,当我单击“开始”时,会出现相应的图像,但当我单击“停止”时,会出现新图像,但前一个图像不会消失。这两个图像一个接一个地显示 我的代码在下面 root = Tk() prompt = StringVar() root.title("AVATAR") label = Label(root, fg=

这是我的第一个python编程代码。我正试图改变一个按钮点击图像。我有两个按钮,开始对话和停止对话

加载表单时,没有图像。单击开始按钮时,将显示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(图像)
画布=画布(高度=200,宽度=200)

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

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
,请将Tkinter导入中的
替换为Tkinter导入中的
*

Hi user6829070,您注意到答案了吗?请提一下,非常感谢。在放置canvas.delete(“全部”)之后,它现在工作得非常好。再次感谢:)当然,雅各布。我接受了上面提到的步骤,再次感谢
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()