[英]How to open a powershell terminal with one python script?
I am working on a text based game that I run by double clicking on my top level script namely TopLevel.py
. 我正在开发一个基于文本的游戏,该游戏通过双击我的顶级脚本
TopLevel.py
。 I am looking for a way to open two terminals in this script. 我正在寻找一种在此脚本中打开两个终端的方法。 In the one terminal the main game will be run where damage is done and spells are used etc. In the other terminal I would like to display a list of commands that the user can type in , and I want the latter one to stay there and not close until the game is finished.
在一个终端中,将运行主要游戏,在游戏中会造成伤害并使用咒语等。在另一个终端中,我想显示用户可以键入的命令列表,我希望后者留在那里并直到游戏结束才关闭。 I am not going to show you the whole top level script (it is too long) but this is basically what I want to achieve:
我不会向您展示整个顶级脚本(太长了),但这基本上是我想要实现的目标:
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
Is there a way that I can open another terminal in this script and pass the displayCommands()
function as a parameter to display its contents in the second terminal? 有没有一种方法可以在此脚本中打开另一个终端,并将
displayCommands()
函数作为参数传递,以在第二个终端中显示其内容? Any help will be appreciated :) 任何帮助将不胜感激 :)
You should convert your second program to .exe first,the one will display user commands. 您应该先将第二个程序转换为.exe,该程序将显示用户命令。 Say you saved it as
usercommands.exe
, after then in your main script use this to open that; 假设您将其保存为
usercommands.exe
,然后在主脚本中使用它打开该文件;
import subprocess
subprocess.Popen([r"usercommands.exe"],
creationflags=subprocess.CREATE_NEW_CONSOLE)
It'll open another console window that runs usercommands.exe
when you run your main file. 当您运行主文件时,它将打开另一个运行
usercommands.exe
控制台窗口。
Notes; 笔记;
They must be in the same directory, your main file and .exe file
它们必须位于同一目录中,即您的主文件和.exe文件
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.exe
必须在无限循环中显示命令(while True
),因此它将显示命令,直到用户关闭它为止。
Edit; 编辑;
Now, we have some spells and usercommands
will have to use that spell variables after then show us some combinations. 现在,我们有了一些拼写,并且在向我们展示了一些组合之后,用户
usercommands
将不得不使用该拼写变量。 For doing this, we have to import your first file to usercommands
script.It's going to like this; 为此,我们必须将您的第一个文件导入到
usercommands
脚本中。
usercommands.py
usercommands.py
import mainfile #mainfile.py
if choose == mainfile.wizard:
print (something)
if choose == mainfile.subzero:
print (something)
The algorithm should be like this,you will convert usercommands.py
to .exe then it will work as you want I guess, we can't now before you try it ;) 该算法应该是这样的,您可以将
usercommands.py
转换为.exe,然后它将按您想的那样工作,我们现在无法尝试;)
It's possible for one Python script to spawn another that will run in parallel with it via subprocess
. 一个Python脚本可能会生成另一个将通过
subprocess
与之并行运行的脚本。 The spawned process can then display any text piped to it (via normal print
statements or calls) in a simple tkinter
-based window -- see the errorwindow
module in this answer of mine for more information. 然后将生成的进程可以显示管道输送到任何文本(通过正常
print
报表或调用)一个简单tkinter
基础的窗口-看到errorwindow
模块这个答案我的更多信息。
It likely doesn't matter how the original script gets started. 原始脚本的启动方式可能并不重要。 I personally have used it both in ones that were started from a command shell as well as from other
tkinter
based applications -- so starting yours from powershell should be fine. 我个人已经在从命令外壳程序以及从其他基于
tkinter
的应用程序启动的应用程序中使用过它-因此从powershell启动应用程序应该很好。 The module's original author was using Linux or something similar, I believe. 我相信,该模块的原始作者正在使用Linux或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.