我已经使用了一个乌龟库(时钟)的示例并对其进行了扩展,因此即使打开了其他程序,它也位于顶部。我希望当有人单击时钟窗口时将其关闭。大多数情况下,它工作正常,但有时会打开一个新的空乌龟窗口。如何在不弹出新窗口的情况下关闭它?
这是代码关闭的部分:
def main():
tracer(False)
setup()
tracer(True)
tick()
wn.exitonclick()
这是整个程序:
import turtle as t
from datetime import datetime
import tkinter as Tkinter
def jump(distanz, winkel=0):
penup()
right(winkel)
forward(distanz)
left(winkel)
pendown()
def hand(laenge, spitze):
fd(laenge*1.15)
rt(90)
fd(spitze/2.0)
lt(120)
fd(spitze)
lt(120)
fd(spitze)
lt(120)
fd(spitze/2.0)
def make_hand_shape(name, laenge, spitze):
reset()
jump(-laenge*0.15)
begin_poly()
hand(laenge, spitze)
end_poly()
hand_form = get_poly()
register_shape(name, hand_form)
def clockface(radius):
reset()
pensize(7)
for i in range(60):
jump(radius)
if i % 5 == 0:
fd(5)
penup()
fd(20)
pendown()
jump(-radius-25)
else:
dot(3)
jump(-radius)
rt(6)
def setup():
global second_hand, minute_hand, hour_hand, writer, wn
mode("logo")
t.setup(width=150, height=150, startx=1920-150, starty=500)
t.title("Clock")
make_hand_shape("second_hand", 60, 15)
make_hand_shape("minute_hand", 50, 15)
make_hand_shape("hour_hand", 30, 15)
clockface(60)
second_hand = Turtle()
second_hand.shape("second_hand")
second_hand.color("gray20", "gray80")
minute_hand = Turtle()
minute_hand.shape("minute_hand")
minute_hand.color("blue1", "red1")
hour_hand = Turtle()
hour_hand.shape("hour_hand")
hour_hand.color("blue3", "red3")
for hand in second_hand, minute_hand, hour_hand:
hand.resizemode("user")
hand.shapesize(1, 1, 3)
hand.speed(0)
ht()
writer = Turtle()
wn=Screen()
wn.screensize(50, 50)
wn.getcanvas()._root().overrideredirect(True)
rootwindow = wn.getcanvas().winfo_toplevel()
rootwindow.call('wm', 'attributes', '.', '-topmost', '1')
#wn.exitonclick()
#rootwindow.call('wm', 'attributes', '.', '-topmost', '0')
#writer.mode("logo")
writer.ht()
writer.pu()
writer.bk(85)
def wochentag(t):
wochentag = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
return wochentag[t.weekday()]
def datum(z):
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
j = z.year
m = monat[z.month - 1]
t = z.day
return "%s %d %d" % (m, t, j)
def tick():
t = datetime.today()
sekunde = t.second + t.microsecond*0.000001
minute = t.minute + sekunde/60.0
stunde = t.hour + minute/60.0
try:
tracer(False) # Terminator can occur here
writer.clear()
writer.home()
writer.forward(25)
writer.write(wochentag(t),
align="center", font=("Courier", 11, "bold"))
writer.back(60)
writer.write(datum(t),
align="center", font=("Courier", 11, "bold"))
writer.forward(85)
tracer(True)
second_hand.setheading(6*sekunde) # or here
minute_hand.setheading(6*minute)
hour_hand.setheading(30*stunde)
tracer(True)
ontimer(tick, 100)
except Terminator:
pass # turtledemo user pressed STOP
def main():
tracer(False)
setup()
tracer(True)
tick()
wn.exitonclick()
return "EVENTLOOP"
try:
if __name__ == "__main__":
mode("logo")
msg = main()
print(msg)
mainloop()
except:
pass
这是输出:
输出:
答案 0 :(得分:0)
(如前所述,您的代码对我而言无法正确启动。)
我担心的是您同时拥有mainloop()
和exitonclick()
。它们执行相同的功能(exitonclick()
将onclick()
设置为bye()
并调用mainloop()
),并且应该只有其中一个。 (同上done()
。)
下面,我对代码进行了一些简化,以简化退出过程并使其同步,因为我设置了一个设置全局变量exitonclick()
的点击处理程序,而不是running
,{ 1}}例程检查是否应该再次调用tick()
或执行ontimer()
:
bye()
我希望您发现其中一些有用的变化。