我在pyqt5中有一个这样的类,它以timhoursa
作为全局变量
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
timhoursa=0
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableView = QtWidgets.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(25, 81, 761, 471))
self.tableView.setObjectName("tableView")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(100, 40, 300, 25))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.dateEdit = QtWidgets.QDateEdit(self.widget)
self.dateEdit.setObjectName("dateEdit")
self.horizontalLayout.addWidget(self.dateEdit)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.timeEdit = QtWidgets.QTimeEdit(self.widget)
self.timeEdit.setObjectName("timeEdit")
self.horizontalLayout.addWidget(self.timeEdit)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Date"))
self.label_2.setText(_translate("MainWindow", "Time"))
self.pushButton.setText(_translate("MainWindow", "Create Chart"))
self.pushButton.clicked.connect(self.btn_clk)
#self.timeEdit.changeEvent.connect
self.timeEdit.timeChanged.connect(self.storedata)
当在timedit模块中更改时间时,我想选择该时间更改并将其存储在变量中,以便我可以访问与该类无关的其他函数,因此在上面的代码中添加了此功能
def storedata(self):
timhoursa = self.timeEdit.time().toString("hh")
return timhoursa
但是当我尝试打印timhoursa
时,出现以下错误
NameError: name 'timhoursa' is not defined
我可以看到这是一个示波器问题,但无法解决,我在这里遗漏了什么