Код для секундомера Python (3.9, PyQT5) Не начинает отчет при нажатии. Укажите на ошибки
from PyQt5 import QtCore, QtGui, QtWidgets
import time
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/AIR/Downloads/watch.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
self.StartButton = QtWidgets.QPushButton(Dialog)
self.StartButton.setGeometry(QtCore.QRect(40, 30, 151, 71))
self.StartButton.setObjectName("StartButton")
self.ResetButton = QtWidgets.QPushButton(Dialog)
self.ResetButton.setGeometry(QtCore.QRect(220, 30, 151, 71))
self.ResetButton.setObjectName("ResetButton")
self.Time = QtWidgets.QLineEdit(Dialog)
self.Time.setGeometry(QtCore.QRect(40, 140, 321, 121))
self.Time.setStyleSheet("font: 22pt \"MS Shell Dlg 2\";")
self.Time.setAlignment(QtCore.Qt.AlignCenter)
self.Time.setReadOnly(True)
self.Time.setObjectName("Time")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Sekundomer"))
self.StartButton.setText(_translate("Dialog", "Старт"))
self.ResetButton.setText(_translate("Dialog", "Стоп/Сброс"))
self.Time.setText(_translate("Dialog", "00:00:00"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
def start():
global isStart, startTime
isStart = True
startTime = time.time()
def reset():
global isStart, startTime
isStart = False
def timerFunction():
global isStart, startTime
if isStart:
time_r = int(time.time() - startTime)
hours = time_r // 3600
minutes = (time_r % 3600)//60
seconds = time_r % 60
if hours > 99:
isStart = False
else:
hours = str(hours)
minutes = str(minutes)
seconds = str(seconds)
time_str = '0'*(2-len(hours))+hours+":"+"0"*(2-len(minutes))+minutes+":"+"0"*(2-len(seconds))+seconds
Sekundomer.Time.setText(time_str)
Sekundomer.StartButton.clicked.connect(start)
Sekundomer.ResetButton.clicked.connect(reset)
timer = QtCore.QTimer()
timer.timeout.connect(timerFunction)
timer.start(100)
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Не изменяйте код, сгенерированный Qt Designer. Создайте другой класс, который наследуется от соответствующего виджета, и используйте созданный класс для его заполнения.
Глобальные переменные - это зло!
from PyQt5 import QtCore, QtGui, QtWidgets
import time
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("C:/Users/AIR/Downloads/watch.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
self.StartButton = QtWidgets.QPushButton(Dialog)
self.StartButton.setGeometry(QtCore.QRect(40, 30, 151, 71))
self.StartButton.setObjectName("StartButton")
self.ResetButton = QtWidgets.QPushButton(Dialog)
self.ResetButton.setGeometry(QtCore.QRect(220, 30, 151, 71))
self.ResetButton.setObjectName("ResetButton")
self.Time = QtWidgets.QLineEdit(Dialog)
self.Time.setGeometry(QtCore.QRect(40, 140, 321, 121))
self.Time.setStyleSheet("font: 22pt \"MS Shell Dlg 2\";")
self.Time.setAlignment(QtCore.Qt.AlignCenter)
self.Time.setReadOnly(True)
self.Time.setObjectName("Time")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Sekundomer"))
self.StartButton.setText(_translate("Dialog", "Старт"))
self.ResetButton.setText(_translate("Dialog", "Стоп/Сброс"))
self.Time.setText(_translate("Dialog", "00:00:00"))
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
self.StartButton.clicked.connect(self.start)
self.ResetButton.clicked.connect(self.reset)
self.isStart = False
self.startTime = ...
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.timerFunction)
self.timer.start(100)
def start(self):
self.isStart = True
self.startTime = time.time()
def reset(self):
self.isStart = False
def timerFunction(self):
if self.isStart:
time_r = int(time.time() - self.startTime)
hours = time_r // 3600
minutes = (time_r % 3600)//60
seconds = time_r % 60
if hours > 99:
self.isStart = False
else:
hours = str(hours)
minutes = str(minutes)
seconds = str(seconds)
time_str = '0'*(2-len(hours)) + hours+":"+"0"*(2-len(minutes)) + \
minutes+":"+"0"*(2-len(seconds)) + seconds
self.Time.setText(time_str)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())
