Python 如何在Tkinter中创建具有给定半径的圆?

Python 如何在Tkinter中创建具有给定半径的圆?,python,tkinter,Python,Tkinter,我要做的是使用create_oval()和Entry()小部件中输入的半径r创建一个圆。我不明白问题出在哪里。这是我的密码: 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 t

我要做的是使用
create_oval()
Entry()
小部件中输入的半径
r
创建一个圆。我不明白问题出在哪里。这是我的密码:

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()
以下是输出和控制台:

参数“command”获取指向函数的指针,而不是函数本身,因此您不能这样调用它。另外,当您第一次运行程序时,
get()
返回一个空字符串,从而导致您得到的无效文本错误,您需要在单击按钮时调用它,例如:

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()

希望能有所帮助。

在不导致.get()错误的情况下,radiumButton.grid(row=2,column=1)应该是radiusButton@EugeneProut啊,那应该是半径,我现在就编辑它,在我的代码中是这样的。首先你必须明白这是否回答了你的问题?是的,我现在明白了,非常感谢!