【问题标题】:Stop mainloop in turtle by check variable通过检查变量停止龟中的主循环
【发布时间】:2019-10-18 19:18:38
【问题描述】:

我正在尝试使用turtle 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 时我无法退出 mainloop。

我怎样才能退出主循环?

我在程序中使用https://repl.it/@eliadchoen/BrightShinyKernel

【问题讨论】:

标签: python turtle-graphics


【解决方案1】:

在您使用完海龟图形之前,您不会离开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!"

我的猜测是这不是你的目标,所以在你的问题中解释你想要发生的事情。

【讨论】:

    猜你喜欢
    • 2014-08-20
    • 2020-07-11
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2020-01-03
    相关资源
    最近更新 更多