方式一:通过QIcon 的方式对 控件按钮或者窗口标签添加图片

from PyQt5.QtGui import QIcon

fileButton = QtWidgets.QPushButton()
fileButton.setGeometry(QtCore.QRect(30, 60, 120, 120)) #设置按钮大小
 fileButton.setIcon(QIcon("图片路径")) #将图片路径通过setIcon的方式写入按钮中
fileButton.setIconSize(QtCore.QSize(96, 96)) #设置图片在按钮中的大小

方式二, 通过QPixmap 的方式对按钮设置图片

icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("图片路径"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
fileButton.setIcon(icon1)
fileButton.setIconSize(QtCore.QSize(96, 96))

设置光标移动到某个位置,光标显示的图片格式

w2 = QtWidgets.QWidget(window)
w2.move(250,150)
w2.resize(100,100)
w2.setStyleSheet('background-color:blue;')
#1. 创建光标的图像,参数为光标的相对位置(本文将光标存在工程目录的Cursor_png文件夹下)
pixmap = QPixmap('Cursor_png/01.png')
#2. 将光标对象传入鼠标对象中
cursor = QCursor(pixmap)
#3. 设置控件的光标
w2.setCursor(cursor)

 

posted on 2020-08-11 16:04  赏孤舟蓑笠  阅读(3725)  评论(0编辑  收藏  举报