I'm making a game where I would like a timer to be displayed on the screen after the user clicks on 'NEW GAME', to keep track of how long they've been playing.
(我正在制作一个游戏,希望在用户单击“新游戏”后在屏幕上显示计时器,以跟踪他们玩了多长时间。)
I have a class that runs the timer fine by itself, but when I incorporate it into the rest of my game and then on top of that, try to display the updated values of the timer, no values in the UI are updated and the printout of the timer doesn't even occur in the terminal. (我有一个可以自行运行计时器的类,但是当我将其合并到游戏的其余部分中,然后再在其上进行操作时,请尝试显示计时器的更新值,UI中的任何值都不会更新并且不会打印输出的计时器甚至不在终端中发生。)
I've tried running the timer in the same thread as the game-setup process and I've also tried creating a new thread to run the timer but neither work. (我尝试在与游戏设置过程相同的线程中运行计时器,并且还尝试创建一个新的线程来运行计时器,但均无效。)
The game loads up and functions fine, with the exception of the timer not counting upwards and not displaying the updated timer values. (游戏加载并正常运行,但计时器不向上计数且不显示更新的计时器值。)
Where am I going wrong here? (我在哪里错了?)
Here is my standalone Timer class, which again, works fine by itself.
(这是我的独立Timer类,再次可以正常工作。)
from PyQt5 import QtCore
import sys
def startThread(functionName, *args, **kwargs):
print(args)
if len(args) == 0:
t = threading.Thread(target=functionName)
else:
try:
t = threading.Thread(target=functionName, args=args, kwargs=kwargs)
except:
try:
if args is None:
t = threading.Thread(target=functionName, kwargs=kwargs)
except:
t = threading.Thread(target=functionName)
t.daemon = True
t.start()
class Timer(object):
app = QtCore.QCoreApplication(sys.argv)
def __init__(self):
super(Timer, self).__init__()
def start_timer(self):
print("Starting timer...")
Timer.timer = QtCore.QTimer()
Timer.time = QtCore.QTime(0, 0, 0)
Timer.timer.timeout.connect(self.tick)
Timer.timer.start(1000)
Timer.app.exec_()
def tick(self):
Timer.time = Timer.time.addSecs(1)
self.update_UI('%s' % Timer.time.toString("hh:mm:ss"))
def update_UI(self, text_string):
print(text_string)
# This is where the text would be sent to try and update the UI
Timer().start_timer()
This is more or less how my game-setup class is structured - currently I'm showing the version that uses threading:
(这或多或少是我的游戏设置类的结构方式-当前,我正在显示使用线程的版本:)
class BuildUI(PyQt5.QtWidgets.QMainWindow, Ui_game):
def __init__(self):
super(BuildUI, self).__init__()
self.setupUi(self)
self.easy_mode = 38
self.user_available_cells = []
self.start_easy_game.triggered.connect(lambda: self.setup_game(self.easy_mode))
def setup_game(self, hidden_count):
def create_game_board():
startThread(Timer().start_timer)
self.game = BuildGame()
#The BuildGame class is not deliberately not shown
startThread(create_game_board)
class GAME(object):
def __init__(self):
GAME.app = PyQt5.QtWidgets.QApplication(sys.argv)
GAME.UI = BuildUI()
GAME.UI.show()
GAME.app.exec_()
def main():
GAME()
if __name__ == '__main__':
main()
ask by NLee23 translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…