PySide6 控件插入日期时间(QDateTime)
PySide6 控件插入日期时间(QDateTime)
这篇文章的由来
这篇文章是基于实现win10
记事本中编辑菜单项的时间/日期
行为的测试。
有兴趣的话可以看看我的项目
前置知识
日期时间的格式化编码
ISO8601
代码逻辑
这里只有关键的代码逻辑。
- 获取当前时间(QDateTime.currentDateTime方法)
- 格式化输出1返回的(QDateTime.toString方法)
- 将2返回的字符串添加到需要的控件中
代码演示
基于行文本编辑(QLineEdit)
# coding = utrf-8from PySide6.QtWidgets import QWidget,QLineEdit,QPushButton,QHBoxLayout,QApplication
from PySide6.QtCore import QDateTime
import sysclass MyWidget(QWidget):"""自定义窗口控件:param QWidget: 基础控件"""def __init__(self):"""构造函数"""super().__init__()def setupUi(self):"""设置用户界面"""# 主布局使用水平布局hbox = QHBoxLayout(self)#创建 行编辑和添加按钮self.lineEdit = QLineEdit()self.insertDateBtn = QPushButton("插入当前日期")# 添加控件到布局hbox.addWidget(self.lineEdit)hbox.addWidget(self.insertDateBtn)def setEventBind(self):"""设置事件绑定"""self.insertDateBtn.clicked.connect(self.insertDate)def insertDate(self):"""插入日期"""currentDateTime = QDateTime.currentDateTime()formattedTime = currentDateTime.toString("hh:mm yyyy/MM/dd")self.lineEdit.insert(formattedTime)if __name__ == "__main__":app = QApplication(sys.argv)myWidget = MyWidget()myWidget.show()myWidget.setupUi()myWidget.setEventBind()sys.exit(app.exec())
基于文本编辑
# coding = utrf-8from PySide6.QtWidgets import QWidget,QTextEdit,QPushButton,QVBoxLayout,QApplication
from PySide6.QtCore import QDateTime
import sysclass MyWidget(QWidget):"""自定义窗口控件:param QWidget: 基础控件"""def __init__(self):"""构造函数"""super().__init__()def setupUi(self):"""设置用户界面"""# 主布局使用垂直布局vbox = QVBoxLayout(self)#创建 文本编辑和添加按钮self.textEdit = QTextEdit()self.insertDateBtn = QPushButton("插入当前日期")# 添加控件到布局vbox.addWidget(self.textEdit)vbox.addWidget(self.insertDateBtn)def setEventBind(self):"""设置事件绑定"""self.insertDateBtn.clicked.connect(self.insertDate)def insertDate(self):"""插入日期"""currentDateTime = QDateTime.currentDateTime()formattedTime = currentDateTime.toString("hh:mm yyyy/MM/dd")self.textEdit.insertPlainText(formattedTime)if __name__ == "__main__":app = QApplication(sys.argv)myWidget = MyWidget()myWidget.show()myWidget.setupUi()myWidget.setEventBind()sys.exit(app.exec())
基于文本预览
# coding = utrf-8from PySide6.QtWidgets import QWidget,QTextBrowser,QPushButton,QVBoxLayout,QApplication
from PySide6.QtCore import QDateTime
import sysclass MyWidget(QWidget):"""自定义窗口控件:param QWidget: 基础控件"""def __init__(self):"""构造函数"""super().__init__()def setupUi(self):"""设置用户界面"""# 主布局使用垂直布局vbox = QVBoxLayout(self)#创建 文本编辑和添加按钮self.textBrowser = QTextBrowser()self.insertDateBtn = QPushButton("插入当前日期")# 添加控件到布局vbox.addWidget(self.textBrowser)vbox.addWidget(self.insertDateBtn)def setEventBind(self):"""设置事件绑定"""self.insertDateBtn.clicked.connect(self.insertDate)def insertDate(self):"""插入日期"""currentDateTime = QDateTime.currentDateTime()formattedTime = currentDateTime.toString("hh:mm yyyy/MM/dd")self.textBrowser.insertPlainText(formattedTime)if __name__ == "__main__":app = QApplication(sys.argv)myWidget = MyWidget()myWidget.show()myWidget.setupUi()myWidget.setEventBind()sys.exit(app.exec())
效果演示
插入日期 行编辑实现
插入日期 文本编辑实现
插入日期 文本阅读实现