I am writing a gui program using wx module in python. In program whenever I am accessing a folder which requires root privilege then it asks password at terminal but I want to display the dialog to user to enter the password and authenticate him. How to call that dialog and authenticate the user in ubuntu?
Both answers use the command
gksudo (already mentioned by @khamer). If you run
gksudo command (also without python) it will basically do what
sudo does, but with a graphical interface - i.e. ask the user for a password and then run the command as root if the user is in the sudoers file.
To implement the suggested solution from the linked answers in python, you can use something as shown by the following example:
Create a file
#!/usr/bin/python import subprocess subprocess.call(['gksudo','python create.py'])
And a file
#!/usr/bin/python # Create test file.. f = file("mytestfile", "w")
python run.py and after you enter your password a file owned by root will be created. If you run
python create.py it will be owned by you (the file should not exist before running the script).
While I'm sure there's a fancier python solution, the down and dirty version is to use gksudo. gksudo does the same thing sudo does, except it creates that dialog for authentication you mentioned instead of asking on the command line.