Python中的图像作为复选框
Python是否可以使用图像作为复选框(在用PyQT制作的GUI应用程序中)?例如,有一个蓝色圆圈的图像,当您单击它时,它将变为红色。以下是此挑战的解决方案:Python中的图像作为复选框,python,python-3.x,pyqt,pyqt4,pyqt5,Python,Python 3.x,Pyqt,Pyqt4,Pyqt5,Python是否可以使用图像作为复选框(在用PyQT制作的GUI应用程序中)?例如,有一个蓝色圆圈的图像,当您单击它时,它将变为红色。以下是此挑战的解决方案: import sys from PyQt4 import QtCore from PyQt4.QtGui import * def chk(): global button, checked checked = not checked ic = icon1 if checked else icon2
import sys
from PyQt4 import QtCore
from PyQt4.QtGui import *
def chk():
global button, checked
checked = not checked
ic = icon1 if checked else icon2
button.setIcon(ic)
app = QApplication(sys.argv)
widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)
checked = True
button = QPushButton()
button.setFlat(True)
button.clicked.connect(chk)
button.setStyleSheet('border:none')
layout.addWidget(button)
icon1 = QIcon("off.png")
icon2 = QIcon("on.png")
button.setIcon(icon1)
button.setIconSize(QtCore.QSize(30, 30))
widget.show()
app.exec_()
是的,这是可能的。也许您应该尝试编写一个小示例(提示:对图像/按钮使用
QLabel
),然后在有更具体的问题时返回。