通过检查变量停止乌龟中的主循环

时间:2019-06-03 08:47:50

标签: python turtle-graphics

我正在尝试用乌龟python编写程序,要求用户输入数字,然后让他在屏幕上单击相同的次数。

import turtle

t = turtle.Turtle()
count = 0

def up_count(x,y):
  global count
  count = count + 1
  print count
  return

def start():

  num1=int(raw_input("enter number"))
  print "in the gaeme you need to enter number and click on button",num1,"times"
  s = t.getscreen()
  if not num1 == count:
    s.onclick(up_count)
  else:
    t.mainloop()


start()

问题是当num1 == count时,我无法退出主循环。

如何退出主循环?

我将https://repl.it/@eliadchoen/BrightShinyKernel用于该程序。

1 个答案:

答案 0 :(得分:0)

直到您对乌龟图形的使用完成后,您才能离开mainloop() 。例如:

from turtle import Screen, Turtle, mainloop

count = 0
number = -1

def up_count(x, y):
    global count
    count += 1

    if number == count:
        screen.bye()

def start():
    global number

    number = int(raw_input("Enter number: "))
    print "You need to click on window", number, "times"

    screen.onclick(up_count)

screen = Screen()
turtle = Turtle()

start()

mainloop()

print "Game over!"

我的猜测是这不是您的目标,因此请在您的问题中解释您想要实现的目标。