Python Pyqt5 QDateEdit 获取日期字符串

标签 python python-3.x qt pyqt5 qdateedit

我正在尝试使用 Pyqt5 QDateEdit 构建日期打印机。我可以弹出日历,但我想在控制台中(或窗口中的标签中)写入单击的日期字符串。我尝试了 print(self.calendarWidget().document().toPlainText())print(self.calendarWidget().currentText()) 但没有成功工作。

我使用此代码;

from PyQt5 import QtCore, QtWidgets


class DateEdit(QtWidgets.QDateEdit):
    popupSignal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(DateEdit, self).__init__(parent)
        self.setCalendarPopup(True)
        self.calendarWidget().installEventFilter(self)

    def eventFilter(self, obj, event):
        if self.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
            self.popupSignal.emit()
        return super(DateEdit, self).eventFilter(obj, event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = DateEdit()
    w.popupSignal.connect(lambda: print("popup"))
    w.show()
    sys.exit(app.exec_())

它的语法是什么?我没有找到足够的文档。你能帮忙吗?

最佳答案

编辑:答案

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.dateEdit = QDateEdit(self)
        self.lbl = QLabel()
        self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
        self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
        self.dateEdit.setCalendarPopup(True)

        layout = QGridLayout()
        layout.addWidget(self.dateEdit)
        layout.addWidget(self.lbl)
        self.setLayout(layout)


        self.dateEdit.dateChanged.connect(self.onDateChanged)

    def onDateChanged(self, qDate):
        print('{0}/{1}/{2}'.format(qDate.day(), qDate.month(), qDate.year()))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())

关于Python Pyqt5 QDateEdit 获取日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59424546/

相关文章:

python - ('Unexpected credentials type',无, 'Expected', 'service_account')与 oauth2client(Python)

Python Selenium-webdriver : for loop returning first element of the list throughout the loop

python - 如何将本地 PyCharm 连接到服务器上安装的 python?这可能吗?

python - 如何将列表拆分为元素中特定字符的列表列表?

python - 读取特定输入的文件 python

python - 快速映射/修改大量 Python 字典中的值?

python-3.x - asyncpg 获取反馈(python)

C++ 和 Qt,如何在对象创建时运行构造函数或不同的函数来设置 Ui?

c++ - 获取网格中心的屏幕位置

c++ - 使用 UDP 协议(protocol)接收数据并读取数据报