我无法在python中对根目录进行身份验证

我无法在python中对根目录进行身份验证,python,python-3.x,linux,bash,tkinter,Python,Python 3.x,Linux,Bash,Tkinter,我有一个小程序来更新linux系统。我想在程序启动时要求我在GUI菜单中输入root密码“运行程序需要身份验证”。我试着把它换成sudo。当我运行程序时,出现以下错误 请问有人能帮忙解决吗??? Script not started as root. Running sudo.. Traceback (most recent call last): File "/home/user/Python/upgrade_test.py", line 17, in <modu

我有一个小程序来更新linux系统。我想在程序启动时要求我在GUI菜单中输入root密码“运行程序需要身份验证”。我试着把它换成sudo。当我运行程序时,出现以下错误

请问有人能帮忙解决吗???

Script not started as root. Running sudo..
Traceback (most recent call last):
  File "/home/user/Python/upgrade_test.py", line 17, in <module>
    root = Tk()
  File "/usr/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
Running. Your euid is 0
添加此行:

os.environ['DISPLAY'] = ':0'

man-pkexec

The environment that PROGRAM will run it, will be set to a minimal known and safe
environment in order to avoid injecting code through LD_LIBRARY_PATH or similar
mechanisms. In addition the PKEXEC_UID environment variable is set to the user id of the 
process invoking pkexec. As a result, pkexec will not allow you to run X11 applications as
another user since the $DISPLAY and $XAUTHORITY environment variables are not set. These
two variables will be retained if the org.freedesktop.policykit.exec.allow_gui annotation
on an action is set to a nonempty value; this is discouraged, though, and should only be
used for legacy programs.

所以你需要设置
org.freedesktop.policykit.exec.allow_gui annotation
annotation

我写了这行,但不起作用!还有相同的消息,您是否可以在不使用pkexec的情况下运行脚本,即除去import语句之外的
root=Tk()
之前的行?是的,它工作正常,没有pkexec的内容。@acw1668您有桌子吗?你想分享我的程序来检查它吗?它不工作。如果我运行pkexec,程序会询问我密码,然后运行程序。我的linux机器中的pkexec工作正常!
The environment that PROGRAM will run it, will be set to a minimal known and safe
environment in order to avoid injecting code through LD_LIBRARY_PATH or similar
mechanisms. In addition the PKEXEC_UID environment variable is set to the user id of the 
process invoking pkexec. As a result, pkexec will not allow you to run X11 applications as
another user since the $DISPLAY and $XAUTHORITY environment variables are not set. These
two variables will be retained if the org.freedesktop.policykit.exec.allow_gui annotation
on an action is set to a nonempty value; this is discouraged, though, and should only be
used for legacy programs.