[英]How to open a powershell terminal with one python script?
我正在开发一个基于文本的游戏,该游戏通过双击我的顶级脚本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()
函数作为参数传递,以在第二个终端中显示其内容? 任何帮助将不胜感激 :)
您应该先将第二个程序转换为.exe,该程序将显示用户命令。 假设您将其保存为usercommands.exe
,然后在主脚本中使用它打开该文件;
import subprocess
subprocess.Popen([r"usercommands.exe"],
creationflags=subprocess.CREATE_NEW_CONSOLE)
当您运行主文件时,它将打开另一个运行usercommands.exe
控制台窗口。
笔记;
它们必须位于同一目录中,即您的主文件和.exe文件
usercommands.exe
必须在无限循环中显示命令(while True
),因此它将显示命令,直到用户关闭它为止。
编辑;
现在,我们有了一些拼写,并且在向我们展示了一些组合之后,用户usercommands
将不得不使用该拼写变量。 为此,我们必须将您的第一个文件导入到usercommands
脚本中。
usercommands.py
import mainfile #mainfile.py
if choose == mainfile.wizard:
print (something)
if choose == mainfile.subzero:
print (something)
该算法应该是这样的,您可以将usercommands.py
转换为.exe,然后它将按您想的那样工作,我们现在无法尝试;)
一个Python脚本可能会生成另一个将通过subprocess
与之并行运行的脚本。 然后将生成的进程可以显示管道输送到任何文本(通过正常print
报表或调用)一个简单tkinter
基础的窗口-看到errorwindow
模块这个答案我的更多信息。
原始脚本的启动方式可能并不重要。 我个人已经在从命令外壳程序以及从其他基于tkinter
的应用程序启动的应用程序中使用过它-因此从powershell启动应用程序应该很好。 我相信,该模块的原始作者正在使用Linux或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.