如何用一个python脚本打开powershell终端?

2024-10-01 07:35:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个基于文本的游戏,我通过双击我的顶级脚本TopLevel.py来运行。 我想在这个脚本中打开两个终端。在一个终端中,主游戏将运行在造成伤害和使用法术等地方。在另一个终端,我想显示一个命令列表,用户可以输入,我希望后一个留在那里,不关闭,直到游戏结束。我不打算向您展示整个顶层脚本(它太长了),但这基本上是我想要实现的:

    def displayCommands(hero):
        list_of_commands = []
        #this contains all my commands that the user can type in

    def main():
        hero = Hero() #make hero instance
        enemy = Enemy() #make and enemy instance
        a_game = TopLevel(hero,enemy) #create game engine
        a_game.play() #start game

        #implement code here to open another terminal 
        #and display user commands in there

有没有一种方法可以在这个脚本中打开另一个终端,并将displayCommands()函数作为参数传递给第二个终端来显示它的内容?如有任何帮助,我们将不胜感激:)


Tags: andinstancein脚本game终端游戏make
2条回答

你应该先把你的第二个程序转换成.exe,这个程序会显示用户命令。假设您将其保存为usercommands.exe,然后在主脚本中使用此项打开该项

import subprocess
subprocess.Popen([r"usercommands.exe"],
                 creationflags=subprocess.CREATE_NEW_CONSOLE)

当您运行主文件时,它将打开另一个控制台窗口,该窗口运行usercommands.exe。在

注释

They must be in the same directory, your main file and .exe file

usercommands.exe must show the commands in an infinite loop(while True), so it's going to display commands untill the user close it.

编辑

现在,我们有了一些咒语,usercommands必须使用这些咒语变量,然后向我们展示一些组合。为此,我们必须将您的第一个文件导入usercommands脚本。它会喜欢这样的

usercommands.py

^{pr2}$

算法应该是这样的,您将usercommands.py转换为.exe,然后它将按您想要的方式工作我猜,在您尝试之前我们现在不能;)

一个Python脚本可以通过subprocess生成另一个与之并行运行的脚本。然后,派生的进程可以在一个简单的基于tkinter的窗口中显示任何通过管道传输到它的文本(通过普通的print语句或调用)有关更多信息,请参阅我的this answer中的errorwindow模块。在

原始脚本如何开始可能并不重要。我个人在从命令shell和其他基于tkinter的应用程序启动的应用程序中都使用过它,所以从powershell启动您的应用程序应该可以。我相信,这个模块的最初作者使用的是Linux或类似的东西。在

相关问题 更多 >