PyQt:如何使用QCheckBox.isChecked()方法



编写以下代码是为了选择礼堂屏幕中的座位。当我选择复选框时,我需要显示结果(门票价格),并且当我尝试取消选中复选框时,结果应该减去并显示,而不是这个它只是不断将结果添加 180 ...

import sys
from PyQt4 import QtGui, QtCore, QtSql
class SeatSel(QtGui.QWidget):
    def __init__(self, parent=None):
        super(SeatSel, self).__init__(parent)
        self.a = {}
        self.l = {}
        self.l1 = QtGui.QLabel("tt     Screen", self)
        self.l1.setStyleSheet("color : black")
        self.l1.move(210,600)
        self.l1.setAutoFillBackground(True)
        self.l1.setMinimumWidth(700)
        self.l3 = QtGui.QLabel("00              " ,self)
        self.l3.setStyleSheet("color : white")
        self.l3.move(1000, 500)
        self.l3.setMaximumWidth(300)
        self.display = QtGui.QLabel(self)
        self.display.setStyleSheet("color : white")
        self.display.move(800,800)
        for i in range(14):
            self.l[(i)] = QtGui.QLabel("%s" % chr(65+i), self)
            self.l[(i)].setStyleSheet("color : white")
            self.l[(i)].move(110, 103 + i * 32)
        for i in range(26):
            for j in range(14):
                self.a[(i,j)] = QtGui.QCheckBox(self)
                self.a[(i,j)].setMaximumSize(40,40)
                self.a[(i,j)].setCheckable(True)
                self.a[(i,j)].move(160+ (i-1) * 31,100 + j *32)
    def seat(self, m_name, x, y):
        self.l2 = QtGui.QLabel(m_name, self)
        self.l2.move(x,y)
        self.l2.setStyleSheet("color : white")

class DeadPool(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(QtGui.QMainWindow, self).__init__(parent)
        self.amount = 0
        self.count = 0
        self.setGeometry(50, 50, 1920, 1080)
        self.showMaximized()
        self.seat_sel("Conjuring", 640,40)
    def full_screen(self):
         if self.count == 0:
             self.count=1
             self.showFullScreen()
         else :
             self.count = 0
             self.showNormal()
    def exit(self):
        sys.exit()

    def seat_sel(self, m_name, x, y):
        self.seat = SeatSel(self)
        self.setWindowTitle("Select Seats")
        self.setCentralWidget(self.seat)
        self.seat.seat(m_name, x, y)
        for i in range(26):
            for j in range(14):
                self.seat.a[(i,j)].stateChanged.connect(lambda :
                                       self.seat_buttons(i, j))
        self.show()
    def seat_buttons(self, i, j):
        self.btn_toggle(self.seat.a[(i,j)])
    def btn_toggle(self, button):
        if button.isChecked():
            self.amount -= 180
            self.seat.l3.setNum(self.amount)
        if not button.isChecked():
            self.amount += 180
            self.seat.l3.setNum(self.amount)
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = DeadPool()
    sys.exit(app.exec_())

如何将stateChanged信号连接到lambda插槽时出现问题。当您在循环中连接信号时,变量不会被缓存,因此每个lambda最终都会得到相同的值(即循环退出时变量的最终值是什么)。有几种方法可以解决此问题。一种方法是使用默认参数显式缓存值:

    for i in range(26):
        for j in range(14):
            self.seat.a[(i,j)].stateChanged.connect(
                lambda state, i=i, j=j: self.seat_buttons(i, j))

另一种方法是使用functools.partial

from functools import partial
...
    for i in range(26):
        for j in range(14):
            self.seat.a[(i,j)].stateChanged.connect(
                partial(self.seat_buttons, i, j))

但在您的情况下更简单的是使用 sender() ,它返回发送信号的任何对象:

    for i in range(26):
        for j in range(14):
            self.seat.a[(i,j)].stateChanged.connect(self.btn_toggle)
...    
def btn_toggle(self, state):
    button = self.sender()
    if button.isChecked():
        self.amount += 180
        self.seat.l3.setNum(self.amount)
    else:
        self.amount -= 180
        self.seat.l3.setNum(self.amount)

(PS:请注意,我已经交换了+/-,因为我认为它们在您的示例代码中是错误的方式)

相关内容

  • 没有找到相关文章

最新更新