代码:
from PyQt5 import QtCore, QtGui, QtWidgets
from datetime import datetime //注意添加class Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(444, 491)MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)self.textBrowser.setGeometry(QtCore.QRect(20, 0, 401, 321))font = QtGui.QFont()font.setFamily("幼圆")font.setPointSize(14)self.textBrowser.setFont(font)self.textBrowser.setObjectName("textBrowser")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(60, 340, 321, 81))font = QtGui.QFont()font.setFamily("幼圆")font.setPointSize(14)self.pushButton.setFont(font)self.pushButton.setObjectName("pushButton")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 444, 23))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)self.pushButton.clicked.connect(self.slot1) //修改selfQtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "ms级时间精度测试工具")) self.pushButton.setText(_translate("MainWindow", "点击获取时间"))def slot1(self):time1=datetime.now()time2=str(time1)print(time1)self.textBrowser.append('当前标准时间:') //需要用append ,不然会覆盖掉获取的数据self.textBrowser.append(time2)import sys
if __name__ == '__main__':app=QtWidgets.QApplication(sys.argv)MainWindow=QtWidgets.QMainWindow()ui=Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())