如何用一个 python 脚本打开一个 powershell 终端? [英] How to open a powershell terminal with one python script?

查看:60
本文介绍了如何用一个 python 脚本打开一个 powershell 终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a text based game that I run by double clicking on my top level script namely 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

有没有办法可以在此脚本中打开另一个终端并将 displayCommands() 函数作为参数传递以在第二个终端中显示其内容?任何帮助将不胜感激:)

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? Any help will be appreciated :)

推荐答案

一个 Python 脚本可能衍生出另一个将通过 subprocess 与其并行运行的脚本.然后,生成的进程可以在一个简单的基于 tkinter 的窗口中显示任何通过管道传送到它的文本(通过普通的 print 语句或调用)——参见 errorwindow 模块在我的 这个答案 中以获取更多信息.

It's possible for one Python script to spawn another that will run in parallel with it via 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.

原始脚本的启动方式可能无关紧要.我个人在从命令外壳启动的应用程序以及其他基于 tkinter 的应用程序中都使用过它——所以从 powershell 启动你的应用程序应该没问题.我相信该模块的原作者使用的是 Linux 或类似的东西.

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. The module's original author was using Linux or something similar, I believe.

这篇关于如何用一个 python 脚本打开一个 powershell 终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