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

问题描述:

下面的代码是为了选择礼堂屏幕中的座位而编写的。当我选择复选框时,我需要显示结果(票价)而且当我尝试取消选中该复选框,结果应减去显示,取而代之的是它只是不断通过180添加的结果...PyQt:如何使用QCheckBox.isChecked()方法

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("\t\t  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:请注意,我换了+/-,因为我认为他们是围绕在错误的道路你的示例代码)