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?
Ubuntu – How to call a dialog for authentication
application-developmentpythonSecurity
Related Solutions
Ways to communicate from an embedded WebKit widget to the controlling Python program
Gtk or Qt:
- set
window.status
from JavaScript; trap the corresponding event in Python - set
document.title
from JavaScript; trap the corresponding event in Python - redirect the page to a custom URL (say,
x-my-app:thing1/thing2/thing3
); trapnavigation-policy-decision-requested
event in Python and look at the URL that's being navigated to, and deal with it if it's your custom URL scheme
Qt only:
- use
frame.addToJavaScriptWindowObject
to add a (specifically designed) Python object to the JavaScript global namespace; call methods on it from JavaScript. (See http://pysnippet.blogspot.com/2010/01/calling-python-from-javascript-in-pyqts.html for an example.)
Methods which theoretically should work but in practice don't, very well
- Fire a custom DOM event on an HTML element (which would include the document object) from JavaScript; trap that event in Python by navigating the WebKit DOM from Python and listening for events. This works, but I can't find a way of having Python be able to read custom data from the event, which means that you can't pass information other than the event having fired.
Ways to communicate from Python to JavaScript
- use
webview.execute_script(js_code)
. Note that if you're passing variable values in with the JS, it's a good idea to JSON encode them; that way you can worry a bit less about escaping, and JS can read JSON natively
Here's some example Gtk code:
from gi.repository import Gtk,WebKit
import json
w = Gtk.Window()
v = WebKit.WebView()
sw = Gtk.ScrolledWindow()
w.add(sw)
sw.add(v)
w.set_size_request(400,300)
w.connect("destroy", lambda q: Gtk.main_quit())
def window_title_change(v, param):
if not v.get_title():
return
if v.get_title().startswith("msgtopython:::"):
message = v.get_title().split(":::",1)[1]
# Now, send a message back to JavaScript
return_message = "You chose '%s'. How interesting." % message
v.execute_script("jscallback(%s)" % json.dumps(return_message))
v.connect("notify::title", window_title_change)
v.load_html_string("""<!doctype html>
<html>
<head>
<title>A demo</title>
<style>
body { font-family: Ubuntu, sans-serif; }
h1 { font-size: 1.3em; }
</style>
<body>
<h1>A tiny JavaScript demonstration</h1>
<form>
<p>What's your favourite thing about Jono? <select>
<option>------choose one------</option>
<option>his beard</option>
<option>his infectious sense of humour</option>
<option>his infectious diseases</option>
<option>his guitar ability</option>
<option>his wife</option>
</select></p>
</form>
<p id="out"></p>
<script>
document.querySelector("select").addEventListener("change", function() {
var chosenOption = this.options[this.selectedIndex].text;
// Now send that text back to Python by setting the title
document.title = "msgtopython:::" + chosenOption;
}, false);
function jscallback(msg) {
document.getElementById("out").innerHTML = msg;
}
</script>
</body>
</html>
""", "file:///")
w.show_all()
Gtk.main()
If you're using python, python-aptdaemon-gtk
is probably the way to go. For a good example of how to do this, look at /usr/share/doc/python-aptdaemon.gtk3widgets/examples/gtk3-demo.py.gz
You get nice install prompts like:
In the case of using C++, you could could use the AptDaemon D-Bus interface, but I don't know of any examples and you wouldn't get the widgets for free.
The D-Bus API of AptDaemon is documented here: http://packages.python.org/aptdaemon/dbus.html
I can't find much in the way of C++ DBus tutorials on the web, so your best bet is probably the documentation and examples in the libdbus-c++-doc package.
Best Answer
There are two different Q&As at stackoverflow answering your question: here and here.
Both answers use the command
gksudo
(already mentioned by @khamer). If you rungksudo command
(also without python) it will basically do whatsudo
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
run.py
:And a file
create.py
:Then run
python run.py
and after you enter your password a file owned by root will be created. If you runpython create.py
it will be owned by you (the file should not exist before running the script).