473,792 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing Colour with a Button

Sushi
19 New Member
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 7619
bartonc
6,596 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
5208
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 need the bgcolor to change at the same time when a user clicks a button. Right now only one changes when I use something like this... document.getElementById(´cellchange´).style.backgroundColor = "FFFFFF" ** (Note, this is not the EXACT code, I...
25
4349
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 for the color of the tabs. (which I would then change with trough php). 3. I could make a different class for each color, and change the class of the tab.
5
1522
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 .Backcolor = etc is there a way I can do it for all of them grouped together, given that the whole group needs to change to the same colours?
2
6631
by: Scott | last post by:
Is it possible to have a button change colour OnClick ? Thanks.
5
1328
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: System::Windows::Forms::Button * Wolfgang; for (i=1;i<100;i++) { Wolfgang = ???;
2
1190
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 want to change the background colour of a button to the default background colour for controls, it may or may not be grey (depending on the preferences, eg Windows Classic style vs. Windows XP style or a customised style may have changed the...
3
10644
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 can get my question answered. Thank you very much.
3
2628
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 he can click to flag a pupil as "Do Not Rebook" (i.e. if they're a non-payer or something). Upon clicking the button, he wants a value to be recorded in the appropriate field in the table (so we can report on it later) and he also wants the...
4
1934
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" title="2008" href="../announcementList.do?year=2008"> 2008
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.