Change control background : Button « Tkinker « Python Tutorial






Change control background
from Tkinter import *

class MyApp:
  def __init__(self, parent):
    self.myLastButtonInvoked = None    
    
    self.myParent = parent   
    self.myContainer1 = Frame(parent)
    self.myContainer1.pack()
    
    self.yellowButton = Button(self.myContainer1, command=self.yellowButtonClick)   
    self.yellowButton.configure(text="YELLOW", background="yellow")     
    self.yellowButton.pack(side=LEFT)

    self.redButton = Button(self.myContainer1, command=self.redButtonClick)  
    self.redButton.configure(text="RED", background= "red")
    self.redButton.pack(side=LEFT)  
    
    self.whiteButton = Button(self.myContainer1, command=self.whiteButtonClick)   
    self.whiteButton.configure(text="WHITE", background="white")     
    self.whiteButton.pack(side=LEFT)
    
  def redButtonClick(self):   
    print "RED    button clicked.  Previous button invoked was", self.myLastButtonInvoked  ### 2
    self.myLastButtonInvoked = "RED"
    
  def yellowButtonClick(self):  
    print "YELLOW button clicked.  Previous button invoked was", self.myLastButtonInvoked ### 2
    self.myLastButtonInvoked = "YELLOW" 
        
  def whiteButtonClick(self):  
    print "WHITE  button clicked.  Previous button invoked was", self.myLastButtonInvoked ### 2
    self.myLastButtonInvoked = "WHITE" 
       
    
root = Tk()
myapp = MyApp(root)
root.mainloop()








18.4.Button
18.4.1.Button Widget DemoButton Widget Demo
18.4.2.Label and Button Widget DemoLabel and Button Widget Demo
18.4.3.Button text and commandButton text and command
18.4.4.Add button to frameAdd button to frame
18.4.5.Config a button for its cursor background, font and borderConfig a button for its cursor background, font and border
18.4.6.Button action: sys.exitButton action: sys.exit
18.4.7.Set button properties in its constructorSet button properties in its constructor
18.4.8.Set button backgroundSet button background
18.4.9.Set text for button with configure functionSet text for button with configure function
18.4.10.Set button properties using list syntax
18.4.11.Bind mouse action to buttonBind mouse action to button
18.4.12.Change control backgroundChange control background
18.4.13.Mouse in/out action
18.4.14.Lazy Buttons: Demonstrates using a class with TkinterLazy Buttons: Demonstrates using a class with Tkinter
18.4.15.An Application with a ButtonAn Application with a Button
18.4.16.To add button handling functionality to your applicationTo add button handling functionality to your application
18.4.17.Button graphicsButton graphics