I am trying to create a button in Python (using Tkinter), that changes color every time I click it. I have this so far:
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
#initialize frame
self.create_widgets()
self.bttn_clicks = 0
#initiates button clicks
def create_widgets(self):
self.bttn1 = Button(self)
self.bttn1["text"] = "Click Me!"
self.bttn1["command"] = self.update_count
self.bttn1.grid()
#creates button
def update_count(self):
if self.bttn_clicks + 1:
self.bttn1.configure(background = "blue")
if self.bttn_clicks + 2:
self.bttn1.configure(background = "green")
if self.bttn_clicks + 3:
self.bttn1.configure(background = "orange")
if self.bttn_clicks + 4:
self.bttn1.configure(background = "red")
if self.bttn_clicks + 5:
self.bttn1.configure(background = "yellow")
#changes colors from blue, to green, orange, red, then yellow
root = Tk()
root.title("Color Button")
root.geometry("200x85")
app = Application(root)
root.mainloop()
If anyone could help, I would greatly appreciate it. Also, I am new to programming and Python, and I just started using Tkinter 3 days ago - so keep that in mind.