【问题标题】:Running two functions at the same time?同时运行两个函数?
【发布时间】:2021-11-16 03:46:40
【问题描述】:
from turtle import *
a = Screen()
b = Turtle()
c = Turtle()

def one():
    b.forward(100)
def two():
    c.forward(-100)

我希望 b 和 c 同时远离彼此,尝试了很多东西,但我无法弄清楚。请帮帮我。

【问题讨论】:

标签: python function python-turtle


【解决方案1】:

Multithreading With Python Turtle 借用所有代码,这似乎“有效”,可以让两个函数同时运行。

如果是这样,这可能是链接答案的副本。

import queue
import threading
import turtle

a = turtle.Screen()
b = turtle.Turtle()
c = turtle.Turtle()

def one():
    b.forward(100)
def two():
    c.forward(-100)

def process_queue():
    while not graphics.empty():
        (graphics.get())(1)

    if threading.active_count() > 1:
        turtle.ontimer(process_queue, 100)

graphics = queue.Queue(1)  # size = number of hardware threads you have - 1

turtle1 = turtle.Turtle('turtle')
turtle1.speed('fastest')
thread1 = threading.Thread(target=one)
thread1.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
thread1.start()

turtle2 = turtle.Turtle('turtle')
turtle2.speed('fastest')
thread2 = threading.Thread(target=two)
thread2.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
thread2.start()

turtle.mainloop()

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2021-07-20
    • 1970-01-01
    相关资源
    最近更新 更多