2016-08-31 26 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への参照の数がゼロになり、オブジェクトがガベージコレクションに利用可能になります。

イメージの参照をどこかに置くと、そのイメージが表示されます。あなたがそれをグローバルに保管していたとき、それはスクリプト全体の範囲にとどまり、したがって現れました。

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() 
関連する問題