You are not logged in.

#1 2010-01-22 08:36:28

gnu_D
Member
From: Macedonia
Registered: 2007-10-21
Posts: 46

building python program with a gui identical with pyqt andpygtk-SOLVED

Hi,

I want to make my Python program to find which DE is in and use that gui toolkit i.e pygtk on GNOME, pyqt on KDE, but my problem is the theme icons, I need to get the current theme icon from kde when using pyqt, in the specification here: http://www.riverbankcomputing.co.uk/sta … qicon.html says there is a function named fromTheme in the QIcon widget, but when I use it in python says: 'QIcon' object has no attribute 'fromTheme, this is my test code:

icon = QtGui.QIcon().fromTheme("edit-undo")

and this is the full error:

Traceback (most recent call last):
  File "qt_guilogin.py", line 135, in <module>
    qtGui()
  File "qt_guilogin.py", line 22, in __init__
    self.loginWindow()
  File "qt_guilogin.py", line 52, in loginWindow
    QtGui.QIcon().fromTheme("edit-undo")
AttributeError: 'QIcon' object has no attribute 'fromTheme

This is the pygtk version of the full code:

        hbUPart = gtk.HBox()
        usrImg = gtk.Image()
        icon_theme = gtk.icon_theme_get_default() # Current theme instance        
        try:
            pixbuf = icon_theme.load_icon("user-info", 32, 0) # Some icon.
            usrImg.set_from_pixbuf(pixbuf) # We set the image widget to load icon form a pixbuf.
        except gobject.GError, exc: print "can't load icon", exc

        userLabel = gtk.Label("User")
        self.userEntry = gtk.Entry(20)
        self.userEntry.connect("activate", self.changer, self.userFill)
        hbUPart.pack_start(usrImg,    10)        # A nice icon.
        hbUPart.pack_start(userLabel, 10)        # A label which says user.
        hbUPart.pack_end(self.userEntry )        # And the entry to enter the user name.

So if the full code can't be converted to pyqt, what other options can I consider ?

What I believe is I can almost fully convert the code if I know the path of the current theme, so when I know that constant, I will learn then the Open Desktop naming standards, and I will take the full path of the icon.

I'm just asking here for a more standard and easier way, if no such thing exist in pyqt, than I'll do this manually big_smile.

Last edited by gnu_D (2010-01-24 08:06:13)


:: Python powered FOREVER ::

Offline

#2 2010-01-22 15:05:09

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

What about just using wxPython? I believe it has qt and gtk backends already so you don't even have to worry about it

Offline

#3 2010-01-22 16:41:19

lman
Member
From: CZ
Registered: 2007-12-18
Posts: 255

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

That's the first time I heard about a wxPython/wxWidgets qt backend. IMHO wxWidgets has only a gtk backend. Maybe I missed something, but I couldn't find anything about it on google.

Edit:
As for the gtk-qt same look. I think gtk enables you to use different theme/application (see chromium browser), so maybe you could use gtk-qt engine or whatever other solution. I'm not that familiar with qt, but I think there is an option, so qt apps look the same as gtk ones...

Last edited by lman (2010-01-22 16:47:13)

Offline

#4 2010-01-22 17:17:57

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

Sorry, I was under the impression that wx could use qt as a backend but it does appear that I am mistaken.

Offline

#5 2010-01-22 18:52:12

gnu_D
Member
From: Macedonia
Registered: 2007-10-21
Posts: 46

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

Grazz256 wrote:

What about just using wxPython? I believe it has qt and gtk backends already so you don't even have to worry about it

I thought on that, but I want to practice pyqt.


:: Python powered FOREVER ::

Offline

#6 2010-01-22 18:54:06

gnu_D
Member
From: Macedonia
Registered: 2007-10-21
Posts: 46

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

lman wrote:

That's the first time I heard about a wxPython/wxWidgets qt backend. IMHO wxWidgets has only a gtk backend. Maybe I missed something, but I couldn't find anything about it on google.

Edit:
As for the gtk-qt same look. I think gtk enables you to use different theme/application (see chromium browser), so maybe you could use gtk-qt engine or whatever other solution. I'm not that familiar with qt, but I think there is an option, so qt apps look the same as gtk ones...

