【问题标题】:Tkinter in Python.. Tic Tac Toe Game.. Error With the Button!Python中的Tkinter ..井字游戏..按钮错误!
【发布时间】:2011-01-09 16:06:46
【问题描述】:

我是 tkinter 的新手,所以我是初学者.. 我需要帮助,因为我正在尝试对井字游戏进行编程.. 我想以心形按钮作为区域开始游戏,当玩家点击该图像时X 或 O 的变化(另一个 gif 图像)... 所以我需要一个函数来帮助我切换按钮中的图像但是当我尝试这样做时它给了我这个错误: AttributeError: 'Button' object has no attribute 'image' 这是给我问题的代码部分..

class Application(object):

    def __init__(self,fnt2):
        self.photo = PhotoImage(file="game.gif")
        self.lab1 = Label(fnt2, text="WELCOME to the GAME")
        self.lab1.image = self.photo
        self.lab1['background']="#BD5151"
        self.lab1['foreground']="#651268"
        self.lab1.image = self.photo
        self.lab1.pack()

        self.lab2= Label(image=self.photo)
        self.lab2.image= self.photo
        self.lab2['background']="#BD5151"
        self.lab2.pack()
        self.imm0 = PhotoImage(file="start.gif")
        self.imm1 = PhotoImage(file="bianco.gif")
        self.imm2 = PhotoImage(file="ics.gif")


        self.Ent = Button(fnt2, text="Click To Enter The GAME")
        self.Ent['relief']="groove"
        self.Ent['command']=self.Ent_Click
        self.Ent.pack()

    def Changepic_1(self):
        imm0 = PhotoImage(file="start.gif")
        imm1 = PhotoImage(file="bianco.gif")
        imm2 = PhotoImage(file="ics.gif")
        if self.Play.image == self.imm0:
            print('ciao')


    def Ent_Click(self):
        fnt2 = Tk()
        fnt2.title("play it!")
        fnt2.resizable(False,False)
        for r in range(3):
            for c in range(3):
                self.Play = Button(image = self.imm0, command=self.Changepic_1)
                self.Play.grid(row=r,column=c)
        fnt2.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    尝试更改它:

    button.config(image=...)
    

    要找出它已经拥有的图像,您需要比较:

    button.cget("image") == image.name
    

    (或单独跟踪其状态)

    Tk 不是面向对象的,所以尽管 Tkinter 试图让界面更“Pythonic”,但它可能有点尴尬。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2013-07-15
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多