点击QLineEdit,弹出日历控件,选择之后,赋值
时间: 2023-07-04 13:15:42 浏览: 191
您可以使用QCalendarWidget和QLineEdit一起实现这个功能。首先,您可以将QCalendarWidget与QLineEdit关联,以便在单击QLineEdit时弹出QCalendarWidget。然后,您可以在QCalendarWidget上选择日期,并将其赋值给QLineEdit。
以下是一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QCalendarWidget, QLineEdit, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个QCalendarWidget和QLineEdit
self.calendar_widget = QCalendarWidget()
self.date_edit = QLineEdit()
# 将日历控件关联到QLineEdit上
self.calendar_widget.clicked.connect(self.update_date_edit)
# 创建一个垂直布局,并将QCalendarWidget和QLineEdit添加到其中
layout = QVBoxLayout()
layout.addWidget(self.calendar_widget)
layout.addWidget(self.date_edit)
# 创建一个QWidget并将垂直布局添加到其中
widget = QWidget()
widget.setLayout(layout)
# 将QWidget设置为主窗口的中心窗口
self.setCentralWidget(widget)
def update_date_edit(self, q):
# 将选定的日期赋值给QLineEdit
date = self.calendar_widget.selectedDate()
self.date_edit.setText(date.toString())
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
当您点击QLineEdit时,将弹出QCalendarWidget。选择日期后,QLineEdit将显示所选日期。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)