[英]How to open a powershell terminal with one python script?
我正在開發一個基於文本的游戲,該游戲通過雙擊我的頂級腳本TopLevel.py
。 我正在尋找一種在此腳本中打開兩個終端的方法。 在一個終端中,將運行主要游戲,在游戲中會造成傷害並使用咒語等。在另一個終端中,我想顯示用戶可以鍵入的命令列表,我希望后者留在那里並直到游戲結束才關閉。 我不會向您展示整個頂級腳本(太長了),但這基本上是我想要實現的目標:
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()
函數作為參數傳遞,以在第二個終端中顯示其內容? 任何幫助將不勝感激 :)
您應該先將第二個程序轉換為.exe,該程序將顯示用戶命令。 假設您將其保存為usercommands.exe
,然后在主腳本中使用它打開該文件;
import subprocess
subprocess.Popen([r"usercommands.exe"],
creationflags=subprocess.CREATE_NEW_CONSOLE)
當您運行主文件時,它將打開另一個運行usercommands.exe
控制台窗口。
筆記;
它們必須位於同一目錄中,即您的主文件和.exe文件
usercommands.exe
必須在無限循環中顯示命令(while True
),因此它將顯示命令,直到用戶關閉它為止。
編輯;
現在,我們有了一些拼寫,並且在向我們展示了一些組合之后,用戶usercommands
將不得不使用該拼寫變量。 為此,我們必須將您的第一個文件導入到usercommands
腳本中。
usercommands.py
import mainfile #mainfile.py
if choose == mainfile.wizard:
print (something)
if choose == mainfile.subzero:
print (something)
該算法應該是這樣的,您可以將usercommands.py
轉換為.exe,然后它將按您想的那樣工作,我們現在無法嘗試;)
一個Python腳本可能會生成另一個將通過subprocess
與之並行運行的腳本。 然后將生成的進程可以顯示管道輸送到任何文本(通過正常print
報表或調用)一個簡單tkinter
基礎的窗口-看到errorwindow
模塊這個答案我的更多信息。
原始腳本的啟動方式可能並不重要。 我個人已經在從命令外殼程序以及從其他基於tkinter
的應用程序啟動的應用程序中使用過它-因此從powershell啟動應用程序應該很好。 我相信,該模塊的原始作者正在使用Linux或類似的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.