Issue
I have a combobox which, when run, is setted up to current year and current month as in picture.
import sys
from time import strftime
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
convert_number_month = {"1": "Gennaio", "2": "Febbraio", "3": "Marzo",
"4": "Aprile", "5": "Maggio", "6": "Giugno", "7": "Luglio", "8": "Agosto",
"9": "Settembre", "10": "Ottobre", "11": "Novembre", "12": "Dicembre"}
class MakeCombos(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.anno_in = None
self.mese_in = None
self.initUI(self)
self.Data_For_Db()
def MeseSelected_func(self, text=None): # +++
self.mese_in = text
def AnnoSelected_func(self, text=None): # +++
self.anno_in = text
def initUI(self, Form):
Form.setObjectName("Form")
Form.resize(300, 300)
self.work_Year_select = QtWidgets.QComboBox(Form)
self.work_Year_select.setGeometry(QtCore.QRect(50, 140, 80, 20))
self.work_Year_select.setObjectName("work_Year_select")
self.work_Month_select = QtWidgets.QComboBox(Form)
self.work_Month_select.setGeometry(QtCore.QRect(150, 140, 100, 21))
self.work_Month_select.setObjectName("work_Month_select")
self.work_Add_btn = QtWidgets.QPushButton(Form)
self.work_Add_btn.setGeometry(QtCore.QRect(100, 169, 70, 20))
self.work_Add_btn.setObjectName("work_Add_btn")
_translate = QtCore.QCoreApplication.translate
self.work_Year_select.addItems(['2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024'])
self.this_year = strftime("%Y")
self.work_Year_select.setCurrentText(self.this_year)
self.work_Year_select.currentTextChanged.connect(self.AnnoSelected_func)
self.work_Month_select.addItems(
['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre',
'Novembre', 'Dicembre'])
self.this_month = strftime("%-m")
self.this_month = convert_number_month[self.this_month]
self.work_Month_select.setCurrentText(self.this_month)
self.work_Month_select.currentTextChanged.connect(self.MeseSelected_func)
self.work_Add_btn.setText(_translate("Library_main", "Select"))
self.work_Add_btn.clicked.connect(self.Data_For_Db)
def Data_For_Db(self):
if self.mese_in == None or self.anno_in == None:
print('Missing Selection')
else: print("Its OK")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
exe= MakeCombos()
exe.show()
sys.exit(app.exec_())
My problem is: if I press "Select" without changing both the values of "Anno" and "Mese" I get the error "SELEZIONE MANCANTE/Missing selection" Which means: if i need to read data related to 2021 an Gennaio/Janaury I must click on another year AND another month then return to the required values
I supposed that the shown values (2021 and Gennaio) were already active
Solution
As the name suggests, Qt signals that end with a Changed
suffix are only emitted when the property changes.
In your case, this means that the connected functions will not be called on startup because you connected the signal after adding the items. Generally, if you want to be sure that the function gets called during initialization, you could connect the signal before adding items, since the text is empty by default, and as soon as the first item is added, QComboBox automatically changes the index to that.
Since you only need that data when you call Data_For_Db
, there's absolutely no need to set instance attributes whenever the text changes, and you can just use combo.currentText()
:
def Data_For_Db(self):
month = self.work_Month_select.currentText()
year = self.work_Year_select.currentText()
# ...
Some further (and partially unrelated) notes:
- using
setCurrentText()
for a non editable combo box is not a good idea, andsetCurrentIndex()
should be always preferred; - using
strftime
,convert_number_month
and thensetCurrentText()
is an ugly way to select the current month (and using a dictionary to get the month name is not a good approach); since the number and name of months are obviously known, get the current month number fromdate.today().month
orQDate.currentDate().month()
and subctract 1 forsetCurrentIndex()
; - avoid fixed geometries at any cost, and always use layout managers instead;
- variable and function names should never be capitalized, as only classes and constants should;
- don't try to imitate the behavior of files generated by pyuic, it's considered bad practice and usually leads to confusion; you can still use an
initUi
function if you want, but get rid of itsForm
argument, as it's completely useless;
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.