PyQt:如何使用 QCheckBox.isChecked() 方法
PyQt : How to use the QCheckBox.isChecked() method
The Below code is written to select the seats in an auditorium screen.. As I select the CheckBox i need the result to be displayed(Price of the Ticket) and also when I try to uncheck the CheckBox the result should be subtracted and displayed, Instead of this It's just keeps adding the result by 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("\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()
,其中 returns 无论对象发送信号:
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:请注意,我已经交换了 +/-
,因为我认为它们在您的示例代码中是错误的方式)
The Below code is written to select the seats in an auditorium screen.. As I select the CheckBox i need the result to be displayed(Price of the Ticket) and also when I try to uncheck the CheckBox the result should be subtracted and displayed, Instead of this It's just keeps adding the result by 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("\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()
,其中 returns 无论对象发送信号:
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:请注意,我已经交换了 +/-
,因为我认为它们在您的示例代码中是错误的方式)