一次执行多个Python命令

一次执行多个Python命令,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我想知道在python中同时执行两个或多个命令的最简单方法是什么。例如: from turtle import * turtle_one=Turtle() turtle_two=Turtle() turtle_two.left(180) #The lines to be executed at the same time are below. turtle_one.forward(100) turtle_two.forward(100) 尝试使用线程模块 from turtle import

我想知道在python中同时执行两个或多个命令的最简单方法是什么。例如:

from turtle import *

turtle_one=Turtle()
turtle_two=Turtle()
turtle_two.left(180)
#The lines to be executed at the same time are below.
turtle_one.forward(100)
turtle_two.forward(100)

尝试使用线程模块

from turtle import *
from threading import Thread

turtle_one=Turtle()
turtle_two=Turtle()
turtle_two.left(180)

Thread(target=turtle_one.forward, args=[100]).start()
Thread(target=turtle_two.forward, args=[100]).start()
这将在后台启动
turtle\u one/two.forward
函数,参数为100

为了简化操作,请在后台运行
函数

def run_in_background(func, *args):
    Thread(target=func, args=args).start()

run_in_background(turtle_one.forward, 100)
run_in_background(turtle_two.forward, 100)

尝试使用线程模块

from turtle import *
from threading import Thread

turtle_one=Turtle()
turtle_two=Turtle()
turtle_two.left(180)

Thread(target=turtle_one.forward, args=[100]).start()
Thread(target=turtle_two.forward, args=[100]).start()
这将在后台启动
turtle\u one/two.forward
函数,参数为100

为了简化操作,请在后台运行
函数

def run_in_background(func, *args):
    Thread(target=func, args=args).start()

run_in_background(turtle_one.forward, 100)
run_in_background(turtle_two.forward, 100)

您可以使用turtle模块附带的计时器事件有效地执行此操作:

from turtle import Turtle, Screen

turtle_one = Turtle(shape="turtle")
turtle_one.setheading(30)
turtle_two = Turtle(shape="turtle")
turtle_two.setheading(210)

# The lines to be executed at the same time are below.
def move1():
    turtle_one.forward(5)

    if turtle_one.xcor() < 100:
        screen.ontimer(move1, 50)

def move2():
    turtle_two.forward(10)

    if turtle_two.xcor() > -100:
        screen.ontimer(move2, 100)

screen = Screen()

move1()
move2()

screen.exitonclick()
从海龟导入海龟,屏幕
海龟一只=海龟(shape=“海龟”)
海龟一号,设置航向(30)
海龟二号=海龟(shape=“海龟”)
乌龟二号,设定航向(210)
#要同时执行的行如下所示。
def move1():
乌龟一号,前进(5)
如果一个.xcor()小于100:
屏幕时间(移动1,50)
def move2():
乌龟二号,前进(10)
如果turtle_two.xcor()>-100:
屏幕时间(move2100)
screen=screen()
move1()
动议二()
screen.exitonclick()
关于线程,正如其他人所建议的,请阅读文章中讨论的问题,如Python的turtle模块是基于Tkinter构建的,最近的文章指出:

许多GUI工具包不是线程安全的,tkinter也不是 例外情况


您可以使用turtle模块附带的计时器事件有效地执行此操作:

from turtle import Turtle, Screen

turtle_one = Turtle(shape="turtle")
turtle_one.setheading(30)
turtle_two = Turtle(shape="turtle")
turtle_two.setheading(210)

# The lines to be executed at the same time are below.
def move1():
    turtle_one.forward(5)

    if turtle_one.xcor() < 100:
        screen.ontimer(move1, 50)

def move2():
    turtle_two.forward(10)

    if turtle_two.xcor() > -100:
        screen.ontimer(move2, 100)

screen = Screen()

move1()
move2()

screen.exitonclick()
从海龟导入海龟,屏幕
海龟一只=海龟(shape=“海龟”)
海龟一号,设置航向(30)
海龟二号=海龟(shape=“海龟”)
乌龟二号,设定航向(210)
#要同时执行的行如下所示。
def move1():
乌龟一号,前进(5)
如果一个.xcor()小于100:
屏幕时间(移动1,50)
def move2():
乌龟二号,前进(10)
如果turtle_two.xcor()>-100:
屏幕时间(move2100)
screen=screen()
move1()
动议二()
screen.exitonclick()
关于线程,正如其他人所建议的,请阅读文章中讨论的问题,如Python的turtle模块是基于Tkinter构建的,最近的文章指出:

许多GUI工具包不是线程安全的,tkinter也不是 例外情况


我写这篇评论只是想指出,这与在Python中并行执行事情(有几种解决方案)关系不大,而与使用Python的Turtle模块并行执行动画关系更大,这两者都会限制人们的选择,我写这篇评论只是想指出,这与在Python中并行执行事情(有几种解决方案)关系不大,而更多的是使用Python的Turtle模块并行执行动画,这将限制人们的选择,由于某种原因,这对turtle graphics不起作用,但对于其他事情,如打印、等待和再次打印,它却能很好地工作。谢谢。@coDE\u RP您使用的是什么版本的Python和Turtle?我现在使用的是Python 3.5.2和Turtle 1.1bAh ok。我使用的是Python2.7,所以这可能会有所不同。出于某种原因,这对turtle图形不起作用,但是对于其他事情,例如打印、等待和再次打印,它工作得非常好。谢谢。@coDE\u RP您使用的是什么版本的Python和Turtle?我现在使用的是Python 3.5.2和Turtle 1.1bAh ok。我使用的是Python2.7,所以这可能会有所不同。谢谢,这非常有效!唯一的问题是,它实际上并不同时做这两件事,它只是快速连续地重复它们。谢谢,这真的很有效!唯一的问题是,它实际上并不同时做这两件事,它只是快速地重复它们。