因此,我正在创建一个小型python程序,该程序将使用subprocess.call()
函数启动其他程序。我想知道是否有任何方法可以在CLI中共同创建可单击的框,以帮助用户知道他/她有什么选项并使选择起来更加容易。
目前,我只是将input()
函数与if/elif/else
函数一起使用来确定要启动什么。我想将其转换为CLI上的可点击界面,因此除了点击之外无需其他输入。
这是我当前正在使用的。找不到有关此选项的任何材料,也许我不够努力。
import getpass
import subprocess
import os
OSPath = "/SYS64 3.7/"
full_path = os.getcwd()
CurrentPath = ""
dir_path = os.path.dirname(os.path.realpath(__file__))
answer = input("INPUT BOOT PARAMETER ")
if answer == "jdos":
subprocess.call(["python3",dir_path + OSPath + "bootthingy.py"])
subprocess.call(["python3",dir_path + OSPath + "jdosos.py"])
elif answer == "jdos.fboot":
subprocess.call(["python3",full_path + OSPath + "jdosos.py"])
elif answer == "jdos.admin":
user = getpass.getpass("username: ")
password = getpass.getpass("password: ")
bootcheck = 0
with open('userpass.txt', 'r') as file:
for line in file:
line = line.strip('\n')
login = line.split(',')
if login[0] == user and login[1] == password:
subprocess.call(["python3",full_path + OSPath + "jdososadmin.py"])
bootcheck = 1
if bootcheck == 0:
print("Incorrect user or password.")
elif "jdos.exp" in answer:
subprocess.call(["python3",full_path + OSPath + "jdosexperimentail.py"])
elif answer == "cancel":
print("")
else:
print("invalid")
您将有4个不同选项的4个可单击框,而不是用户在jdos
中键入input()
,它们看起来像这样:[jdos] [jdos.fboot] [jdos.admin] [jdos.exp]
,每个都运行subprocess.call()
(如果已点击)
我尝试使用npyscreen
包,但无法使其成功运行
这就是我使用提供的npyscreen
代码时的样子。
这是当我选择一个框并单击“确定”时发生的情况
Python program does not launch properly...
如您所见,程序确实运行了,它只是覆盖了最上方的文本,我无法在input('C:/SYS64 3.7> ')
函数中输入任何内容,我认为这可能是对npyscreen
执行的操作应用未关闭。我对npyscreen
还是陌生的,我不知道它是如何工作的。
答案 0 :(得分:0)
Npyscreen允许您创建控制台应用程序,您可以要求用户提供一些信息。
您可以继续使用我仅使用基本方法制作的该模板。我建议您阅读该库的文档。
import npyscreen
import os
import getpass
import subprocess
class mainform(npyscreen.ActionForm):
def create(self):
self.OSPath = "/SYS64 3.7/"
self.full_path = os.getcwd()
self.CurrentPath = ""
self.dir_path = os.path.dirname(os.path.realpath(__file__))
opt_values = ["jdos", "jdos.fboot", "jdos.admin", "jdos.exp"]
welcome_box = self.add(npyscreen.BoxTitle, max_height = 2, editable = False, value = "Welcome. Select BOOT PARAMETER")
self.answer = self.add(npyscreen.TitleSelectOne, max_height = 4, name = "Selections:", values = opt_values, scroll_exit = True)
def on_ok(self):
if self.answer.value[0] == 0:
subprocess.call(["python3", self.dir_path + self.OSPath + "bootthingy.py"])
subprocess.call(["python3", self.dir_path + self.OSPath + "jdosos.py"])
elif self.answer.value[0] == 1:
subprocess.call(["python3", self.full_path + self.OSPath + "jdosos.py"])
elif self.answer.value[0] == 2:
user = getpass.getpass("username: ")
password = getpass.getpass("password: ")
bootcheck = 0
with open('userpass.txt','r') as file:
for line in file:
line = line.strip('\n')
login = line.split(',')
if login[0] == user and login[1] == password:
subprocess.call(["python3", self.full_path + self.OSPath + "jdososadmin.py"])
bootcheck = 1
if bootcheck == 0:
print("Incorrect user or password.")
elif self.answer.value[0] == 3:
subprocess.call(["python3", self.full_path + self.OSPath + "jdosexperimentail.py"])
self.parentApp.setNextForm(None)
def on_cancel(self):
self.parentApp.setNextForm(None)
#Application starts here
class App(npyscreen.NPSAppManaged):
#Defining forms.
def onStart(self):
self.addForm('MAIN', mainform, name="name_here")
if __name__ == "__main__":
app = App().run()