Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Tkinter button

Status
Not open for further replies.

lw22

Programmer
Feb 19, 2005
7
0
0
US
alright well i am not good at writing programs at all. actually im really bad at it but im trying. alright right now im tring to figure out how to change the color of a button when you click on it. for example if i have a button that says "a" and its blue, when you click on it, it will change to red.

this is what i have written so far.

from Tkinter import *
c1 = "blue"
class App:
def __init__(self, master, c1):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.there = Button(frame, text="Hello", fg=c1, command=self.clr)
self.there.pack(side=LEFT)
def clr(self):
c1 = "red"
root = Tk()
app = App(root)
root.mainloop()


the only problem with that is c1 is not in parameters for __init__. i guess there can only be 2 parameters for __init__ and using the class app is the only way i learned how to make a frame. so any help?
 
This is not the greatest programming, but its a solution to your problem:
from Tkinter import *
c1 = "blue"
class App:
def __init__(self, master, c1):
self.frame = Frame(master)
self.frame.pack()
self.button = Button(self.frame, text="QUIT", fg="red", command=self.frame.quit)
self.button.pack(side=LEFT)
self.there = Button(self.frame, text="Hello", fg=c1, command=self.clr)
self.there.pack(side=LEFT)
def clr(self):
self.there.destroy()
btn = Button(self.frame, text="Hello", fg="red") #, command=self.clr)
btn.pack(side=LEFT)
root = Tk()
app = App(root, c1)
root.mainloop()
 
so basically the only way to change the button color is to destroy it and make a new one?
 
Probably not, but this took me 2 seconds to implement and it works well.
 
To re-iterate my comments on a previous post: figure out how to do it in TK, then port it to Python. Python is simply a wrapper around the TK calls.

Don't restrict yourself to web searches that mention python, find TK sites that tell you how to do what you need, then if you have problems getting it to work in Python, come back here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top