Python中的“图像为”复选框

2024-04-25 05:24:09 发布

您现在位置:Python中文网/ 问答频道 /正文

Python是否可以使用image作为复选框(在PyQT制作的GUI应用程序中)?例如,有一个蓝色圆圈的图像,当你点击它时,它变成红色。在


Tags: 图像image应用程序guipyqt复选框蓝色红色
1条回答
网友
1楼 · 发布于 2024-04-25 05:24:09

以下是解决此难题的方法:

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_()

相关问题 更多 >