Saturday, December 13, 2014

PyQt (5): QIcon & QPixmap

QIcon is used to create icons, and QPixmap is used to attach image on widget.

Case 1: 
touch image on a button.

Example:
from PyQt4 import QtGui, QtCore

# create icon and load image
icon = QtGui.QIcon('C:\icons\myIcon.png')
# create a button
button = QtGui.QPushButton()
# set icon to button
button.setIcon(icon)
# set the size of the icon
button.setIconSize(QtCore.QSize(200,200))
# show the button
button.show()
Case 2: 
set window icon.

Example:
from PyQt4 import QtGui, QtCore

# create icon and load image
icon = QtGui.QIcon('C:\icons\myIcon.png')
# create a widget
widget = QtGui.QWidget()
# set the icon as widget window icon
widget.setWindowIcon(icon)
# show the widget
widget.show()
Case 3: 
touch image to widget.

Example:
from PyQt4 import QtGui, QtCore

# create label widget, which is used to carry the image
label = QtGui.QLabel()
# create QPixmap and load the image
myPixmap = QtGui.QPixmap('C:\icons\myIcon.png')
# set pixmap to the label widget
label.setPixmap(myPixmap)
# show the widget
label.show()

No comments:

Post a Comment