Python turtle 程序在 IDLE 下工作,否则不行
Python turtle program works in IDLE, not otherwise
我在 Python 中使用 turtle 创建了一个简单的按钮程序。
它很可能非常草率,但在 IDLE 中工作得很好。但是,当尝试在没有 IDLE 的情况下加载它时,它只会绘制两个按钮,然后退出程序。我查看了代码,但没有成功找到原因。
这是我认为的问题所在(最后几行):
def main():
onscreenclick(Button.clicked,1)
main()
不过我不太确定。这是完整的程序以防万一。
from turtle import *
bgcolor('skyblue')
penup()
left(90)
speed(0)
hideturtle()
buttonlist = []
class Button:
x_click = 0
y_click = 0
def __init__(self, x, y, size, color, text, fontsize, fixvalue):
self.x = x
self.y = y
self.size = size
self.color = color
self.text = text
self.fontsize = fontsize
self.fixvalue = fixvalue
def showButton(self):
goto(self.x , self.y)
pendown()
fillcolor(self.color)
begin_fill()
for i in range(4):
forward(self.size)
right(90)
end_fill()
penup()
goto((self.x+self.size/2),self.y+self.fixvalue)
right(90)
write(self.text, move=False, align="center", font=("Arial", self.fontsize, "normal"))
left(90)
def hideButton(self):
goto(self.x, self.y)
fillcolor('skyblue')
pencolor('skyblue')
pendown()
begin_fill()
for i in range(4):
forward(self.size)
right(90)
end_fill()
penup()
pencolor('black')
def checkClick(self):
if self.x < Button.x_click:
if Button.x_click < (self.x+self.size):
if self.y < Button.y_click:
if Button.y_click < (self.y+self.size):
return 1
def clicked(x, y):
Button.x_click = x
Button.y_click = y
if home_1.checkClick() == 1:
home_1.hideButton()
if home_2.checkClick() == 1:
home_2.hideButton()
home_1 = Button(10,10,100,'red','←',45,20)
home_2 = Button(-50,-50,50,'blue','Hello!',10,15)
Button.showButton(home_1)
Button.showButton(home_2)
def main():
onscreenclick(Button.clicked,1)
main()
希望有解决办法
干杯。
您是对的,问题出在 main()
函数上,请尝试在末尾添加一个 turtle.mainloop()
调用:
def main():
onscreenclick(Button.clicked,1)
mainloop()
main()
如果这对您不起作用,您也可以尝试 turtle.done()
功能,但我建议您先尝试 mainloop()
:
def main():
onscreenclick(Button.clicked,1)
done()
main()
turtle
基于tkinter
,后者基于tcl/tk GUI框架。 Turtle 的 mainloop
最终调用了 tcl 事件处理程序 mainloop
,它重复调用 tcl 的 update() 函数,该函数处理未决事件并更新屏幕。 Tcl 的 mainloop 保持控制直到它被显式退出,例如通过关闭海龟 window.
IDLE 通过定期调用 tcl 的更新 无阻塞 来辅助 turtle 和 tkinter 的开发和学习,这样就可以(暂时)省略调用 mainloop 并与一个或多个屏幕交互通过在 IDLE Shell 中输入命令继续工作。但是当 运行 在 IDLE 之外时,需要将 mainloop 添加回来。
我在 Python 中使用 turtle 创建了一个简单的按钮程序。 它很可能非常草率,但在 IDLE 中工作得很好。但是,当尝试在没有 IDLE 的情况下加载它时,它只会绘制两个按钮,然后退出程序。我查看了代码,但没有成功找到原因。
这是我认为的问题所在(最后几行):
def main():
onscreenclick(Button.clicked,1)
main()
不过我不太确定。这是完整的程序以防万一。
from turtle import *
bgcolor('skyblue')
penup()
left(90)
speed(0)
hideturtle()
buttonlist = []
class Button:
x_click = 0
y_click = 0
def __init__(self, x, y, size, color, text, fontsize, fixvalue):
self.x = x
self.y = y
self.size = size
self.color = color
self.text = text
self.fontsize = fontsize
self.fixvalue = fixvalue
def showButton(self):
goto(self.x , self.y)
pendown()
fillcolor(self.color)
begin_fill()
for i in range(4):
forward(self.size)
right(90)
end_fill()
penup()
goto((self.x+self.size/2),self.y+self.fixvalue)
right(90)
write(self.text, move=False, align="center", font=("Arial", self.fontsize, "normal"))
left(90)
def hideButton(self):
goto(self.x, self.y)
fillcolor('skyblue')
pencolor('skyblue')
pendown()
begin_fill()
for i in range(4):
forward(self.size)
right(90)
end_fill()
penup()
pencolor('black')
def checkClick(self):
if self.x < Button.x_click:
if Button.x_click < (self.x+self.size):
if self.y < Button.y_click:
if Button.y_click < (self.y+self.size):
return 1
def clicked(x, y):
Button.x_click = x
Button.y_click = y
if home_1.checkClick() == 1:
home_1.hideButton()
if home_2.checkClick() == 1:
home_2.hideButton()
home_1 = Button(10,10,100,'red','←',45,20)
home_2 = Button(-50,-50,50,'blue','Hello!',10,15)
Button.showButton(home_1)
Button.showButton(home_2)
def main():
onscreenclick(Button.clicked,1)
main()
希望有解决办法
干杯。
您是对的,问题出在 main()
函数上,请尝试在末尾添加一个 turtle.mainloop()
调用:
def main():
onscreenclick(Button.clicked,1)
mainloop()
main()
如果这对您不起作用,您也可以尝试 turtle.done()
功能,但我建议您先尝试 mainloop()
:
def main():
onscreenclick(Button.clicked,1)
done()
main()
turtle
基于tkinter
,后者基于tcl/tk GUI框架。 Turtle 的 mainloop
最终调用了 tcl 事件处理程序 mainloop
,它重复调用 tcl 的 update() 函数,该函数处理未决事件并更新屏幕。 Tcl 的 mainloop 保持控制直到它被显式退出,例如通过关闭海龟 window.
IDLE 通过定期调用 tcl 的更新 无阻塞 来辅助 turtle 和 tkinter 的开发和学习,这样就可以(暂时)省略调用 mainloop 并与一个或多个屏幕交互通过在 IDLE Shell 中输入命令继续工作。但是当 运行 在 IDLE 之外时,需要将 mainloop 添加回来。