我一直在寻找解决我的问题的答案,但一直不吉利。我希望得到与本地python一起工作的答案,希望能够变得简单。Python3 Tkinter - 写入输入(y)到控制台的子进程
我的问题是我在我的tkinter应用程序中使用子进程,但其中一个命令要求您写入Y/N以确保您要继续执行此操作。
因此,我正在寻找一种方法在此类消息出现时将y写入终端: 您确定要继续吗? (y/N)
我试过运行subprocess.run(“y”),但似乎没有工作。
我在Debian Linux上测试这个,并调用询问是否要继续的命令是subprocess.getoutput(),以便我可以检查错误。
CODE
class RemovePublicKeyDialog:
def __init__(self, parent):
top = self.top = Toplevel(parent)
Label(top, text="Who to remove?").pack()
self.e = Entry(top)
self.e.pack(padx=5)
b = Button(top, text="REMOVE", command=self.ok)
b.pack(pady=5)
def ok(self):
#print("value is " + self.e.get())
key = self.e.get()
cmd = subprocess.getoutput("gpg --delete-keys " + key)
print(cmd)
if ("key \"" + key + "\" not found" in cmd):
messagebox.showerror("Error", "No such public key.")
elif ("Delete this key from keyring?" in cmd):
#subprocess.run("echo 'y'")
messagebox.showinfo("Success", "Public key \"" + key + "\" deleted from keyring.")
else:
messagebox.showerror("Error", "Unknown error, did you input a key?")
self.top.destroy()
这是“主”的代码,一切的作品,但它只是我需要输入y以让它继续。
根据命令,您可能会提供一个标志,导致它跳过用户的确认信息,例如'--no-confirm'。 –
你只是想弹出一个信息询问'是'和'否',并根据哪一个按下来采取行动? –
对于某些命令,-y标志可以执行所需的操作,例如:apt-get -y install [软件包名称]。检查您的命令手册(man命令) –