Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改功能中Tkinter按钮的颜色_Python_Python 2.7_Tkinter - Fatal编程技术网

Python 更改功能中Tkinter按钮的颜色

Python 更改功能中Tkinter按钮的颜色,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,当按下不同的按钮时,我想改变按钮的颜色。下面的代码重新创建属性错误 理想情况下,解决方案应该能够更改按钮的所有属性(请参阅尝试的状态更改),但我没有将其放在标题中,因为我不知道“attributes”是否是正确的词 import Tkinter def tester(): class window(Tkinter.Tk): def __init__(self,parent): Tkinter.Tk.__init__(self,parent)

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

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

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()
此处建议的所有方法都会产生相同的错误:

谢谢

  • 您需要在声明时给出
    self.button1
  • 若你们看到网格,你们给按钮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()
    
  • 您需要在声明时给出
    self.button1
  • 若你们看到网格,你们给按钮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()
    
  • 您需要在声明时给出
    self.button1
  • 若你们看到网格,你们给按钮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()
    
  • 您需要在声明时给出
    self.button1
  • 若你们看到网格,你们给按钮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()
    

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

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

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

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

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

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

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

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

    谢谢为什么这样做有效?我不太理解类和self,所以如果添加self.variable,请记住这一点。如果不提供self.variable,它将通过类的方法访问。它认为该方法只是局部变量:“声明时给self.button一个”。“一”是指什么——一种颜色?谢谢。为什么这样做有效?我不太理解类和self,所以如果添加self.variable,请记住这一点。如果不提供self.variable,它将通过类的方法访问。它认为该方法只是局部变量:“声明时给self.button一个”。“一”是指什么——一种颜色?谢谢。为什么这样做有效?我不太理解类和self,所以如果添加self.variable,请记住这一点。如果不提供self.variable,它将通过类的方法访问。它认为该方法只是局部变量:“声明时给self.button一个”。“一”是指什么——一种颜色?谢谢。为什么这样做有效?我不太理解类和self,所以如果添加self.variable,请记住这一点。如果不提供self.variable,它将通过类的方法访问。它认为该方法只是局部变量:“声明时给self.button一个”。“一”是指什么——一种颜色?