Amgad Elsaiegh
Amgad Elsaiegh

Reputation: 33

PyQt5 changing QlineEdit text automatically

i have a main window with a read-only QlineEdit that displays today date and a button that opens calendar widget window (from another module in the project) to choose another date ,the problem that when i choose another date the QlineEdit text in Gui do not change although the variable that stores the value changes successfully ,i searched a lot with no clue

here is the main window:

import sys
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
from PyQt5 import QtCore, QtGui, QtWidgets
from cal_window import prev_day, today_str, UiCalWindow


class MainWindow(QMainWindow):


    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("My App")
        self.setGeometry(50, 50, 800, 600)

        self.label = QLineEdit(self)
        self.label.setFont(QFont("Arial", 20))

        self.label.setReadOnly(True)
        self.label.setText(today_str)

        self.setCentralWidget(self.label)

        self.btn = QPushButton('open calender', self)
        self.btn.move(50, 50)
        self.btn.clicked.connect(self.choose_date)


    def choose_date(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = UiCalWindow()
        self.ui.setupUi(self.window)
        self.window.show()



app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

and here is the calendar sub-window:

from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from datetime import date




today = date.today()
today_str = today.strftime("%Y-%m-%d")
prev_day = "2000-01-01"


class UiCalWindow(object):

    def get_date(self, qDate):
        global prev_day
        self.day = '{0}-{1}-{2}'.format(qDate.year(), qDate.month(), qDate.day())
        prev_day = self.day
        print(prev_day)

    def setupUi(self, cal_window):
        cal_window.setObjectName("cal_window")
        cal_window.setWindowModality(QtCore.Qt.ApplicationModal)
        cal_window.resize(800, 600)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        cal_window.setFont(font)
        self.centralwidget = QtWidgets.QWidget(cal_window)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.calendarWidget.setFont(font)
        self.calendarWidget.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.calendarWidget.setFirstDayOfWeek(QtCore.Qt.Monday)
        self.calendarWidget.setGridVisible(True)
        self.calendarWidget.setVerticalHeaderFormat(QtWidgets.QCalendarWidget.NoVerticalHeader)
        self.calendarWidget.setObjectName("calendarWidget")
        self.gridLayout.addWidget(self.calendarWidget, 0, 0, 1, 1)
        self.calendarWidget.clicked.connect(self.get_date)
        self.ok_dt_btn = QtWidgets.QPushButton(self.centralwidget)
        self.ok_dt_btn.setMinimumSize(QtCore.QSize(0, 50))
        self.ok_dt_btn.setObjectName("ok_dt_btn")
        self.ok_dt_btn.clicked.connect(cal_window.close)
        self.gridLayout.addWidget(self.ok_dt_btn, 1, 0, 1, 1)
        cal_window.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, cal_window):
        _translate = QtCore.QCoreApplication.translate
        cal_window.setWindowTitle(_translate("cal_window", "MainWindow"))
        self.ok_dt_btn.setText(_translate("cal_window", "go"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    cal_window = QtWidgets.QMainWindow()
    ui = UiCalWindow()
    ui.setupUi(cal_window)
    cal_window.show()
    sys.exit(app.exec_())```

Upvotes: 0

Views: 3697

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

You have concept problems:

  • Do not modify the code generated by Qt Designer (it is recommended that you restore cal_window.py.

  • Do not use global variables, they are unnecessary and generate more problems than solutions.

  • That the variable "prev_day" is updated does not imply that the text displayed by the QLabel is updated, the QLabel copies the information and does not monitor the string.

The logic is to use the signal to update the text.

import sys
from PyQt5.QtCore import pyqtSignal, QDate
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
from cal_window import UiCalWindow


class CalendarWindow(QMainWindow, UiCalWindow):
    def __init__(self, parent=None):
        super(CalendarWindow, self).__init__(parent)
        self.setupUi(self)


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("My App")
        self.setGeometry(50, 50, 800, 600)

        self.label = QLineEdit(self)
        self.label.setFont(QFont("Arial", 20))
        self.label.setReadOnly(True)

        self.setCentralWidget(self.label)

        self.btn = QPushButton("open calender", self)
        self.btn.move(50, 50)

        self.cal = CalendarWindow()
        self.cal.calendarWidget.clicked.connect(self.handle_date_clicked)
        self.btn.clicked.connect(self.cal.show)

    def handle_date_clicked(self, date):
        self.label.setText(date.toString("yyyy-MM-dd"))


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

Upvotes: 2

Related Questions