dimanche 2 décembre 2018

How to add a checkbox to a dictionary?

I'm trying to have users vote on the best day of the week & time to meet (e.g., Sunday 8AM) by using checkboxes in PyQt. Each of the checkboxes is an object. If a user checks the box for Sunday 8AM and Sunday 9AM, I want both of those checkboxes to go into a dictionary where the keys are the date/time options (i.e., Sunday 8AM) and the values are the number of times that option has been voted for (i.e., 1).

I'm not sure how to loop through the checkboxes in PyQt. This is my code so far.

import sys
from pprint import pprint
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QCheckBox
from PyQt5.QtCore import Qt
from PyQt5 import uic


qtCreatorFile = "MyApp.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.submit.clicked.connect(self.PollCount)
        self.ui.results.clicked.connect(self.ViewResults)

    def PollCount(self):
        poll_count={}
        options=[]
        best_option_count=0
        best_options=[]
        for x in range(self.MainWindow.count()):
            if self.ui.MainWindow.itemAt(x).widget().isChecked():
                options.append(item)
        for i in options:
            poll_count[option]=poll_count.get(option,0)+1
        for count in poll_count.values():
            if int(count)>best_option_count:
                best_option_count=int(count)
        for (option,count) in poll_count.items():
            if int(poll_count[option])==best_option_count:
                best_options.append(option)
        results_string =best_options
        self.ui.result_window.setText(results_string)


    def ViewResults(self):
        results_string =best_options
        self.ui.result_window.setText(results_string)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Myapp()
    window.show()
    sys.exit(app.exec_())




Aucun commentaire:

Enregistrer un commentaire