简体   繁体   中英

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

You should convert your second program to .exe first,the one will display user commands. Say you saved it as usercommands.exe , after then in your main script use this to open that;

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.

Notes;

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.

Edit;

Now, we have some spells and usercommands will have to use that spell variables after then show us some combinations. For doing this, we have to import your first file to usercommands script.It's going to like this;

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 ;)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM