在PyQt5中,可以使用QPushButton小部件来创建一个带有图像的按钮。要在pushbutton小工具中添加图像,可以按照以下步骤进行操作:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
app = QApplication([])
window = QWidget()
button = QPushButton(window)
button.setIcon(QIcon('path_to_image.png'))
在上述代码中,'path_to_image.png'是图像文件的路径。你可以将图像文件放在与你的代码文件相同的目录中,或者提供图像文件的完整路径。
button.setGeometry(10, 10, 100, 30)
上述代码中的参数依次表示按钮的x坐标、y坐标、宽度和高度。
window.show()
app.exec_()
完整的代码示例:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
app = QApplication([])
window = QWidget()
button = QPushButton(window)
button.setIcon(QIcon('path_to_image.png'))
button.setGeometry(10, 10, 100, 30)
window.show()
app.exec_()
这样,你就可以在pushbutton小工具中添加图像了。请确保将'path_to_image.png'替换为你实际图像文件的路径。
没有搜到相关的结果
领取专属 10元无门槛券
手把手带您无忧上云