I'm reading a tutorial on PySide and I was thinking , do I need to find my own icons for every thing or is there some way to use some built in icons . That way I wouldn't need to find an entire new set of icons if I want my little gui to run on another desktop environment .
我正在阅读关于PySide的教程,我在想,我是否需要为每件事找到自己的图标,或者是否有某种方法可以使用一些内置图标。这样,如果我想让我的小gui在另一个桌面环境中运行,我就不需要找到一整套新的图标。
7 个解决方案
#1
33
What you need is Pyside QIcon.fromTheme function. Basicaly it creates QIcon object with needed icon from current system theme.
您需要的是Pyside QIcon.fromTheme功能。 Basicaly它使用当前系统主题中的所需图标创建QIcon对象。
Usage:
undoicon = QIcon.fromTheme("edit-undo")
undoicon = QIcon.fromTheme(“edit-undo”)
"edit undo" - name of the icon "type"/"function" can be found here
“编辑撤消” - 可在此处找到图标“类型”/“功能”的名称
This works on X11 systems, for MacOSX and Windows check QIcon documentation QIcon.fromTheme
这适用于X11系统,对于MacOSX和Windows,请查看QIcon文档QIcon.fromTheme
Edit Inserting this from the website, since last time it was a broken link.
编辑从网站上插入此内容,因为上次它是一个断开的链接。
static PySide.QtGui.QIcon.fromTheme(name[, fallback=QIcon()])
static PySide.QtGui.QIcon.fromTheme(name [,fallback = QIcon()])
Parameters:
- name – unicode
名字 - unicode
- fallback – PySide.QtGui.QIcon
后备 - PySide.QtGui.QIcon
Return type:
Returns the PySide.QtGui.QIcon corresponding to name in the current icon theme. If no such icon is found in the current theme fallback is returned instead.
返回与当前图标主题中的name对应的PySide.QtGui.QIcon。如果在当前主题中找不到这样的图标,则返回后退。
The latest version of the freedesktop icon specification and naming specification can be obtained here:
可以在此处获取最新版本的freedesktop图标规范和命名规范:
- http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
- http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
To fetch an icon from the current icon theme:
要从当前图标主题获取图标:
undoicon = QIcon.fromTheme("edit-undo")
Or if you want to provide a guaranteed fallback for platforms that do not support theme icons, you can use the second argument:
或者,如果要为不支持主题图标的平台提供有保证的回退,可以使用第二个参数:
undoicon = QIcon.fromTheme("edit-undo", QIcon(":/undo.png"))
Note
By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of your PySide.QtGui.QIcon.themeSearchPaths() and set the appropriate PySide.QtGui.QIcon.themeName() .
默认情况下,只有X11支持主题图标。要在Mac和Windows上使用主题图标,您必须在PySide.QtGui.QIcon.themeSearchPaths()中捆绑一个兼容主题,并设置相应的PySide.QtGui.QIcon.themeName()。
See also
#2
7
There is another way to access some of the standard builtin icons in PyQt/PySide using the standard pixmap in the default style. For example, the following creates an icon for opening a file:
还有另一种方法可以使用默认样式的标准像素图访问PyQt / PySide中的一些标准内置图标。例如,以下内容创建一个用于打开文件的图标:
self.style().standardIcon(QtGui.QStyle.SP_DialogOpenButton)
For the full list of standard pixmaps, see:
有关标准像素映射的完整列表,请参阅:
#3
6
I don't think this is specific to any binding, you can check the qt general documentation
我不认为这是特定于任何绑定,你可以检查qt一般文档
看看这里和这里
#4
2
I keep having trouble finding images of the standard icons, so for future reference: http://nukesaq88.hatenablog.com/entry/2013/04/12/005525
我一直无法找到标准图标的图像,以便将来参考:http://nukesaq88.hatenablog.com/entry/2013/04/12/005525
And the code I'm using (PyQt4, Pyside probably similar):
我正在使用的代码(PyQt4,Pyside可能类似):
# In method of QMainWindow subclass
stdicon = self.style().standardIcon
style = QtGui.QStyle
reload_foo = QtGui.QAction(stdicon(style.SP_BrowserReload), '&Reload', self)
It would be nice if the (obviously auto-generated) Qt docs had pictures in the standardIcons enum table...
如果(显然是自动生成的)Qt文档在standardIcons枚举表中有图片会很好...
#5
1
In PyQt5
, here's a simple example of creating a push button with the play icon:
在PyQt5中,这是一个使用播放图标创建按钮的简单示例:
play_button = QtGui.QPushButton('Play video')
play_button.setIcon(QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_MediaPlay))
The Qt5 documentation provides a list of the possible SP ("Standard Pixmap") icons. See enum QStyle::StandardPixmap
here: http://doc.qt.io/qt-5/qstyle.html
Qt5文档提供了可能的SP(“标准Pixmap”)图标列表。请参阅枚举QStyle :: StandardPixmap:http://doc.qt.io/qt-5/qstyle.html
#6
1
Another PyQt5 example using the standard icons (eqzx's answer didn't work for me):
使用标准图标的另一个PyQt5示例(eqzx的答案对我不起作用):
from PyQt5.QtWidgets import QApplication, QStyle
from PyQt5.QtGui import QIcon
desktop_icon = QIcon(QApplication.style().standardIcon(QStyle.SP_DesktopIcon)
#7
0
in PyQt, the window icon is by default the Qt logo. I think you will have to find your own icons for things inside the gui.
在PyQt中,窗口图标默认为Qt徽标。我想你必须在gui内找到你自己的图标。
#1
33
What you need is Pyside QIcon.fromTheme function. Basicaly it creates QIcon object with needed icon from current system theme.
您需要的是Pyside QIcon.fromTheme功能。 Basicaly它使用当前系统主题中的所需图标创建QIcon对象。
Usage:
undoicon = QIcon.fromTheme("edit-undo")
undoicon = QIcon.fromTheme(“edit-undo”)
"edit undo" - name of the icon "type"/"function" can be found here
“编辑撤消” - 可在此处找到图标“类型”/“功能”的名称
This works on X11 systems, for MacOSX and Windows check QIcon documentation QIcon.fromTheme
这适用于X11系统,对于MacOSX和Windows,请查看QIcon文档QIcon.fromTheme
Edit Inserting this from the website, since last time it was a broken link.
编辑从网站上插入此内容,因为上次它是一个断开的链接。
static PySide.QtGui.QIcon.fromTheme(name[, fallback=QIcon()])
static PySide.QtGui.QIcon.fromTheme(name [,fallback = QIcon()])
Parameters:
- name – unicode
名字 - unicode
- fallback – PySide.QtGui.QIcon
后备 - PySide.QtGui.QIcon
Return type:
Returns the PySide.QtGui.QIcon corresponding to name in the current icon theme. If no such icon is found in the current theme fallback is returned instead.
返回与当前图标主题中的name对应的PySide.QtGui.QIcon。如果在当前主题中找不到这样的图标,则返回后退。
The latest version of the freedesktop icon specification and naming specification can be obtained here:
可以在此处获取最新版本的freedesktop图标规范和命名规范:
- http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
- http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
To fetch an icon from the current icon theme:
要从当前图标主题获取图标:
undoicon = QIcon.fromTheme("edit-undo")
Or if you want to provide a guaranteed fallback for platforms that do not support theme icons, you can use the second argument:
或者,如果要为不支持主题图标的平台提供有保证的回退,可以使用第二个参数:
undoicon = QIcon.fromTheme("edit-undo", QIcon(":/undo.png"))
Note
By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of your PySide.QtGui.QIcon.themeSearchPaths() and set the appropriate PySide.QtGui.QIcon.themeName() .
默认情况下,只有X11支持主题图标。要在Mac和Windows上使用主题图标,您必须在PySide.QtGui.QIcon.themeSearchPaths()中捆绑一个兼容主题,并设置相应的PySide.QtGui.QIcon.themeName()。
See also
#2
7
There is another way to access some of the standard builtin icons in PyQt/PySide using the standard pixmap in the default style. For example, the following creates an icon for opening a file:
还有另一种方法可以使用默认样式的标准像素图访问PyQt / PySide中的一些标准内置图标。例如,以下内容创建一个用于打开文件的图标:
self.style().standardIcon(QtGui.QStyle.SP_DialogOpenButton)
For the full list of standard pixmaps, see:
有关标准像素映射的完整列表,请参阅:
#3
6
I don't think this is specific to any binding, you can check the qt general documentation
我不认为这是特定于任何绑定,你可以检查qt一般文档
看看这里和这里
#4
2
I keep having trouble finding images of the standard icons, so for future reference: http://nukesaq88.hatenablog.com/entry/2013/04/12/005525
我一直无法找到标准图标的图像,以便将来参考:http://nukesaq88.hatenablog.com/entry/2013/04/12/005525
And the code I'm using (PyQt4, Pyside probably similar):
我正在使用的代码(PyQt4,Pyside可能类似):
# In method of QMainWindow subclass
stdicon = self.style().standardIcon
style = QtGui.QStyle
reload_foo = QtGui.QAction(stdicon(style.SP_BrowserReload), '&Reload', self)
It would be nice if the (obviously auto-generated) Qt docs had pictures in the standardIcons enum table...
如果(显然是自动生成的)Qt文档在standardIcons枚举表中有图片会很好...
#5
1
In PyQt5
, here's a simple example of creating a push button with the play icon:
在PyQt5中,这是一个使用播放图标创建按钮的简单示例:
play_button = QtGui.QPushButton('Play video')
play_button.setIcon(QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_MediaPlay))
The Qt5 documentation provides a list of the possible SP ("Standard Pixmap") icons. See enum QStyle::StandardPixmap
here: http://doc.qt.io/qt-5/qstyle.html
Qt5文档提供了可能的SP(“标准Pixmap”)图标列表。请参阅枚举QStyle :: StandardPixmap:http://doc.qt.io/qt-5/qstyle.html
#6
1
Another PyQt5 example using the standard icons (eqzx's answer didn't work for me):
使用标准图标的另一个PyQt5示例(eqzx的答案对我不起作用):
from PyQt5.QtWidgets import QApplication, QStyle
from PyQt5.QtGui import QIcon
desktop_icon = QIcon(QApplication.style().standardIcon(QStyle.SP_DesktopIcon)
#7
0
in PyQt, the window icon is by default the Qt logo. I think you will have to find your own icons for things inside the gui.
在PyQt中,窗口图标默认为Qt徽标。我想你必须在gui内找到你自己的图标。