如何在另一个函数中使用在 QDateEdit 小部件中设置的用户值?

How do I use the user value set in the in QDateEdit widget in another function?

我正在使用 PyQT5 为一些需要用户日期输入的脚本构建 GUI 运行。

我已经成功地做到了 select 使用 DateEdit 范围的日期,并在每次用户更改时将新日期打印到控制台。

我需要做的是在单击 运行 按钮时在函数中使用 QDateEdit 小部件中的任何日期。

这是我的示例代码

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 the dateChanged signal, but in your case you do not want the notification but the current date and for this you must use the date() 方法。

import sys
from PyQt5.QtCore import Qt, QDate
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
    QApplication,
    QCheckBox,
    QGridLayout,
    QGroupBox,
    QPushButton,
    QVBoxLayout,
    QWidget,
    QFrame,
    QDateEdit,
)


class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setWindowIcon(QIcon("logo.png"))
        self.setWindowTitle("Test")
        self.resize(400, 300)

        grid = QGridLayout(self)
        grid.addWidget(self.group1(), 0, 0)

    def group1(self):
        groupBox = QGroupBox("Box 1")

        date = QDate.currentDate().addDays(-1)
        self.dateSelect = QDateEdit()
        self.dateSelect.setDate(date)
        self.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(frameShape=QFrame.HLine, frameShadow=QFrame.Sunken)

        vbox = QVBoxLayout()
        vbox.addWidget(self.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,):
        print(self.dateSelect.date().toString("yyyy-MM-dd"))

    def onDateChanged(self, newDate):
        date = newDate.toString("yyyy-MM-dd")
        print(date)


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