By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Changing Colour with a Button

Sushi
P: 19
Hello!
I'm new to the Python language(about 2 months). Having done C programming for 2 years I find out I need Python for a final project, and have been trying to do get my head around object oriented programming. At the moment I'm simply trying to get the colour of a circle to change when you press a button. I've got this so far:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Op:
  4.  
  5.     def __init__(self, master):
  6.  
  7.         frame = Frame(master)
  8.         frame.pack()
  9.  
  10.         self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
  11.         self.button.pack(side=RIGHT)
  12.  
  13.         self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
  14.         self.button2.pack(side=LEFT)
  15.  
  16.     def circle(self):
  17.  
  18.         canvas = Canvas(width=210, height=210)  
  19.         canvas.pack()                
  20.  
  21.         self.button4=Button(canvas, text="colour blue", command=self.colour)
  22.         self.button4.pack()
  23.  
  24.         canvas = Canvas(width=210, height=210, bg='black')  
  25.         canvas.pack(expand=YES, fill=BOTH)                
  26.         canvas.create_oval(10, 10, 200, 200, width=2, fill='red')
  27.  
  28.     def colour(self):
  29.         canvas.create_oval(10, 10, 200, 200, width=2, fill='blue')
  30.         create_oval.pack()
  31.  
  32. root = Tk()
  33.  
  34. op = Op(root)
  35.  
  36. root.mainloop()
  37.  
I know the problem is with the defcolour(self), but don't really know how to proceed. If anyone could point me in the right direction, or a link to a tutorial that would help it would be very much appreciated!

Mel
Dec 24 '06 #1
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
Hi Mel. I've changed and anotated your submission. This is how the docs say to do it. However, it doesn't actually work on my system. It's been a long time since I have used Tkinter, so let me know if this works on your system.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Op:
  4.  
  5.     def __init__(self, master):
  6.  
  7.         frame = Frame(master)
  8.         frame.pack()
  9.  
  10.         self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
  11.         self.button.pack(side=RIGHT)
  12.  
  13.         self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
  14.         self.button2.pack(side=LEFT)
  15.  
  16.     def circle(self):
  17.  
  18.         canvas = Canvas(width=210, height=210)
  19.         canvas.pack()
  20.         # keep a reference to the canvas in order to work on it outside this function's (method's) scope
  21.         self.canvas = canvas
  22.  
  23.         self.button4=Button(canvas, text="colour blue", command=self.colour)
  24.         self.button4.pack()
  25.  
  26.         canvas = Canvas(width=210, height=210, bg='black')
  27.         canvas.pack(expand=YES, fill=BOTH)
  28.         # keep a reference to the item ID in order to work on it outside this function's (method's) scope
  29.         self.ovalID = canvas.create_oval(10, 10, 200, 200, width=2, fill='red')
  30.  
  31.     def colour(self):
  32.         print "change color of item #%d" %self.ovalID
  33.         print repr(self.canvas.itemcget(self.ovalID, "fill"))
  34.         self.canvas.itemconfigure(self.ovalID, fill="blue")
  35.  
  36.  
  37. root = Tk()
  38.  
  39. op = Op(root)
  40.  
  41. root.mainloop()
  42.  
Dec 24 '06 #2

Sushi
P: 19
Thanks for the quick reply Barton!

The code doesn't seem to work on my system I'm afraid. When I click on the 'change colour' button I just get printed:

change color of item #1
''

Also you said 'This is how it is done in the docs'. Not wanting to sound dim but what docs do you mean?
I'll have a tinker with the code to see if I can get something from it!

Mel
Dec 24 '06 #3

bartonc
Expert 5K+
P: 6,596
Thanks for the quick reply Barton!

The code doesn't seem to work on my system I'm afraid. When I click on the 'change colour' button I just get printed:

change color of item #1
''

Also you said 'This is how it is done in the docs'. Not wanting to sound dim but what docs do you mean?
I'll have a tinker with the code to see if I can get something from it!

Mel
Back in 1999, Fredrik Lundh set his students to work on Tkinter docs. It has since become a book, but the initial work is still available here.
Dec 24 '06 #4

Sushi
P: 19
I don't generally work on christmas(in fact I never), but whilst waiting for the cheesy xmas tv I had a burst of enlightenment as to why the code wasn't working. Simply, the answer was there are 2 canvases and it was the wrong canvas being told to change.

I now have 2 versions, the first where the new buttons are in the existing frame which looks messy, and the second where their on their own canvas and it looks tidier

