dialog = gtk.FileChooserDialog("Open..",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)
I want to use the above code for file browsing- but when I give the following import
import pygtk
pygtk.require('2.0')
import gtk
I get Error: No module named 'pygtk'
Do I need to give pygtk as a module inside my application folder? Do say with steps. Thank you.
pygtk is the old and deprecated gtk Python api. Do not use it unless you have a legacy application. The correct way nowadays is to use GObject-introspection to use Gtk3 from Python. Which is also really awesome as it makes Python a first-class supported language, with no wrappers necessary.
from gi.repository import Gtk
dlg = Gtk.FileChooserDialog()
dlg.show()
You should look at this: http://rox.sourceforge.net/desktop/node/245.html
It shows some problems. Good luck on fixing it :).
Edit:
This has lots of info on pip install (windows): How do I install pip on Windows?
Related
I have some python applications using the Qt5 libs I want to port/use under Wayland/wlroots. I have searched for documentation everywhere but I haven't found really nothing, apart about compositors. With the Gtk3 libs very useful is the module gtk-layer-shell for wlroots, but I cannot use it with a pyqt5 window, obviously.
Any suggestions?
I'm trying to compile a program using distutils but I want to make sure that the user has Tkinter installed before installing my package.
My Google searches have failed to turn up any useful info, any clue how I'd do this?
Thanks,
Wayne
You can have a class that inherits from install and then do this:
from distutils.command.install import install
class Install(install):
def run(self):
if not check_dependencies():
# Tkinter was not installed, handle this here
install.run(self) # proceed with the installation
def check_dependencies():
try:
return __import__('Tkinter')
except ImportError:
return None
Unfortunately there is no standard cross-platform way to force Tkinter to be installed. Tkinter is part of the Python standard library so distributors who strip out Tkinter, or other standard library modules, and package them as optional entities are doing so using their own package management tools and, in general, you'd need to know the specific commands for each distribution. The best you can do in general is test for and fail gracefully if Tkinter (or tkinter in Python 3) is not importable, so something like:
import sys
try:
import Tkinter
except ImportError:
sys.exit("Tkinter not found")
Tkinter is in the python standard library, it should always be there.
I'm using python-webkit right now, but it's missing WebKitAuthenticationRequest, which I need. Is there a python webkit2 module, or maybe is there a way to do authentication in the old webkit?
You can get WebKit2 from the gi.repository module (also known as PyGObject).
Make sure you have the following packages:
python-gobject
gir1.2-webkit2-3.0
You should then be able to use WebKit2 -
from gi.repository import WebKit2
(Note that this module is for GTK+ 3).
I am trying to use rsvg in Python 3.2 but I keep getting an import error. I have installed all of the librsvg packages along with cairo. I cannot find anything online about what else to install to get it to work. I did hear that the rsvg module hasn't been updated since 2005 so is it just not compatible with Python 3.2, or is there something else I can try to install it? Alternatively, if rsvg does not work, does anyone have any suggestions for a simple way to display an SVG file through Python (basically just show the image)?
EDIT: The error I get is: 'ImportError: No module named rsvg'
This error does not show in python2
Thanks in advance
I experienced a lot of difficulty trying to figure out how to do this. I hope others find this answer and save themselves a lot of time!
For Python 3, Python language bindings for several libraries originally written in C (including GTK, Clutter, and librsvg) have been replaced by GObject introspection libraries, Python code which dynamically generates Python objects from C "objects".
In order to use librsvg on Python 3, first install the necessary GObject introspection libraries (in addition to the Python 3 Cairo library). For example, on Ubuntu 13.10:
sudo apt-get install gir1.2-rsvg-2.0 python3-cairo python-gi-cairo python3-gi
Then test it out with the following code.
#!/usr/bin/env python3
# `gi.repository` is a special Python package that dynamically generates objects
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
import cairo
INPUTFILE = 'tiger.svg'
if __name__ == '__main__':
# create the cairo context
surface = cairo.SVGSurface('myoutput.svg', 580, 530)
context = cairo.Context(surface)
# use rsvg to render the cairo context
handle = Rsvg.Handle()
svg = handle.new_from_file(INPUTFILE)
svg.render_cairo(context)
In order to implement this for your project,
change cairo.SVGSurface to be whatever surface you are going to draw on, and
modify the value of INPUTFILE to be the name of the SVG file you wish to render.
I'm trying to compile a program using distutils but I want to make sure that the user has Tkinter installed before installing my package.
My Google searches have failed to turn up any useful info, any clue how I'd do this?
Thanks,
Wayne
You can have a class that inherits from install and then do this:
from distutils.command.install import install
class Install(install):
def run(self):
if not check_dependencies():
# Tkinter was not installed, handle this here
install.run(self) # proceed with the installation
def check_dependencies():
try:
return __import__('Tkinter')
except ImportError:
return None
Unfortunately there is no standard cross-platform way to force Tkinter to be installed. Tkinter is part of the Python standard library so distributors who strip out Tkinter, or other standard library modules, and package them as optional entities are doing so using their own package management tools and, in general, you'd need to know the specific commands for each distribution. The best you can do in general is test for and fail gracefully if Tkinter (or tkinter in Python 3) is not importable, so something like:
import sys
try:
import Tkinter
except ImportError:
sys.exit("Tkinter not found")
Tkinter is in the python standard library, it should always be there.