简体   繁体   English

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

[英]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.

相关问题 Python脚本打开并写入终端 - Python Script Open and Write into Terminal 如何在 VScode for Python 中将 PowerShell 终端更改为简单的终端? - How can I change a PowerShell terminal to a simple one in VScode for Python? 如何打开Python终端? - How to open Python Terminal? 如何使用 python + applescript 脚本在特定目录中打开 Mac 终端 - How to open Mac terminal at a specific directory using python + applescript script 在 Python 脚本中打开一个终端并在新打开的终端中执行终端命令 - Open a terminal in Python script and execute terminal commands in that newly opened terminal 从PowerShell终端添加到Python脚本吗? - Add to Python Script from PowerShell Terminal? 避免在终端中键入“ python”来打开.py脚本? - Avoid typing “python” in a terminal to open a .py script? 从 python 脚本打开多个独立终端 - Open multiple independent terminal from a python script 如何打开终端,在其上执行 python 脚本,然后等待脚本结束? - How can I open a terminal, execute a python script on it and then wait for the script to end? 如何在尝试运行 python 脚本时修复 powershell 终端中未定义的“名称错误'_' - How to fix "name error '_' not defined in powershell terminal while trying to run a python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM