i have my code and i want to cycle through the following colors blue green orange red yellow. i got blue but i dont know how to the rest

from Tkinter import *

myfont = ("Arial", 1, "bold")

window = Tk()
window.title("grid")
window.geometry("500x420")

frame = Frame(window)
frame.grid()


button0 = Button(frame, font = ("Arial", 14, "bold"), 
                 fg = "blue", bg = "blue", width = 4, height = 2)
button0.grid(row = 1, column = 0, padx = 0, pady = 0 )

window.mainloop()

Recommended Answers

All 8 Replies

Make button event which changes the color on button click.

Make button event which changes the color on button click.

i add this to the code, but it wont let me run it in GUI i dont if its in the wrong spot could you take a look.

from Tkinter import *



window = Tk()
window.title("grid")
window.geometry("500x420")
backgrounds = ("blue": "#0000FF", "green": "#00FF00", "orange": "#FF6600", "red": "#FF0000", "yellow": "#FFFF00")
frame = Frame(window)
frame.grid()


def getNextColor(self, pColor):
    sendNext = false
    foreach key in background.keys():
      if sendNext:
         return background[key]
         break
      if backgrounds[key] == pColor
         sendNext = true



button0 = Button(frame, 
                 fg = "blue", bg = "blue", width = 4, height = 2)
button0.grid(row = 1, column = 0, padx = 0, pady = 0 )





window.mainloop()

You have not registered events. Why self parameter in function when you do not have class definitions? And where you call the function?

You have not registered events. Why self parameter in function when you do not have class definitions?

is there some where i can go to read on the button event?

It depends somewhat on what widget you want to set, but generally you can use configure. This is something I have lying around from somewhere and added a configure to change the background and foreground of the button when the top button is pressed. Another example is here. You also want to become familiar with the class structure as it is much easier to pass variables around when coding a GUI. The example at the link posted shows how to use a class for this.

from Tkinter import *

def callback1() :
    print "Callback #1"
    option1.configure(bg = 'white', fg='green')

def callback2() :
    print "Callback #2"

def callback3() :
    print "Callback #3"

##===============================================================
top = Tk()

##---  always place in upper left corner ( +10+10 )
##     size = 200x200 = minimum size so it's big enough to be seen
top.geometry("150x150+10+10" )

label1 = Label( top, text = "Test Menu" )
label1.pack()

label2 = Label( top, text = "" )          ## blank line = spacer
label2.pack()

option1 = Button(top, text='Option 1',
       command=callback1, bg='blue', fg='white' )
option1.pack(fill=X, expand=1)

option2 = Button(top, text='Option 2',
       command=callback2, bg='green', fg='white' )
option2.pack(fill=X, expand=1)

option3 = Button(top, text='Option 3 - No Exit',
       command=callback3, bg='black', fg='white' )
option3.pack(fill=X, expand=1)

exit=  Button(top, text='EXIT',
       command=top.quit, bg='red', fg='white' )
exit.pack(fill=X, expand=1)

top.mainloop()

i just want to make the color of the button change when i click the button from blue green red yellow orange i can get two colors but nothing after that

There are several ways to achieve this.
You can define enumration of colors. These color got id which are int. eg.

RED=200
BLUE=300
YELLOW=400
PINK=500

Then import random to rand the range from 200-500 by 100 (100,500,100)
Then provide the result in the bind method to set the color of the button.

2. You can also have a counter to count when ever the button is click and use the counter variable to help know which color come next.

You got the idea ? ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.