mardi 3 août 2021

Why doesn't Python QT QCheckBox state change when ticked?

I have a small project to automate a text file creation, and decided to use Python & QT Designer (I'm new to both) to present all the options to the user, so I can easily get it working on Windows and Mac.

I've hit an odd issue: whenever I click in a QCheckBox, although the tick appears and the connected function runs, the state never changes to isChecked == True.

None of the other complaints I've found seem to have a solution relevant to my code.

All suggestions welcome!

MAIN:

import configparser
import datetime
import os
import pathlib
import platform
import sys
#import builder_gui
from checkbox_gui import Ui_MainWindow
# from functools import partial
from pathlib import Path
from PyQt5 import QtWidgets
from PyQt5.QtCore import QTime

defaultSlides = {}
churchesFile = 'churches.ini'
codeVersion = '0.07A'

# Separating church config processing
embeddedFolders = ['040','070','080','090','120','150','160','170','190','210','220','250']
launchPath = ''
slideFolders = ['010','020','030','040','050','060','070','080','090',\
                '100','110','115','120','130','140','150','160','170',\
                '180','190','200','210','220','230','240','250','260']

# Define a class to setup the UI
class BuilderApp(QtWidgets.QWidget, Ui_MainWindow):
    def __init__(self,  parent=None):
        super().__init__(parent)
#        Ui_MainWindow.__init__(self)
        # Load the main form
#        self.lblCode.setText(codeVersion)
        print("?")
        self.setupUi(self)
        self.chk040.toggled.connect(lambda: self.embedToggled("040"))
        self.chk070.stateChanged.connect(lambda: self.embedToggled("070"))
        self.chk080.toggled.connect(lambda: self.embedToggled("080"))
        self.chk090.toggled.connect(lambda: self.embedToggled("090"))
        self.chk120.toggled.connect(lambda: self.embedToggled("120"))
        self.chk150.toggled.connect(lambda: self.embedToggled("150"))
        self.chk160.toggled.connect(lambda: self.embedToggled("160"))
        self.chk170.toggled.connect(lambda: self.embedToggled("170"))
        self.chk190.toggled.connect(lambda: self.embedToggled("190"))
        self.chk210.toggled.connect(lambda: self.embedToggled("210"))
        self.chk220.toggled.connect(lambda: self.embedToggled("220"))
        self.chk250.toggled.connect(lambda: self.embedToggled("250"))

        
    # Declare method to react to Embedded Music checkboxes
    def embedToggled(self, index):
        # Reprocess the affected combobox
#         prepCombo(index, Path(parentDir + '/' + eval('ui.cbx' + index + '.text()')), )
        print(index)
        print('isChecked: ', eval('ui.chk' + index + '.isChecked()'))
        print('checkState: ', eval('ui.chk' + index + '.checkState()'))
#        ui.prepCombo(index)

    

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QWidget()
    ui = BuilderApp()
    # main()
    ui.setupUi(MainWindow)
    ui.show()
    
    sys.exit(app.exec_())

GUI:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'P:\StFrancis\PowerPoint\000 Start\builder_gui_v0.02A.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.NonModal)
        MainWindow.resize(730, 870)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(730, 870))
        MainWindow.setMaximumSize(QtCore.QSize(730, 870))
        self.lblTitle = QtWidgets.QLabel(MainWindow)
        self.lblTitle.setGeometry(QtCore.QRect(30, 10, 671, 91))
        font = QtGui.QFont()
        font.setPointSize(48)
        self.lblTitle.setFont(font)
        self.lblTitle.setObjectName("lblTitle")
        self.cbxChurch = QtWidgets.QComboBox(MainWindow)
        self.cbxChurch.setGeometry(QtCore.QRect(250, 150, 101, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cbxChurch.setFont(font)
        self.cbxChurch.setObjectName("cbxChurch")
        self.cbxChurch.addItem("")
        self.cbxChurch.addItem("")
        self.datMass = QtWidgets.QDateEdit(MainWindow)
        self.datMass.setGeometry(QtCore.QRect(360, 150, 111, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.datMass.setFont(font)
        self.datMass.setObjectName("datMass")
        self.timMass = QtWidgets.QTimeEdit(MainWindow)
        self.timMass.setGeometry(QtCore.QRect(480, 150, 71, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.timMass.setFont(font)
        self.timMass.setObjectName("timMass")
        self.lblMass = QtWidgets.QLabel(MainWindow)
        self.lblMass.setGeometry(QtCore.QRect(30, 150, 201, 21))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.lblMass.setFont(font)
        self.lblMass.setObjectName("lblMass")
        self.lblParts = QtWidgets.QLabel(MainWindow)
        self.lblParts.setGeometry(QtCore.QRect(30, 190, 201, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        self.lblParts.setFont(font)
        self.lblParts.setObjectName("lblParts")
        self.lblPath = QtWidgets.QLabel(MainWindow)
        self.lblPath.setGeometry(QtCore.QRect(30, 90, 781, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lblPath.setFont(font)
        self.lblPath.setObjectName("lblPath")
        self.lblTrust = QtWidgets.QLabel(MainWindow)
        self.lblTrust.setGeometry(QtCore.QRect(30, 100, 671, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lblTrust.setFont(font)
        self.lblTrust.setObjectName("lblTrust")
        self.pbnParts = QtWidgets.QPushButton(MainWindow)
        self.pbnParts.setGeometry(QtCore.QRect(30, 800, 111, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.pbnParts.setFont(font)
        self.pbnParts.setObjectName("pbnParts")
        self.pbnCopyright = QtWidgets.QPushButton(MainWindow)
        self.pbnCopyright.setGeometry(QtCore.QRect(160, 800, 141, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.pbnCopyright.setFont(font)
        self.pbnCopyright.setObjectName("pbnCopyright")
        self.pbnAbout = QtWidgets.QPushButton(MainWindow)
        self.pbnAbout.setGeometry(QtCore.QRect(450, 800, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.pbnAbout.setFont(font)
        self.pbnAbout.setObjectName("pbnAbout")
        self.pbnReset = QtWidgets.QPushButton(MainWindow)
        self.pbnReset.setGeometry(QtCore.QRect(540, 800, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.pbnReset.setFont(font)
        self.pbnReset.setObjectName("pbnReset")
        self.pbnCancel = QtWidgets.QPushButton(MainWindow)
        self.pbnCancel.setGeometry(QtCore.QRect(630, 800, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.pbnCancel.setFont(font)
        self.pbnCancel.setObjectName("pbnCancel")
        self.chk040 = QtWidgets.QCheckBox(MainWindow)
        self.chk040.setGeometry(QtCore.QRect(230, 280, 20, 21))
        self.chk040.setText("")
        self.chk040.setObjectName("chk040")
        self.lblTick = QtWidgets.QLabel(MainWindow)
        self.lblTick.setGeometry(QtCore.QRect(230, 190, 231, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lblTick.setFont(font)
        self.lblTick.setObjectName("lblTick")
        self.chk070 = QtWidgets.QCheckBox(MainWindow)
        self.chk070.setGeometry(QtCore.QRect(230, 340, 20, 21))
        self.chk070.setText("")
        self.chk070.setObjectName("chk070")
        self.chk080 = QtWidgets.QCheckBox(MainWindow)
        self.chk080.setGeometry(QtCore.QRect(230, 360, 20, 21))
        self.chk080.setText("")
        self.chk080.setObjectName("chk080")
        self.chk090 = QtWidgets.QCheckBox(MainWindow)
        self.chk090.setGeometry(QtCore.QRect(230, 380, 20, 21))
        self.chk090.setText("")
        self.chk090.setObjectName("chk090")
        self.chk120 = QtWidgets.QCheckBox(MainWindow)
        self.chk120.setGeometry(QtCore.QRect(230, 460, 20, 21))
        self.chk120.setText("")
        self.chk120.setObjectName("chk120")
        self.chk150 = QtWidgets.QCheckBox(MainWindow)
        self.chk150.setGeometry(QtCore.QRect(230, 520, 20, 21))
        self.chk150.setText("")
        self.chk150.setObjectName("chk150")
        self.chk160 = QtWidgets.QCheckBox(MainWindow)
        self.chk160.setGeometry(QtCore.QRect(230, 540, 20, 21))
        self.chk160.setText("")
        self.chk160.setObjectName("chk160")
        self.chk170 = QtWidgets.QCheckBox(MainWindow)
        self.chk170.setGeometry(QtCore.QRect(230, 560, 20, 21))
        self.chk170.setText("")
        self.chk170.setObjectName("chk170")
        self.chk190 = QtWidgets.QCheckBox(MainWindow)
        self.chk190.setGeometry(QtCore.QRect(230, 600, 20, 21))
        self.chk190.setText("")
        self.chk190.setObjectName("chk190")
        self.chk210 = QtWidgets.QCheckBox(MainWindow)
        self.chk210.setGeometry(QtCore.QRect(230, 640, 20, 21))
        self.chk210.setText("")
        self.chk210.setObjectName("chk210")
        self.chk220 = QtWidgets.QCheckBox(MainWindow)
        self.chk220.setGeometry(QtCore.QRect(230, 660, 20, 21))
        self.chk220.setText("")
        self.chk220.setObjectName("chk220")
        self.chk250 = QtWidgets.QCheckBox(MainWindow)
        self.chk250.setGeometry(QtCore.QRect(230, 720, 20, 21))
        self.chk250.setText("")
        self.chk250.setObjectName("chk250")
        self.layoutWidget = QtWidgets.QWidget(MainWindow)
        self.layoutWidget.setGeometry(QtCore.QRect(30, 850, 115, 15))
        self.layoutWidget.setObjectName("layoutWidget")
        self.layGui = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.layGui.setContentsMargins(0, 0, 0, 0)
        self.layGui.setObjectName("layGui")
        self.lblGui = QtWidgets.QLabel(self.layoutWidget)
        self.lblGui.setObjectName("lblGui")
        self.layGui.addWidget(self.lblGui)
        self.lblGuiVer = QtWidgets.QLabel(self.layoutWidget)
        self.lblGuiVer.setObjectName("lblGuiVer")
        self.layGui.addWidget(self.lblGuiVer)
        self.layoutWidget1 = QtWidgets.QWidget(MainWindow)
        self.layoutWidget1.setGeometry(QtCore.QRect(160, 850, 104, 15))
        self.layoutWidget1.setObjectName("layoutWidget1")
        self.layCode = QtWidgets.QHBoxLayout(self.layoutWidget1)
        self.layCode.setContentsMargins(0, 0, 0, 0)
        self.layCode.setObjectName("layCode")
        self.lblCodeVer = QtWidgets.QLabel(self.layoutWidget1)
        self.lblCodeVer.setObjectName("lblCodeVer")
        self.layCode.addWidget(self.lblCodeVer)
        self.lblCode = QtWidgets.QLabel(self.layoutWidget1)
        self.lblCode.setText("")
        self.lblCode.setObjectName("lblCode")
        self.layCode.addWidget(self.lblCode)
        self.lbl24hour = QtWidgets.QLabel(MainWindow)
        self.lbl24hour.setGeometry(QtCore.QRect(486, 170, 51, 21))
        self.lbl24hour.setObjectName("lbl24hour")

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Powerpoint Builder"))
        self.lblTitle.setText(_translate("MainWindow", "Powerpoint Builder"))
        self.cbxChurch.setItemText(0, _translate("MainWindow", "St Francis"))
        self.cbxChurch.setItemText(1, _translate("MainWindow", "St Marys"))
        self.lblMass.setText(_translate("MainWindow", "Mass Details"))
        self.lblParts.setText(_translate("MainWindow", "Powerpoint Parts"))
        self.lblPath.setText(_translate("MainWindow", "Running from ... path."))
        self.lblTrust.setText(_translate("MainWindow", "(This must be set as a Trusted Location in Powerpoint)"))
        self.pbnParts.setText(_translate("MainWindow", "Create Parts File"))
        self.pbnCopyright.setText(_translate("MainWindow", "Create Copyright File"))
        self.pbnAbout.setText(_translate("MainWindow", "About"))
        self.pbnReset.setText(_translate("MainWindow", "Reset"))
        self.pbnCancel.setText(_translate("MainWindow", "Cancel"))
        self.lblTick.setText(_translate("MainWindow", "(Tick if embedded music required)"))
        self.lblGui.setText(_translate("MainWindow", "GUI version:"))
        self.lblGuiVer.setText(_translate("MainWindow", "0.02A"))
        self.lblCodeVer.setText(_translate("MainWindow", "Code version:"))
        self.lbl24hour.setText(_translate("MainWindow", "24 hour"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QWidget()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())



Aucun commentaire:

Enregistrer un commentaire