【问题标题】:Changing the colour of a Tkinter button in a function在函数中更改 Tkinter 按钮的颜色
【发布时间】:2014-09-12 11:29:54
【问题描述】:

我想在按下不同按钮时更改按钮的颜色。下面的代码重新创建了属性错误。

理想情况下,解决方案应该能够更改按钮的所有属性(请参阅尝试的状态更改),但我没有将其放在标题中,因为我不知道“属性”是否是正确的词。

import Tkinter

def tester():

    class window(Tkinter.Tk):
        def __init__(self,parent):
            Tkinter.Tk.__init__(self,parent)
            self.parent = parent
            self.initialize()

        def initialize(self):
            self.grid()
            button1 = Tkinter.Button(self,text=u"Button")
            button1.grid(padx=5,pady=5)

            button2 = Tkinter.Button(self,text=u"Change",command=self.colourer)
            button2.grid(column=1,row=0,pady=5)  

            button3 = Tkinter.Button(self,text=u"Disabled",state='disabled')
            button3.grid(column=1,row=0,pady=5)                     

        def colourer(self):
            self.button1.configure(bg='red')
#           self.button1.config(bg='red')  -- this gives same error
#           self.button3.configure(state='normal')  -- as does this
    if __name__ == "__main__":
        app = window(None)
        app.title('Tester')
        app.mainloop()

tester()

这里建议的所有方法都会给出相同的错误:Changing colour of buttons in tkinter

谢谢

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    问题的根源在于您没有定义self.button。您需要为该变量赋值:

    self.button = Tkinter.Button(...)
    

    【讨论】:

      【解决方案2】:
      1. 你需要给self.button1declaring
      2. 如果您看到网格,您为按钮 2 和按钮 3 提供了相同的列名称,因此它们相互重叠

      试试这个

      import Tkinter
      
      def tester():
      
          class window(Tkinter.Tk):
              def __init__(self,parent):
                  Tkinter.Tk.__init__(self,parent)
                  self.parent = parent
                  self.initialize()
      
              def initialize(self):
                  print self.grid()
                  self.button1 = Tkinter.Button(self,text=u"Button")
                  self.button1.grid(padx=5,pady=5)
      
                  self.button2 = Tkinter.Button(self,text=u"Change",command=self.colourer)
                  self.button2.grid(column=1,row=0,pady=5)
      
                  self.button3 = Tkinter.Button(self,text=u"Disabled",state='disabled')
                  self.button3.grid(column=2,row=0,pady=5)
      
      
      
              def colourer(self):
                  self.button1.configure(bg='red')
      #           self.button1.config(bg='red')  -- this gives same error
      #           self.button3.configure(state='normal')  -- as does this
          if __name__ == "__main__":
              app = window(None)
              app.title('Tester')
              app.mainloop()
      
      tester()
      

      【讨论】:

      • 谢谢。为什么这行得通?我不太了解类和自我。
      • @ElConfuso 只是记住这一点,如果你添加 self .variable 如果你不给它,它将通过类的方法访问。它认为只是该方法的局部变量
      • re:“你不需要在声明时给 self.button 一个”。 “一个”指的是什么——一种颜色?
      猜你喜欢
      • 2014-02-22
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      相关资源
      最近更新 更多