Python 海龟库“onclick”的问题功能

发布于 2025-01-14 11:45:42 字数 362 浏览 0 评论 0原文

所以,问题是“onclick”功能不起作用。相反,它只是在代码启动时打印测试代码(print('working'))。请帮忙!

SubmitButton = turtle.Turtle()
SubmitButton.penup()
SubmitButton.goto(0,-300)
SubmitButton.shape('square')
SubmitButton.shapesize(2)
SubmitButton.fillcolor('red')
SubmitButton.penup()
SubmitButton.goto
SubmitButton.onclick(print("working"))

有代码片段,让我知道您是否需要更多。

So, the problem is is that the "onclick" function is not working. It is instead just printing the test code (print('working')) when the code is started. Please help!

SubmitButton = turtle.Turtle()
SubmitButton.penup()
SubmitButton.goto(0,-300)
SubmitButton.shape('square')
SubmitButton.shapesize(2)
SubmitButton.fillcolor('red')
SubmitButton.penup()
SubmitButton.goto
SubmitButton.onclick(print("working"))

There is the snippet of the code, lemme know if you need anymore.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2025-01-21 11:45:42

onclick 需要传递一个函数,它将调用该函数。该函数需要有两个参数。 (请参阅文档,您应该始终使用它在此处发帖之前先查阅一下)。您向其传递 print() 的返回值,即 None。您可以使用 lambda,它创建一个函数,在调用该函数时将调用 print()

SubmitButton.onclick(lambda x, y: print("working"))

或者您可以定义一个函数并将其传入:

def onclick_function(x, y):
    print("working")

SubmitButton.onclick(onclick_function)

请注意如何我传入 onclick_function 但不调用该函数。

onclick needs to be passed a function, which it will call. The function needs to have two arguments. (See the Documentation, which you should always look up before posting here). You're passing it the returned value of a print(), which is None. You could use a lambda, which creates a function that will call print() when that function is called:

SubmitButton.onclick(lambda x, y: print("working"))

Or you could define a function and pass that in:

def onclick_function(x, y):
    print("working")

SubmitButton.onclick(onclick_function)

Notice though how I'm passing in onclick_function but not calling that function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文