对于一个项目,我正在为ifconfig
Linux 编写一个 PyQt5 小部件。我的代码的某些部分需要管理员权限。例如,关闭一个接口。
这是一个 GUI 应用程序,用户必须使用 GUI 输入他们的 sudo 密码。我在网上搜索过,发现pkexec
允许授权用户以另一个用户身份执行程序。因此,我编写了.policy
所需的操作文件。我以前从未使用过 polkit,我可能错过了一些东西。
我可以使用没有错误地执行我的程序
python3 ifconfig_Logic.py
pkexec python3 ~/Desktop/GUI/ifconfig_Logic.py
但是如果我尝试使用身份验证窗口上的消息
执行我的程序
是
消息不应该是 Authentication is required to run ifconfigLogic而不是Authentication is required to install this project吗?
另外,我收到以下错误
Traceback (most recent call last):
File "/home/grandino/Desktop/GUI/ifconfig_Logic.py", line 9, in <module>
baseUIClass, baseUIWidget = uic.loadUiType("ifconfig.ui")
File "/usr/lib/python3/dist-packages/PyQt5/uic/__init__.py", line 199, in loadUiType
winfo = compiler.UICompiler().compileUi(uifile, code_string, from_imports,
File "/usr/lib/python3/dist-packages/PyQt5/uic/Compiler/compiler.py", line 111, in compileUi
w = self.parse(input_stream, resource_suffix)
File "/usr/lib/python3/dist-packages/PyQt5/uic/uiparser.py", line 1020, in parse
document = parse(filename)
File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 1202, in parse
tree.parse(source, parser)
File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 584, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig.ui'
ifconfig.ui
是ifconfig_Logic.py
用来导入界面的ui文件。我认为这个问题是因为ifconfig.ui
政策中没有添加,所以我添加了它。
这是ifconfig_Logic.py
from PyQt5 import QtCore, QtGui, QtWidgets, QtWidgets
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QProcess, Qt
from PyQt5 import uic
from PyQt5 import QtGui
import sys
baseUIClass, baseUIWidget = uic.loadUiType("ifconfig.ui")
class ifconfig_Logic(baseUIWidget,baseUIClass):
process = QProcess()
send_output = pyqtSignal(str)
def __init__(self, parent = None):
super(ifconfig_Logic,self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = ifconfig_Logic(None)
ui.show()
sys.exit(app.exec_())
政策文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<action id="com.ifconfigLogic">
<description>Run ifconfigLogic</description>
<message>Authentication is required to run the ifconfigLogic</message>
<defaults>
<allow_any>auth_admin_keep</allow_any>
<allow_inactive>auth_admin_keep</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/home/grandino/Desktop/GUI/ifconfig</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
</policyconfig>
我该如何解决这个问题?为什么我使用时文件似乎丢失了pkexec
?