Changing Colour with a Button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sushi
    New Member
    • Dec 2006
    • 18

    Changing Colour with a Button

    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:

    Code:
    from Tkinter import *
    
    class Op:
        
        def __init__(self, master):
            
            frame = Frame(master)
            frame.pack()
            
            self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
            self.button.pack(side=RIGHT)
            
            self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
            self.button2.pack(side=LEFT)
          
        def circle(self):
            
            canvas = Canvas(width=210, height=210)  
            canvas.pack()                
        
            self.button4=Button(canvas, text="colour blue", command=self.colour)
            self.button4.pack()
            
            canvas = Canvas(width=210, height=210, bg='black')  
            canvas.pack(expand=YES, fill=BOTH)                
            canvas.create_oval(10, 10, 200, 200, width=2, fill='red')
            
        def colour(self):
            canvas.create_oval(10, 10, 200, 200, width=2, fill='blue')
            create_oval.pack()
          
    root = Tk()
    
    op = Op(root)
    
    root.mainloop()
    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
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    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.

    Code:
    from Tkinter import *
    
    class Op:
    
        def __init__(self, master):
    
            frame = Frame(master)
            frame.pack()
    
            self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
            self.button.pack(side=RIGHT)
    
            self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
            self.button2.pack(side=LEFT)
    
        def circle(self):
    
            canvas = Canvas(width=210, height=210)
            canvas.pack()
            # keep a reference to the canvas in order to work on it outside this function's (method's) scope
            self.canvas = canvas
    
            self.button4=Button(canvas, text="colour blue", command=self.colour)
            self.button4.pack()
    
            canvas = Canvas(width=210, height=210, bg='black')
            canvas.pack(expand=YES, fill=BOTH)
            # keep a reference to the item ID in order to work on it outside this function's (method's) scope
            self.ovalID = canvas.create_oval(10, 10, 200, 200, width=2, fill='red')
    
        def colour(self):
            print "change color of item #%d" %self.ovalID
            print repr(self.canvas.itemcget(self.ovalID, "fill"))
            self.canvas.itemconfigure(self.ovalID, fill="blue")
    
    
    root = Tk()
    
    op = Op(root)
    
    root.mainloop()

    Comment

    • Sushi
      New Member
      • Dec 2006
      • 18

      #3
      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

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Sushi
        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.

        Comment

        • Sushi
          New Member
          • Dec 2006
          • 18

          #5
          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:

          Code:
          from Tkinter import *
          
          class Op:
          
              def __init__(self, master):
          
                  frame = Frame(master)
                  frame.pack()
                  self.frame = frame
          
                  self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
                  self.button.pack(side=RIGHT)
          
                  self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
                  self.button2.pack(side=LEFT)
          
              def circle(self):
          
                  self.blue=Button(self.frame, text="colour blue", command=self.blue)
                  self.blue.pack(side=BOTTOM)
                  
                  self.red=Button(self.frame, text="colour red", command=self.red)
                  self.red.pack(side=BOTTOM)
          
                  canvas = Canvas(width=210, height=210, bg='black')
                  canvas.pack(expand=YES, fill=BOTH)
                  self.canvas = canvas
                  # keep a reference to the item ID in order to work on it outside this function's (method's) scope
                  self.ovalID = canvas.create_oval(10, 10, 200, 200, width=2, fill='red')
          
              def blue(self):
                  self.canvas.itemconfigure(self.ovalID, fill="blue")
                  
              def red(self):
                  self.canvas.itemconfigure(self.ovalID, fill="red")
          
          
          root = Tk()
          
          op = Op(root)
          
          root.mainloop()
          or
          Code:
          from Tkinter import *
          
          class Op:
          
              def __init__(self, master):
          
                  frame = Frame(master)
                  frame.pack()
          
                  self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
                  self.button.pack(side=RIGHT)
          
                  self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
                  self.button2.pack(side=LEFT)
          
              def circle(self):
          
                  canvas = Canvas(width=210, height=210)
                  canvas.pack(expand=YES, fill=BOTH)
                  # keep a reference to the canvas in order to work on it outside this function's (method's) scope
                  self.canvas = canvas
          
                  self.blue=Button(canvas, text="colour blue", command=self.blue)
                  self.blue.pack()
                  
                  self.red=Button(canvas, text="colour red", command=self.red)
                  self.red.pack()
          
                  canvas2 = Canvas(width=210, height=210, bg='black')
                  canvas2.pack(expand=YES, fill=BOTH)
                  self.canvas2 = canvas2
                  # keep a reference to the item ID in order to work on it outside this function's (method's) scope
                  self.ovalID = canvas2.create_oval(10, 10, 200, 200, width=2, fill='red')
          
              def blue(self):
                  self.canvas2.itemconfigure(self.ovalID, fill="blue")
                  
              def red(self):
                  self.canvas2.itemconfigure(self.ovalID, fill="red")
          
          
          root = Tk()
          
          op = Op(root)
          
          root.mainloop()
          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.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by Sushi
            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:

            Code:
            from Tkinter import *
            
            class Op:
            
                def __init__(self, master):
            
                    frame = Frame(master)
                    frame.pack()
            
                    self.button = Button(frame, text="Bye Bye", fg="green", bg="black", command=frame.quit)
                    self.button.pack(side=RIGHT)
            
                    self.button2 = Button(frame, text="circle", fg="red", bg="black", command=self.circle)
                    self.button2.pack(side=LEFT)
            
                def circle(self):
            
                    canvas = Canvas(width=210, height=210)
                    canvas.pack(expand=YES, fill=BOTH)
                    # keep a reference to the canvas in order to work on it outside this function's (method's) scope
                    self.canvas = canvas
            
                    self.blue=Button(canvas, text="colour blue", command=self.blue)
                    self.blue.pack()
                    
                    self.red=Button(canvas, text="colour red", command=self.red)
                    self.red.pack()
            
                    canvas2 = Canvas(width=210, height=210, bg='black')
                    canvas2.pack(expand=YES, fill=BOTH)
                    self.canvas2 = canvas2
                    # keep a reference to the item ID in order to work on it outside this function's (method's) scope
                    self.ovalID = canvas2.create_oval(10, 10, 200, 200, width=2, fill='red')
            
                def blue(self):
                    self.canvas2.itemconfigure(self.ovalID, fill="blue")
                    
                def red(self):
                    self.canvas2.itemconfigure(self.ovalID, fill="red")
            
            
            root = Tk()
            
            op = Op(root)
            
            root.mainloop()
            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).

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Mel's got a cool avatar, too. I guess I'm on an avatar kick.

              Comment

              Working...