im doing a game that diffusing a bomb by answering questions. is there any way to wait for the user to push the pushbutton at a certain time? and when that certain time runs out the button will disabled. Thank you for your answers :)
You have to use a QTimer to implement the logic:
from PyQt5 import QtCore, QtWidgets
from functools import partial
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QtWidgets.QPushButton(
text='Start Game',
clicked=self.on_start_game_clicked
)
self.game_button = QtWidgets.QPushButton(
text='Press me',
clicked=self.on_game_clicked
)
self.time_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(self.game_button)
lay.addWidget(self.time_label)
self.timer = QtCore.QTimer(self,
interval=5000, # time in ms
timeout=partial(self.game_button.setDisabled, True),
singleShot=True
)
self.time_timer = QtCore.QTimer(self,
interval=100,
timeout=self.update_label
)
@QtCore.pyqtSlot()
def on_start_game_clicked(self):
if not self.timer.isActive():
self.timer.start()
self.time_timer.start()
self.game_button.setEnabled(True)
@QtCore.pyqtSlot()
def update_label(self):
if self.timer.remainingTime() >= 0:
self.time_label.setText('{0:.2f} ms'.format(self.timer.remainingTime()*0.001))
else:
self.time_label.setText('0 ms')
@QtCore.pyqtSlot()
def on_game_clicked(self):
print("clicked")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.