0

I'm a new programmer who am relatively new at using classes and Tkinter. I've had a problem where my Tkinter window won't pop up nor would it show any errors.

import Tkinter
from Tkinter import Canvas, Button

root = Tkinter.Tk()
canvas = Canvas(root, width=360, height=360, bg="white")
canvas.grid()

color = canvas.create_oval(100, 100, 160, 160, fill = "white")

def changeColor(x):
    canvas.delete(color)
    c = canvas.create_oval(100, 100, 160, 160)
    canvas.itemconfigure(c,fill = x)


pink = Button(root, text = "This is just a button",changeColor("pink"))
pink.pack()

brown = Button(root, text = "This is brown",changeColor("brown"))
brown.pack()

root.mainloop()

I wanted to make a program where the color of the oval in the canvas changes according to the button. Any advices on how to fix this are greatly appreciated!

Paul Rooney
  • 19,499
  • 9
  • 39
  • 60
Newbie
  • 1
  • 1
  • 3
    make sure the title is a question, not "asking for advice" – Keith Nicholas Apr 26 '17 at 04:41
  • Yes it is, I updated it but to no avail... – Newbie Apr 26 '17 at 04:45
  • I can't get this code to run at all, there are several clear error messages. Which version of python 2 are you on? You should not mix `grid` and `pack` in the same canvas (see [here](http://stackoverflow.com/questions/23584325/cannot-use-geometry-manager-pack-inside)). – Paul Rooney Apr 26 '17 at 04:48
  • its python 2.7 not 1.0 – Surajano Apr 26 '17 at 04:55
  • I see 3 major issues. Firstly the pack/grid issue (see link in previous comment), the lack of a `command` parameter when creating the `Button`s and the fact that you dont pass a function as the parameter you intend for the `command`. I get a clear error, could your environment be hiding the errors from you? Try [this](https://ideone.com/uQj1mx) does it fix it? – Paul Rooney Apr 26 '17 at 04:59
  • It did. Seems like I missed a simple code. Thanks alot – Newbie Apr 26 '17 at 05:08
  • There's no reason to delete `color` in `changeColor`, nor to create a new oval. You can just use `canvas.itemconfigure(color,fill = x)`. However, as noted in the previous comment, you can't use `changeColor("pink")` as a command. You have to use a function name, like `changeToPink`, not a function call, not even `changeToPink()`. (You can also have changeColor return a function, as Paul Rooney showed, but you may not have gotten up to this yet.) – saulspatz Apr 26 '17 at 05:09

1 Answers1

1

I would be worried if your environment is hiding the tkinter error messages from you. If you are in this situation again, I suggest running your code outside the IDE/environment if possible.

Anyway here is the code with the errors that are stopping it from running fixed.

  1. Do not use grid and pack in the same master window.
  2. Add a command parameter to the Button constructors.
  3. Pass a function as the parameter to command, I changed what you had to use a closure to allow you to still specify the color parameter. It's basically just creating a function inside another function and returning that inner function, with some preloaded state (i.e. x), to be called later.

code:

import Tkinter
from Tkinter import Canvas, Button

root = Tkinter.Tk()
canvas = Canvas(root, width=360, height=360, bg="white")
canvas.grid()

color = canvas.create_oval(100, 100, 160, 160, fill = "white")

def changeColor(x):
    def f():
        canvas.itemconfig(color, fill = x)

    return f


pink = Button(root, text = "This is just a button", command=changeColor("pink"))
pink.grid()

brown = Button(root, text = "This is brown", command=changeColor("brown"))
brown.grid()

root.mainloop()

Also note, as suggested in comments, you don't need to delete and recreate the oval. You can just reconfigure the existing one.

Paul Rooney
  • 19,499
  • 9
  • 39
  • 60