473,419 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Changing Colour with a Button

Sushi
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
6 7582
bartonc
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
6,596 Expert 4TB
Mel's got a cool avatar, too. I guess I'm on an avatar kick.
Dec 27 '06 #7

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

Similar topics

14
by: Holden Caulfield | last post by:
Hi there, for IE 5+ I need to be able to change certain TD tags´ background colors, and can´t figure out how to do it... For instance, in a table with 25 cells, somewhere between 5 or 10 will...
25
by: madsgormlarsen | last post by:
Hi I am making a tab menu with css, and the tabs changes color depending on wich tab is current. 1. I could load a different css file for each tab/color. 2. I could do some inline css only...
5
by: Paolo | last post by:
I need to create a button which changes the back colour and the font colour and type of several fiels in a form, This is the way I would normally do it: .Backcolor = 11112121 .tabstop = True...
2
by: Scott | last post by:
Is it possible to have a button change colour OnClick ? Thanks.
5
by: Wolfgang | last post by:
Dear all, I've a lot of buttons named button1..button100 and want to change some of there propertys. Maintaining the overview I'm looking for a possibility for something like: ...
2
by: Colin McGuire | last post by:
Hi, according to the on-line documentation you can change the background colour of, for example a button, to Gray as shown below. Dim b as Button b.BackColor=Color.Gray If I programmatically...
3
by: Mel | last post by:
Hi, Is there any way I can force to change button background colour when toggle button is checked? Just like button change its background colour when mouseover in MS Visual Studio.NET. I hope I...
3
by: bettyboo | last post by:
Hi I'm new to the forum and also a VERY new user of Access to develop databases. I'm building a DB for a driving instructor acquaintance, and he wants a button on the pupil data entry form which...
4
by: sgxbytes | last post by:
Hi, My html has <Table style="year-button-archive" width="60%" > <tr> <td class="gold" > <a title="year">By Year:</a> <td class="gold"> <a id = "2008"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.