2016-09-14 9 views
2

이것은 내 첫번째 파이썬 프로그래밍 코드입니다. 클릭 한 번으로 이미지를 변경하려고합니다. 두 개의 버튼이 있습니다 대화 시작 & 대화 중지.버튼 클릭시 이미지 변경

양식로드시 이미지가 없습니다. 시작 버튼을 클릭하면 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() 
+0

으로 바꿉니다. 사용자 6829070, 대답을 확인 했습니까? 언급하십시오. –

답변

2

. (버튼) 기능을 다음과 같이 시작하십시오 :

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*

+0

정말 고마워요. canvas.delete ("all")를 쓴 후에는 이제 완벽하게 작동합니다. 다시 한번 감사드립니다 :) – user6829070

+0

Sure Jacob. 나는 위에서 언급 한 단계를 따라 그것을 받아 들였습니다. 다시 한번 감사드립니다. – user6829070