How do I open a powershell terminal with a single python script?

I am working on a text game that I run by double clicking on my top level script, viz TopLevel.py

. I am looking for a way to open two terminals in this script. In one terminal, the main game will run where damage is done and spells are used, etc. On another terminal, I would like to display a list of commands that the user can enter and I want the latter to stay there and not close until the game ends. I'm not going to show you the entire top-level script (it's 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 to open another terminal in this script and pass a function displayCommands()

as a parameter to display its content in a second terminal? Any help would be appreciated :)

0


source to share


2 answers


Perhaps, for one Python script, another will appear that will work in parallel with it through subprocess

. The created process can then display any text passed over it (via normal print

statements or calls) in a simple tkinter

window - see the module errorwindow

in this answer for more information.



It probably doesn't matter how the original script starts. I've personally used it from both those launched from the command shell and other based applications tkinter

, so getting started with powershell should be fine. I believe the original author was using Linux or something similar.

0


source


First you have to convert your second program to an .exe that will display the custom commands. Let's say you saved it as usercommands.exe

, then in your main script, use this to open this;

import subprocess
subprocess.Popen([r"usercommands.exe"],
                 creationflags=subprocess.CREATE_NEW_CONSOLE)

      

It will open another console window that will be launched usercommands.exe

when the main file starts.

Note;

They should be in the same directory, your main file and the .exe file

usercommands.exe

should show commands in an infinite loop ( while True

), so this is to display commands until the user closes it.



Edit

Now we have some spells, but we usercommands

will have to use variable spells after we show us some combinations. To do this, we have to import your first file into usercommands

script. It will be 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 convert usercommands.py

to .exe, then it will work the way you want it, I think we cannot now, before you try it;)

0


source







All Articles