For those who might find it helpful, here they are:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Op:
  4.  
  5.     def __init__(self, master):
  6.  
  7.         frame = Frame(master)
  8.         frame.pack()
  9.         self.frame = frame
  10.  
  11.         self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
  12.         self.button.pack(side=RIGHT)
  13.  
  14.         self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
  15.         self.button2.pack(side=LEFT)
  16.  
  17.     def circle(self):
  18.  
  19.         self.blue=Button(self.frame, text="colour blue", command=self.blue)
  20.         self.blue.pack(side=BOTTOM)
  21.  
  22.         self.red=Button(self.frame, text="colour red", command=self.red)
  23.         self.red.pack(side=BOTTOM)
  24.  
  25.         canvas = Canvas(width=210, height=210, bg='black')
  26.         canvas.pack(expand=YES, fill=BOTH)
  27.         self.canvas = canvas
  28.         # keep a reference to the item ID in order to work on it outside this function's (method's) scope
  29.         self.ovalID = canvas.create_oval(10, 10, 200, 200, width=2, fill='red')
  30.  
  31.     def blue(self):
  32.         self.canvas.itemconfigure(self.ovalID, fill="blue")
  33.  
  34.     def red(self):
  35.         self.canvas.itemconfigure(self.ovalID, fill="red")
  36.  
  37.  
  38. root = Tk()
  39.  
  40. op = Op(root)
  41.  
  42. root.mainloop()
  43.  
or
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Op:
  4.  
  5.     def __init__(self, master):
  6.  
  7.         frame = Frame(master)
  8.         frame.pack()
  9.  
  10.         self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
  11.         self.button.pack(side=RIGHT)
  12.  
  13.         self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
  14.         self.button2.pack(side=LEFT)
  15.  
  16.     def circle(self):
  17.  
  18.         canvas = Canvas(width=210, height=210)
  19.         canvas.pack(expand=YES, fill=BOTH)
  20.         # keep a reference to the canvas in order to work on it outside this function's (method's) scope
  21.         self.canvas = canvas
  22.  
  23.         self.blue=Button(canvas, text="colour blue", command=self.blue)
  24.         self.blue.pack()
  25.  
  26.         self.red=Button(canvas, text="colour red", command=self.red)
  27.         self.red.pack()
  28.  
  29.         canvas2 = Canvas(width=210, height=210, bg='black')
  30.         canvas2.pack(expand=YES, fill=BOTH)
  31.         self.canvas2 = canvas2
  32.         # keep a reference to the item ID in order to work on it outside this function's (method's) scope
  33.         self.ovalID = canvas2.create_oval(10, 10, 200, 200, width=2, fill='red')
  34.  
  35.     def blue(self):
  36.         self.canvas2.itemconfigure(self.ovalID, fill="blue")
  37.  
  38.     def red(self):
  39.         self.canvas2.itemconfigure(self.ovalID, fill="red")
  40.  
  41.  
  42. root = Tk()
  43.  
  44. op = Op(root)
  45.  
  46. root.mainloop()
  47.  
So many thanks Barton! Problem solved :) Next I'm going to tackle doing the same thing but with a slider, woo.
But for now back to christmas tv and turkey sandwiches.
Dec 25 '06 #5

bartonc
Expert 5K+
P: 6,596
I don't generally work on christmas(in fact I never), but whilst waiting for the cheesy xmas tv I had a burst of enlightenment as to why the code wasn't working. Simply, the answer was there are 2 canvases and it was the wrong canvas being told to change.

I now have 2 versions, the first where the new buttons are in the existing frame which looks messy, and the second where their on their own canvas and it looks tidier

For those who might find it helpful, here it is:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Op:
  4.  
  5.     def __init__(self, master):
  6.  
  7.         frame = Frame(master)
  8.         frame.pack()
  9.  
  10.         self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
  11.         self.button.pack(side=RIGHT)
  12.  
  13.         self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
  14.         self.button2.pack(side=LEFT)
  15.  
  16.     def circle(self):
  17.  
  18.         canvas = Canvas(width=210, height=210)
  19.         canvas.pack(expand=YES, fill=BOTH)
  20.         # keep a reference to the canvas in order to work on it outside this function's (method's) scope
  21.         self.canvas = canvas
  22.  
  23.         self.blue=Button(canvas, text="colour blue", command=self.blue)
  24.         self.blue.pack()
  25.  
  26.         self.red=Button(canvas, text="colour red", command=self.red)
  27.         self.red.pack()
  28.  
  29.         canvas2 = Canvas(width=210, height=210, bg='black')
  30.         canvas2.pack(expand=YES, fill=BOTH)
  31.         self.canvas2 = canvas2
  32.         # keep a reference to the item ID in order to work on it outside this function's (method's) scope
  33.         self.ovalID = canvas2.create_oval(10, 10, 200, 200, width=2, fill='red')
  34.  
  35.     def blue(self):
  36.         self.canvas2.itemconfigure(self.ovalID, fill="blue")
  37.  
  38.     def red(self):
  39.         self.canvas2.itemconfigure(self.ovalID, fill="red")
  40.  
  41.  
  42. root = Tk()
  43.  
  44. op = Op(root)
  45.  
  46. root.mainloop()
  47.  
So many thanks Barton! Problem solved :) Next I'm going to tackle doing the same thing but with a slider, woo.
But for now back to christmas tv and turkey sandwiches.
I'm glad that you solved the problem, but mad at myself for missing the creation of a second canvas. I guess that is the difference between the author and the reader of a chunk of code... I still don't see the first canvas being used for anything, though. You are welcome for what little help that I gave and I hope that you keep posting. Here is a fun Tkinter "script" (not OO style).
Dec 25 '06 #6

bartonc
Expert 5K+
P: 6,596
Mel's got a cool avatar, too. I guess I'm on an avatar kick.
Dec 27 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.