【问题标题】:In Python's Turtle, how can I pause my program while waiting for a click event?在 Python Turtle 中,如何在等待点击事件时暂停程序?
【发布时间】:2017-02-14 05:55:42
【问题描述】:

使用 Python 的海龟图形,我想接受我的用户点击。但是,我还需要我的程序在等待点击时暂停,而不是像现在这样继续。这是我的设置:

from turtle import *
screen = getscreen()

def getInput():
    coordinates = onscreenclick(clickHandler) # Returns x and y coordinates as a list
    print(coordinates) # Prints "None" as coordinates is empty

那么我该如何设置暂停,以便print(coordinates) 仅在收到点击后运行?

【问题讨论】:

    标签: python python-3.x onclick turtle-graphics


    【解决方案1】:

    下面的代码应该做你想做的。

    turtle 程序中的最后一条语句将控制权移交给等待点击和其他事件的事件循环——通常该语句是mainloop()done()exitonclick() 之一(不适用于这种情况):

    from turtle import Turtle, Screen
    
    FONT = ("Arial", 18, "normal")
    
    def clickHandler(x, y):
        yertle.undo()  # unwrite previous coordinates
        yertle.write((x, y), align="center", font=FONT)
    
    yertle = Turtle(visible=False)
    
    yertle.write((0, 0), align="center", font=FONT)
    
    screen = Screen()
    
    screen.onscreenclick(clickHandler)
    
    screen.mainloop()
    

    我没有将坐标打印到控制台,而是将它们写入窗口本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多