System theme icons and PyQt4 - python

I'm writing a basic program in python using the PyQt4 module. I'd like to be able to use my system theme's icons for things like the preference dialog's icon, but i have no idea how to do this. So my question is, how do you get the location of an icon, but make sure it changes with the system's icon theme? If it matters, i'm developing this under ubuntu 9.04, so i am using the gnome desktop.

Unfortunately, It appears that Qt does not support getting icons for a specific theme. There are ways to do this for both KDE and Gnome.
The KDE way is quite elegant, which makes sense considering that Qt is KDE's toolkit. Instead of using the PyQt4.QtGui class QIcon, you instead use the PyKDE4.kdeui class KIcon. An example of this is:
from PyKDE4.kdeui import *
icon = KIcon("*The Icon Name*")
see the PyKDE documentation for this class, here.
One way to gain support for this for gnome is to use the python gtk package. It is not as nice as the kde way, but it works none the less. It can be used like this:
from PyQt4 import QtGui
from gtk import icon_theme_get_default
iconTheme = icon_theme_get_default()
iconInfo = iconTheme.lookup_icon("*The Icon Name*", *Int of the icon size*, 0)
icon = QtGui.QIcon(iconInfo.get_filename())
See the documentation for the Icon Theme class and Icon Info class.
EDIT: thanks for the correction CesarB

Use the PyKDE4 KIcon class:
http://api.kde.org/pykde-4.2-api/kdeui/KIcon.html

I spent a decent amount of researching this myself not long ago, and my conclusion was that, unfortunately, Qt doesn't provide this functionality in a cross-platform fashion. Ideally the QIcon class would have defaults for file open, save, '+', '-', preferences, etc, but considering it doesn't you'll have to grab the appropriate icon for your desktop environment.

Related

Qt Designer's preview ui is not the same when it running in python

This is the preview
This is how it looks when it running in python
Qt handles various styles that are enabled depending on various flags such as environment variables, and Qt Designer probably uses some styles by default, so the style shown by the preview may be different from the application when it is run.
Analyzing the style it seems to me that Qt Designer uses the "fusion" style, so the solution is to add:
app.setStyle("fusion")
If you want to visualize your design with other styles from Qt Designer you must execute: Form-> Preview in -> style
If you want to know all the styles available in your installation you can run the following:
from PySide2 import QtWidgets
print(QtWidgets.QStyleFactory.keys())
In my case I get the following:
['Breeze', 'bb10dark', 'bb10bright', 'cleanlooks', 'gtk2', 'cde', 'motif', 'plastique', 'Oxygen', 'Windows', 'Fusion']

Best Qt Widget to use for properties window... in PySide?

This question is the exact same as this one except that I'm looking for such a widget to be used in PySide. Anyone knows some code out there that provide easy-to-reuse property editor widget?
There is the proposal of using QTreeView or QTableView to build such a widget that is an option with PySide, but this is not a straight forward solution...
I don't know if you would be OK with this, but if you are happy to add the whole of pyqtgraph as a dependency, then you might want to try using pyqtgraph's ParameterTree.
There is a pretty comprehensive set of examples, just install pyqtgraph and then run:
import pyqtgraph.examples
pyqtgraph.examples.run()
Launch the ParameterTree example!

Create a little python applet in a linux task bar

I would like to create for my linux task bar a little applet in python, which simply display an icon that could change of color, and when we click on it, which show a simple information menu.
But I have no idea about the resources I could use to perform it.
I started with gtk and in particular I found the gtk.status_icon_new_from_file to display an svg icon. But I can't dynamically change the icon's color through the svg. I have to modify the colors in my file, and then to reload it. Is there a way to give directly the data of the icon rather than the name of it ?
Besides, I don't know where to continue then to code my menu. Any idea ?
Finally, is it possible to some text above an icon ?
Edit : I would like to have solution about a generic taskbar, since I'm using tint2 (with openbox) under archlinux. So I need solutions compatible with the freedesktop recommendations and not specific to Gnome or Ubuntu. Maybe solutions with a xfce4 desktop.
Status icon is most generic solution, but legacy.
Appindicator was designed for ubuntu to easy keep icon and menu in taskbar.
There is examples in several languages
http://developer.ubuntu.com/resources/technologies/application-indicators/
What is your Desktop Shell? Each shell has a better way to keep applet on taskbar. Some of these solutions has compatibility with other shells (appindicator has support in KDE, Gnome2, Gnome Shell and maybe others).

Displaying icon in mac statusbar with python

I have a script that just returns true/false depending on some conditions, and I need to display this status in the status bar/some topmost window, the script is written in python.
I tried to find a way to display a status bar icon with python but seems all existing wrappers are outdated. I feel that it is not very good solution to create a native app just for displaying an icon and control it with my script.
I thought about using GeekTool for mac, but it can display stuff only on the desktop. Since the desktop is hidden with some windows all the time, that's not very good solution.
Any ideas?
Have found out that
import objc, from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
solves my problem

Cross-platform Python GUI suitable for taskbar (Win) and menubar (mac) functionality?

I am fairly new to Python programming, and completely new to cross-platform GUI building (only previous GUI experience is through visual basic and Java).
I've written some python code to screen-scrape data from a website, and now I want to build a GUI that will reside in the Mac OS X menubar, and in Window's task bar (i.e., the system tray).
The most useful general page on cross-plaform Python GUIs for me was this one (despite its name indication Window GUIs). And some stackoverflow questions came in useful as well (especially this one, and the accepted answer of this one about splitting up the GUI and cli code).
I think I will go for either wxPython or QT because I want the GUI to look as native as possible.
However, as I've said the fairly simple GUI will mainly live in the taskbar/menubar.
Should this influence my decision?
Here's an example for PyQt. This works for me on MacOS X; I haven't tried it on other platforms. Note that the QSystemTrayIcon class will raise exceptions if it doesn't have an icon – I grabbed the RSS feed svg from Wiki commons for my icon.svg (but you can give QIcon a PNG directly and not mess around with QtSvg).
import PyQt4
from PyQt4 import QtCore, QtGui, QtSvg
app = QtGui.QApplication([])
i = QtGui.QSystemTrayIcon()
m = QtGui.QMenu()
def quitCB():
QtGui.QApplication.quit()
def aboutToShowCB():
print 'about to show'
m.addAction('Quit', quitCB)
QtCore.QObject.connect(m, QtCore.SIGNAL('aboutToShow()'), aboutToShowCB)
i.setContextMenu(m)
svg = QtSvg.QSvgRenderer('icon.svg')
if not svg.isValid():
raise RuntimeError('bad SVG')
pm = QtGui.QPixmap(16, 16)
painter = QtGui.QPainter(pm)
svg.render(painter)
icon = QtGui.QIcon(pm)
i.setIcon(icon)
i.show()
app.exec_()
del painter, pm, svg # avoid the paint device getting
del i, icon # deleted before the painter
del app
See this related SO answer on how to accomplish Windows system tray/OS X menu bar functionality in wxPython.

Categories

Resources