I'm building a GUI using PyQT5 for some scripts I run which requires a user date input.
I've managed to get to a point where I can select a date using the DateEdit range, and have new date printed to the console every time the user changes it.
What I need to do is use whatever date is in the QDateEdit widget in a function when I click the Run button.
Here's my sample code
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QDate
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox,
QMenu, QPushButton, QRadioButton, QVBoxLayout, QWidget, QFrame, QDateEdit)
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowIcon(QtGui.QIcon('logo.png'))
self.setWindowTitle("Test")
self.resize(400, 300)
grid = QGridLayout()
grid.addWidget(self.group1(), 0, 0)
self.setLayout(grid)
def group1(self):
groupBox = QGroupBox("Box 1")
date = QDate.currentDate().addDays(-1)
dateSelect = QDateEdit()
dateSelect.setDate(date)
dateSelect.dateChanged.connect(self.onDateChanged)
checkbox1 = QCheckBox("Task 1")
checkbox2 = QCheckBox("Task 2")
checkbox3 = QCheckBox("Task 3")
button1 = QPushButton('Run')
button1.setMaximumWidth(75)
button1.clicked.connect(self.btn1_onClicked)
button2 = QPushButton('Run')
button2.setMaximumWidth(75)
separatorLine = QFrame()
separatorLine.setFrameShape(QFrame.HLine)
separatorLine.setFrameShadow(QFrame.Sunken)
vbox = QVBoxLayout()
vbox.addWidget(dateSelect)
vbox.addWidget(checkbox1)
vbox.addWidget(checkbox2)
vbox.addWidget(button1)
vbox.addWidget(separatorLine)
vbox.addWidget(checkbox3)
vbox.addWidget(button2)
vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
def btn1_onClicked(self, ):
date = onDateChanged()
def onDateChanged(self,newDate):
date = newDate.toString('yyyy-MM-dd')
print(date)
return date
if __name__ == '__main__':
app = QApplication(sys.argv)
clock = Window()
clock.show()
sys.exit(app.exec_())
QDateEdit
notifies the change of the date through thedateChanged
signal, but in your case you do not want the notification but the current date and for this you must use thedate()
method.