I have a program in Python. I would like when the program starts for it to ask me to enter root password, in a GUI dialog. I run the program then it shows the below error:
couldn't connect to display ": 0.0"
If I delete all lines with pkexec
stuff inside upgrade.py
then it works perfectly. Also if I run in gnome-terminal
with the command sudo python3 /home/user/Python/upgrade.py
then it works too. Maybe it's a problem with sudo
? Exactly the same happens with GTK for Python.
Python code:
from tkinter import *
#!/usr/bin/python3
import os
import subprocess
import sys
euid = os.geteuid()
if euid != 0:
print ("Script not started as root. Running sudo..")
args = ['pkexec', sys.executable] + sys.argv + [os.environ]
# the next line replaces the currently-running process with the sudo
os.execlpe('pkexec', *args)
print ('Running. Your euid is', euid)
root = Tk()
root.title('Update system')
#root.geometry("290x100")
def button_add1():
callProcess = subprocess.Popen(['ls', '-la'], shell=True)
subprocess.run(['pkexec', 'ls', '-la'], check=True)
def button_add4():
root.destroy()
# Define Buttons
button_1 = Button(root, text="Upgrade system", padx=40, pady=20, command=button_add1)
button_4 = Button(root, text="Quit", padx=40, pady=20, command=button_add4)
# Put the buttons on the screen
button_1.grid(row=0, column=0)
button_4.grid(row=0, column=4)
root.mainloop()
Please I don't want like something this... Simplest method of asking user for password using graphical dialog in Python?
I would like authentication like programs such as Synaptic
, Gparted
, Bleachbit
I have already asked on Stackoverflow but they said, It is related on system policy configuration and nothing related to Python or tkinter. This question is now migrated back to Stack Overflow!
I tried my code on Debian and Linux Mint.