繁体   English   中英

Tkinter - 单击按钮时如何显示图像?

[英]Tkinter - How to display image when clicking a button?

第一次来这里,请原谅我,因为这是我第一次尝试制作一个愚蠢的 GUI 游戏(如果你想这样称呼它)。 我试图让用户单击一个按钮,然后弹出他们选择的图像。 我似乎无法弄清楚如何让图像弹出。

如果我单独运行它,图像会显示。

我的代码:

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()

这有效,但实际图像不再显示,如果我将 PhotoImage 保留在类中,它将打印但我希望在他们单击特定按钮时打印它:

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()

您需要维护对PhotoImage对象的引用。 不幸的是,tkinter 的不一致之处在于将Button附加到父小部件会增加引用计数,但将图像添加到小部件不会增加引用计数。 因此,当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()

对我有帮助的解决方案只是在“root = Tk()”之后的下一行声明所有图像变量。 这样做不会破坏您的代码或任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM