Py学习  »  Python

python-pyqt5如何将gui的复选框视为单独的对象?

Michele Rava • 4 年前 • 586 次点击  

在研究qt和python的qt方面,我是个新手;关于pyqt5复选框对象,我有点麻烦。 在下面的代码中,当我选中一个复选框时,我得到一个重定向到连接函数的信号。 它正确地显示,如果框已被选中或取消选中,但似乎我的脚本混淆了哪个框已更改状态。 例如,如果我检查第一个对象,那么在第一个对象之后,我执行的所有检查都显示绑定到最早对象的名称值,就像它们实际上是同一个对象或具有相同的地址一样。相反,如果我选中并取消选中一个框,那么下一个选中的框将返回绑定到输出中特定复选框的正确名称。 我做错什么了?

这是课程代码:

class window_gui(QMainWindow):
def __init__(self):
    QtWidgets.QMainWindow.__init__(self)
    mainWindow = loadUi("utilities/mygui_gui.ui", self)
    self.setWindowTitle("window name")
    #checkBox per selezione uno: sensore sul checked yes or not
    mainWindow.checkOne.clicked.connect(self.selectBooks)
    # checkBox per selezione due: sensore sul checked yes or not
    mainWindow.checkTwo.toggled.connect(self.selectBooks)
    # checkBox per selezione tre: sensore sul checked yes or not
    mainWindow.checkThree.toggled.connect(self.selectBooks)
    # checkBox per selezione quattro: sensore sul checked yes or not
    mainWindow.checkFour.toggled.connect(self.selectBooks)
    # checkBox per selezione cinque: sensore sul checked yes or not
    mainWindow.checkFive.toggled.connect(self.selectBooks)

def selectBooks(self, toggle):
    if toggle == QtCore.Qt.Checked:
        if self.checkOne.isChecked():
            print(self.checkOne.text(), "1")
        elif self.checkTwo.isChecked():
            print(self.checkTwo.text(), "2")
        elif self.checkThree.isChecked():
            print(self.checkThree.text(), "3")
        elif self.checkFour.isChecked():
            print(self.checkFour.text(), "4")
        elif self.checkFive.isChecked():
            print(self.checkFive.text(), "5")
        else:
            print("error")
        print('Checked')
    else:
        print('Unchecked')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38234
 
586 次点击  
文章 [ 1 ]  |  最新文章 4 年前