The goal is to use the DE libs only, not to load gtk or qt lib if the opposite DE is on.


:: Python powered FOREVER ::

Offline

#7 2010-01-23 00:51:31

Grazz256
Member
Registered: 2009-06-28
Posts: 69

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

I've been doing a little research to try to make up for my mistake (actually more cause I'm bored) and no guarantees but I think your problem may be that the package is out of date. The documentation you reference is for pyQt 4.7, the version in extra is 4.6.2.

Offline

#8 2010-01-23 11:42:55

gnu_D
Member
From: Macedonia
Registered: 2007-10-21
Posts: 46

Re: building python program with a gui identical with pyqt andpygtk-SOLVED

Grazz256 wrote:

I've been doing a little research to try to make up for my mistake (actually more cause I'm bored) and no guarantees but I think your problem may be that the package is out of date. The documentation you reference is for pyQt 4.7, the version in extra is 4.6.2.

Someone else told me that yesterday, but I need than a workaround, I devised one, what I need is to get the full path of the current theme.
This is the idea, I grep-ed around the conf files and I dug this> ~/.kde4/share/config/kdeglobals, where the current icon theme is:

[Icons]
Theme=oxygen

with this code:

cd "~/.kde4/share/config/"
for a in *; do echo "$a:"; cat "$a" | grep -H "oxygen"; done

With the structure of the file I bet it's an ini file.
So, I made this little module:

#!/usr/bin/python
# -*- coding: utf-8 -*-

#       themeIconFetch.py
#
#       Copyright 2010 Damjan Dimitrioski <damjandimitrioski@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

from iniparse import INIConfig
from os import environ
from os.path import join
from os.path import exists
from os.path import isdir
from os.path import basename
from os.path import split
from os.path import splitext
from glob import glob

"""
 Usage: getIconPath()
 gct = getIconPath()
 sQuery = "face-wink"
 sSize = "32x32"
 ext = "png"
 print("Fullpath:\n%s"%gct.getIcon(sQuery, sSize, ext))
 Current Theme icons indexer and theme icon search tool.
 Can return the full path of some icon if the method getIcon is called.
"""
class getIconPath:
    iconsIndex = []
    def __init__(self):
        self.indexAll()

    # Usage: print (getIconsFolder("oxygen"))
    # themeFolder Which folder to search for, string.
    # debug True/False, to print debug info.
    def getIconsFolder(self, themeFolder, debug=False):
        """
        Discussion:
        what if the user has e.g: "oxygen" custom theme :D in .icons,
        then we should make the home icons folder to have priority.
        """
        msg="Icons are in '%s'."
        iconFolders  = ["/usr/share/icons/", join(environ.get("HOME"), ".icons")]
        if (debug):print (":: Get icons folder ::"); print (40*"-+")
        if exists(join(iconFolders[0], themeFolder)) and not exists(join(iconFolders[1], themeFolder)):
            if (debug):print (msg%iconFolders[0]); print (80*"=")
            return iconFolders[0]
        else:
            if (debug):print (msg%iconFolders[1]); print (80*"=")
            return iconFolders[1]

    # Usage: themePath = getCurrentTheme()
    # debug True/False, to print debug info.
    def getCurrentTheme(self, debug=False):
        kdeConfigDir = ".kde4/share/config/kdeglobals" # Currently hardcoded.
        if (debug):print (":: Current Theme grabber ::"); print (40*"-+")
        # Current home + path to kde4, works only with kde4.x
        themeIni = join(environ.get("HOME"), kdeConfigDir)
        if exists(themeIni):
            if (debug):print ("Our conf file '%s' exists."%themeIni)
            cfg = INIConfig(open(themeIni, 'r'))
            theme = cfg.Icons.theme
            if (debug):print ("Our current KDE4 theme is '%s'."%theme)
            themeFullPath = join(self.getIconsFolder("oxygen"), theme)
            if (debug):print ("The full path of the KDE4 theme is in '%s'."%themeFullPath); print (80*"=")
            return themeFullPath
        return False

    # Usage: indexAll()
    # debug True/False, to print debug info.
    def indexAll(self, debug=False):
        if (debug):print (":: Indexing service ::"); print (40*"-+")
        #themePath = getCurrentTheme(True)
        themePath = self.getCurrentTheme()
        folders = glob (join(themePath,  "*"))
        #print ("we are in: %s"%themePath)
        #print ("loop #1>")
        for folder in folders:
            if isdir(folder):
                for size in glob(join(folder,  "*")):
                    #print(size)
                    category = [glob (join(size,  "*"))] # category
                    self.iconsIndex.append(category)
        if (debug):print (80*"=")

    """
     Usage: getIcon(sQuery, sSize, ext)
     sQuery Input the name of the icon, string.
     sSize Input the size of the icon, string, possible values:
         128x128  16x16  22x22  256x256  32x32  42x42  48x48  64x64  8x8 scalable.
     debug True/False, to print debug info.
     bug: no scalable i.e svg, svgz support yet :(.
    """
    def getIcon(self, sQuery, sSize, ext, debug=False):
        if (debug):print (":: Icon search ::"); print (40*"-+")
        if (debug):print (":: We are going to find '%s' ::"%sQuery)
        for iconset in self.iconsIndex:
            iconsize = []
            for icon in iconset[0]:
                if sSize in icon:
                    pathonly = basename(split(icon)[0])
                    nameonly = splitext(basename(icon))[0]
                    extension = splitext(basename(icon))[1]
                    if "."+ext == extension:
                        #if sQuery in basename(icon): # find all possibilities.
                        #if basename(icon).startswith(sQuery): # if startswith some letters
                        if sQuery == nameonly: # must be that name.
                            #print ("Најдов: %s,\nкатегорија:%s\nсо целосен пат:\n%s"%(splitext(basename(icon))[0], basename(split(icon)[0]), icon))
                            if (debug): print ("Found: '%s'\n\tcategory:\n\t\t'%s'.\n\tfullpath:\n\t\t'%s'.\n\tSize:\n\t\t'%s'."%(nameonly+extension, pathonly, icon, sSize)); print (80*"=")
                            return icon
                #else: print ("Промашок: %s"%basename(icon))
                #else: print ("Miss: %s"%basename(icon))

