我正在阅读一份关于PySide的教程,并思考,我是否需要为每个事物找到自己的图标,还是有一些内置图标可供使用。这样,如果我想让我的小GUI运行在另一个桌面环境中,我就不需要找一个全新的图标集。
我正在阅读一份关于PySide的教程,并思考,我是否需要为每个事物找到自己的图标,还是有一些内置图标可供使用。这样,如果我想让我的小GUI运行在另一个桌面环境中,我就不需要找一个全新的图标集。
undoicon = QIcon.fromTheme("edit-undo")
"edit-undo" - 图标的名称/函数可以在这里找到。static PySide.QtGui.QIcon.fromTheme(name[, fallback=QIcon()])
Parameters:
- name – unicode
- fallback – 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.
The latest version of the freedesktop icon specification and naming specification can be obtained here:
- 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() .
See also
使用默认样式中的标准 pixmap,可以另一种方法访问PyQt/PySide中一些标准内置的图标。例如,以下代码创建一个打开文件的图标:
self.style().standardIcon(QtGui.QStyle.SP_DialogOpenButton)
查看完整的标准像素图列表,请参见:
self
不是上述两者之一的情况下使用它时,“self”指的是Qt5中的“QtWidgets.QApplication”或Qt4中的“QtGui.QApplication”。请注意,这里的翻译已经尽量简明扼要且不改变原意。 - user136036SP_
图标太有限了,真是遗憾。我希望他们能够增加更多的图标到列表中。 - JasonPyQt5
中,以下是创建一个带有播放图标的按钮的简单示例:play_button = QtGui.QPushButton('Play video')
play_button.setIcon(QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_MediaPlay))
枚举QStyle :: StandardPixmap
:http://doc.qt.io/qt-5/qstyle.html
play_button.setIcon(qApp.style().standardIcon(QStyle.SP_MediaPlay))
,其中 from PyQt5.QtWidgets import qApp, QStyle
。 - analytical_prat我经常找不到标准图标的图片,所以为了以后方便查阅:http://nukesaq88.hatenablog.com/entry/2013/04/12/005525
我正在使用的代码(PyQt4,Pyside可能类似):
# In method of QMainWindow subclass
stdicon = self.style().standardIcon
style = QtGui.QStyle
reload_foo = QtGui.QAction(stdicon(style.SP_BrowserReload), '&Reload', self)
如果(显然是自动生成的)Qt文档中的standardIcons枚举表格中有图片那就太好了...
from PyQt5.QtWidgets import (QApplication, QGridLayout, QPushButton, QStyle, QWidget)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
icons = [
'SP_ArrowBack',
'SP_ArrowDown',
'SP_ArrowForward',
'SP_ArrowLeft',
'SP_ArrowRight',
'SP_ArrowUp',
'SP_BrowserReload',
'SP_BrowserStop',
'SP_CommandLink',
'SP_ComputerIcon',
'SP_DesktopIcon',
'SP_DialogAbortButton',
'SP_DialogApplyButton',
'SP_DialogCancelButton',
'SP_DialogCloseButton',
'SP_DialogDiscardButton',
'SP_DialogHelpButton',
'SP_DialogIgnoreButton',
'SP_DialogNoButton',
'SP_DialogNoToAllButton',
'SP_DialogOkButton',
'SP_DialogOpenButton',
'SP_DialogResetButton',
'SP_DialogRetryButton',
'SP_DialogSaveAllButton',
'SP_DialogSaveButton',
'SP_DialogYesButton',
'SP_DialogYesToAllButton',
'SP_DirClosedIcon',
'SP_DirHomeIcon',
'SP_DirIcon',
'SP_DirLinkIcon',
'SP_DirLinkOpenIcon',
'SP_DirOpenIcon',
'SP_DockWidgetCloseButton',
'SP_DriveCDIcon',
'SP_DriveDVDIcon',
'SP_DriveFDIcon',
'SP_DriveHDIcon',
'SP_DriveNetIcon',
'SP_FileDialogBack',
'SP_FileDialogContentsView',
'SP_FileDialogDetailedView',
'SP_FileDialogEnd',
'SP_FileDialogInfoView',
'SP_FileDialogListView',
'SP_FileDialogNewFolder',
'SP_FileDialogStart',
'SP_FileDialogToParent',
'SP_FileIcon',
'SP_FileLinkIcon',
'SP_LineEditClearButton',
'SP_MediaPause',
'SP_MediaPlay',
'SP_MediaSeekBackward',
'SP_MediaSeekForward',
'SP_MediaSkipBackward',
'SP_MediaSkipForward',
'SP_MediaStop',
'SP_MediaVolume',
'SP_MediaVolumeMuted',
'SP_MessageBoxCritical',
'SP_MessageBoxInformation',
'SP_MessageBoxQuestion',
'SP_MessageBoxWarning',
'SP_RestoreDefaultsButton',
'SP_TitleBarCloseButton',
'SP_TitleBarContextHelpButton',
'SP_TitleBarMaxButton',
'SP_TitleBarMenuButton',
'SP_TitleBarMinButton',
'SP_TitleBarNormalButton',
'SP_TitleBarShadeButton',
'SP_TitleBarUnshadeButton',
'SP_ToolBarHorizontalExtensionButton',
'SP_ToolBarVerticalExtensionButton',
'SP_TrashIcon',
'SP_VistaShield',
]
layout = QGridLayout()
for n, name in enumerate(icons):
btn = QPushButton(name)
pixmapi = getattr(QStyle.StandardPixmap, name)
icon = self.style().standardIcon(pixmapi)
btn.setIcon(icon)
layout.addWidget(btn, int(n / 4), n % 4)
self.setLayout(layout)
app = QApplication([])
w = Window()
w.show()
app.exec()