如何使用pyqt询问sudo的密码?

时间:2016-02-05 19:27:15

标签: python-3.x pyqt5

cmd = subprocess.run(["sudo", "ovs-vsctl", "list-br"], stdout=subprocess.PIPE, universal_newlines=True)

这是我尝试运行时显示的错误消息。

sudo: no tty present and no askpass program specified

这是我正在尝试的PyQt5应用程序。所以我想知道如何在这个GUI应用程序中询问管理员密码?

这是我的代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os, sys, design, subprocess
from PyQt5 import QtCore, QtGui, QtWidgets


class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.button1.clicked.connect(self.browse_folder)

    def browse_folder(self):
        self.listWidget.clear()

        cmd = subprocess.run(["sudo", "ovs-vsctl", "list-br"], stdout=subprocess.PIPE, universal_newlines=True)
        bridges = cmd.stdout.split('\n')

        if bridges:
            for bridge in bridges:
                self.listWidget.addItem(bridge)

def main():
    app = QtWidgets.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

0 个答案:

没有答案