"""
# Test cases
gct = getIconPath()
sQuery = "face-wink"
sSize = "32x32"
ext = "png"
print("Fullpath:\n%s"%gct.getIcon(sQuery, sSize, ext))

sQuery = "user-identity"
sSize = "32x32"
ext = "png"
print("Fullpath:\n%s"%gct.getIcon(sQuery, sSize, ext))
"""

That module finds the current theme and index all the icons, than let's you find an icon by name, returning it's full path.

This is the test pyqt gui I made:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from sys import argv
from sys import exit
from PyQt4 import QtCore, QtGui
from themeIconFetch import getIconPath

class qtGui(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.gct=getIconPath() # Theme index.
        icon = self.gct.getIcon("face-wink","16x16","png")
        if icon:
            winIcon = QtGui.QIcon(icon)
            self.setWindowIcon(winIcon)
        self.setWindowTitle(u"::\ Login /::")
        self.resize(400, 150)
        self.center()
        self.loginWindow()
    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

    def loginWindow(self):
        vbPole = QtGui.QHBoxLayout()
        hbKPole = QtGui.QHBoxLayout()

        # The image
        self.loginImg = QtGui.QLabel()
        iconp = self.gct.getIcon("user-identity","32x32","png")
        if iconp:
            icon = QtGui.QPixmap(iconp)
            self.loginImg.setPixmap(icon)

        # The label
        self.userLabel = QtGui.QLabel()
        self.userLabel.setText(u"User")

        # The input
        self.userInput = QtGui.QLineEdit()

        hbKPole.addWidget(self.loginImg)
        hbKPole.addWidget(self.userLabel)
        hbKPole.addWidget(self.userInput)

        vbPole.addLayout(hbKPole)

        self.setLayout(vbPole)
if __name__ == "__main__":
    app = QtGui.QApplication(argv)
    qb = qtGui()
    qb.show()
    exit(app.exec_())

Hope someone can improve my theme digger big_smile while still no news about pyQt 4.7 in extra.

Last edited by gnu_D (2010-01-23 11:49:32)


:: Python powered FOREVER ::

Offline

Board footer

Powered by FluxBB