如何在 QDateEdit 弹出窗口 QCalendarWidget 中添加今天按钮 [英] How to add Today Button in QDateEdit Pop-up QCalendarWidget
本文介绍了如何在 QDateEdit 弹出窗口 QCalendarWidget 中添加今天按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像这样的今天按钮
我的弹出式日历小部件的图像:
Image of my pop-up calendar widget:
我正在尝试使用带有日期选择器选项的 Python 中的 PyQt5 创建简单的 Gui.我需要在弹出的 QCalendarWidget 中的 QDateEdit 中添加今天按钮.
I am trying to create simple Gui using PyQt5 in Python with date picker option. I need to add today button in QDateEdit in pop-up QCalendarWidget.
推荐答案
必须通过布局将按钮添加到 QCalendarWidget 中,并在按钮按下时将 QDate::currentDate() 设置为 QCalendarWidget 的 selectedDate:
You must add the button to the QCalendarWidget through the layout, and when the button is pressed set the QDate::currentDate() as selectedDate of the QCalendarWidget:
import sys
from PyQt5 import QtCore, QtWidgets
class DateEdit(QtWidgets.QDateEdit):
def __init__(self, parent=None):
super().__init__(parent, calendarPopup=True)
self._today_button = QtWidgets.QPushButton(self.tr("Today"))
self._today_button.clicked.connect(self._update_today)
self.calendarWidget().layout().addWidget(self._today_button)
@QtCore.pyqtSlot()
def _update_today(self):
self._today_button.clearFocus()
today = QtCore.QDate.currentDate()
self.calendarWidget().setSelectedDate(today)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = DateEdit()
w.show()
sys.exit(app.exec_())
这篇关于如何在 QDateEdit 弹出窗口 QCalendarWidget 中添加今天按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文