【发布时间】:2021-07-18 15:01:24
【问题描述】:
应该发生的是,我运行代码,TKinter 窗口打开,您只需单击一个按钮,turtle 打开并绘制形状。
但是,当我运行我的代码时,Tkinter 窗口和海龟窗口打开,但它只是立即开始绘制形状。 Tkinter 窗口也没有任何按钮。
我不确定我的代码有什么问题。请给我一些帮助。
我的代码:
import tkinter as tk
import turtle as t
window = tk.Tk()
window.geometry("500x500")
def square():
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(200)
t.exitonclick
def triangle():
t.forward(200)
t.right(135)
t.forward(200)
t.right(115)
t.forward(200)
t.exitonclick
def rectangle():
t.forward(200)
t.right(90)
t.forward(100)
t.right(90)
t.forward(200)
t.right(90)
t.forward(100)
t.exitonclick
b1 = tk.Button(window, command=square(), text="Square", bg="Red")
b2 = tk.Button(window, command=triangle(), text="Triangle", bg="Cyan")
b3 = tk.Button(window, command=rectangle(), text="Rectangle", bg="Gold")
b1.place(x=0,y=0)
b2.place(x=0,y=30)
b3.place(x=0,y=60)
window.mainloop
【问题讨论】:
-
应该是
command=square,不带括号。您想告诉按钮要运行哪个函数,而不是通过立即运行它来给出函数的输出。 -
您不要在代码末尾调用
window.mainloop- 请注意这次没有括号...
标签: python python-3.x tkinter python-turtle