-1

What I want to do is create a circle using create_oval() with the radius r entered in Entry() widget. I don't understand what the problem is. Here's my code:

from tkinter import *

root = Tk()

#Drawing the circle
def circle(canvas,x,y,r):
   id = canvas.create_oval(x-r,y-r,x+r,y+r)
   return id


#Label
radiusLabel = Label(root, text="Enter the radius: ")
radiusLabel.grid(row=0,column=0)


#Entering the radius
radiusEntry = Entry(root)
radiusEntry.grid(row=1, column=0)


r = int(radiusEntry.get())

#Canvas 
canvas = Canvas(width=400,height=400, bg="#cedbd2")
canvas.grid(row=2,column=0)


#Calling the function for drawing the circle button
radiusButton = Button(root, text="Create",command=circle(canvas,100,100,r))
radiusButton.grid(row=2,column=1)

root.mainLoop()

Here's the output and the console: enter image description here

MareGraphics MaRe
  • 439
  • 1
  • 5
  • 14
  • While not causing the .get() error, radiumButton.grid(row=2,column=1) should probably be radiusButton – EugeneProut Feb 16 '20 at 13:58
  • @EugeneProut Ah, that should be Radius, Ill edit it now, it is like that in my code. – MareGraphics MaRe Feb 16 '20 at 14:04
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) Does this answer your question? [valueerror-invalid-literal-for-int-with-base-10](https://stackoverflow.com/questions/1841565) – stovfl Feb 16 '20 at 14:12

1 Answers1

1

The parameter "command" takes a pointer to a function, not a function itself so you cannot call it like that, also, when you first run the program, get() returns an empty string thus causing the invalid literal error you are getting, you need to call it when your button is clicked, e.g.:

from tkinter import *

root = Tk()

#Label
radiusLabel = Label(root, text="Enter the radius: ")
radiusLabel.grid(row=0,column=0)


#Entering the radius
radiusEntry = Entry(root)
radiusEntry.grid(row=1, column=0)

#Canvas 
canvas = Canvas(width=400,height=400, bg="#cedbd2")
canvas.grid(row=2,column=0)

#Drawing the circle
def circle():
    x=100
    y=100
    r = 0
    if radiusEntry.get():
        r = int(radiusEntry.get())
    id = canvas.create_oval(x-r,y-r,x+r,y+r)
    return id

#Calling the function for drawing the circle button
radiusButton = Button(root, text="Create", command=circle)
radiusButton.grid(row=2,column=1)


root.mainloop()

Hope it helps.

Isma
  • 14,604
  • 5
  • 37
  • 51