2016-08-31 2 views
0

첫 번째 시간을 용서해주세요. 내 GUI를 바보로 만들려고 시도한 첫 번째 시도입니다. 나는 사용자가 버튼을 클릭하도록하고 그들의 선택의 이미지가 나타납니다. 나는 이미지를 팝업하는 방법을 알아낼 수 없다.Tkinter - 버튼을 클릭 할 때 이미지를 표시하는 방법은 무엇입니까?

별도로 실행하면 이미지가 표시됩니다.

내 코드 :

from Tkinter import * 

root = Tk() 

class PokemonClass(object): 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 

     self.WelcomeLabel = Label(root, text="Welcome! Pick your Pokemon!", 
            bg="Black", fg="White") 
     self.WelcomeLabel.pack(fill=X) 

     self.CharButton = Button(root, text="Charmander", bg="RED", fg="White", 
           command=self.CharClick) 
     self.CharButton.pack(side=LEFT, fill=X) 

     self.SquirtButton = Button(root, text="Squirtle", bg="Blue", fg="White") 
     self.SquirtButton.pack(side=LEFT, fill=X) 

     self.BulbButton = Button(root, text="Bulbasaur", bg="Dark Green", 
           fg="White") 
     self.BulbButton.pack(side=LEFT, fill=X) 

    def CharClick(self): 
     print "You like Charmander!" 
     global CharSwitch 
     CharSwitch = 'Yes' 

CharSwitch = 'No' 

if CharSwitch == 'Yes': 
    CharPhoto = PhotoImage(file="Charmander.gif") 
    ChLabel = Label(root, image=CharPhoto) 
    ChLabel.pack() 

k = PokemonClass(root) 
root.mainloop() 
+0

이 올바른 들여 쓰기인가의 이상인가? 이미지가 곧바로 실행되도록 설정하는 부분을 의도하지 않았다고 생각합니다. 값을 'no'로 설정하면 절대로 실행되지 않습니다. 단추 콜백 내에서 해당 코드를 이동해야합니다. –

답변

0

이 작동하지 않습니다,하지만 난 그것을 인쇄 할 클래스의 광 화상 OUT을 유지하지만, 내가 갖고 싶어 그들은 특정 버튼을 클릭하면 실제 이미지가 더 이상 쇼, 그것은 인쇄 :

from Tkinter import * 



root = Tk() 


class PokemonClass(object): 

    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 

     self.WelcomeLabel = Label(root, text = "Welcome! Pick your Pokemon!", bg = "Black", fg = "White") 
     self.WelcomeLabel.pack(fill = X) 

     self.CharButton = Button(root, text = "Charmander", bg = "RED", fg = "White", command = CharClick) 
     self.CharButton.pack(side = LEFT, fill = X) 

     self.SquirtButton = Button(root, text = "Squirtle", bg = "Blue", fg = "White") 
     self.SquirtButton.pack(side = LEFT, fill = X) 

     self.BulbButton = Button(root, text = "Bulbasaur", bg = "Dark Green", fg = "White") 
     self.BulbButton.pack(side = LEFT, fill = X) 

    def CharClick(): 
    print "You like Charmander!" 
    CharPhoto = PhotoImage(file = "Charmander.gif") 
    ChLabel = Label(root, image = CharPhoto) 
    ChLabel.pack() 


k = PokemonClass(root) 
root.mainloop() 
+0

이것은 답변이 아닌 질문에 대한 편집이어야합니다. 귀하의 문제는 http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm 또는 잠재적으로 최신 소스 인 http://tkinter.unpythonic.net/에서 설명됩니다. wiki/PhotoImage –

0

PhotoImage 개체에 대한 참조를 유지해야합니다. 불행히도 부모 컨테이너에 Button을 붙이면 참조 카운트가 증가하지만 이미지를 위젯에 추가해도 참조 카운트가 증가하지 않는다는 점에서 tkinter에는 불일치가 있습니다. 결과적으로 CharPhoto 변수가 함수 CharClick의 끝에서 범위를 벗어나면 PhotoImage에 대한 참조 수가 0으로 떨어지고 개체가 가비지 수집에 사용 가능하게됩니다.

이미지를 어딘가에 유지하면 해당 이미지가 나타납니다. 전 세계적으로 보관했을 때 전체 스크립트의 범위에 남아 있었기 때문에 등장했습니다.

PokemonClass 개체 또는 Label 위젯에서이 참조를 유지할 수 있습니다. 다음은

은 이러한 옵션

from Tkinter import * 

root = Tk() 

class PokemonClass(object): 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 

     self.WelcomeLabel = Label(root, text="Welcome! Pick your Pokemon!", 
            bg="Black", fg="White") 
     self.WelcomeLabel.pack(fill=X) 

     self.CharButton = Button(root, text="Charmander", bg="RED", fg="White", 
           command=self.CharClick) 
     self.CharButton.pack(side=LEFT, fill=X) 

     self.SquirtButton = Button(root, text="Squirtle", bg="Blue", fg="White") 
     self.SquirtButton.pack(side=LEFT, fill=X) 

     self.BulbButton = Button(root, text="Bulbasaur", bg="Dark Green", 
           fg="White") 
     self.BulbButton.pack(side=LEFT, fill=X) 

    def CharClick(self): 
     print "You like Charmander!" 
     global CharSwitch 
     CharSwitch = 'Yes' 
     CharPhoto = PhotoImage(file="Charmander.gif") 
     ChLabel = Label(root, image=CharPhoto) 
     ChLabel.img = CharPhoto 
     ChLabel.pack() 

CharSwitch = 'No' 

k = PokemonClass(root) 
root.mainloop() 
관련 문제