Остановить mainloop в черепахе проверкой переменной - PullRequest
0 голосов
/ 03 июня 2019

Я пытаюсь написать программу с 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()

Проблема в том, что я не могу выйти из mainloop, когда num1 == count.

Как мне выйти из mainloop?

Я использую https://repl.it/@eliadchoen/BrightShinyKernel для программы.

1 Ответ

0 голосов
/ 03 июня 2019

Вы не выйдете из 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!"

Полагаю, это не ваша цель, поэтому объясните в своем вопросе, чего вы хотите от этого.

...