PyQt5加载uic子窗口时界面卡顿问题求助
问题:打开游戏窗口时界面卡顿
我已按照您的指导操作,但问题仍未解决:打开第二个游戏窗口时界面出现卡顿。我保留了if __name__ == "__main__"中的app = ...代码,仅在主文件中创建第二个窗口的调用变量。现将相关代码重新贴出供您审阅:
main.py代码片段:
# pyttsx3 处理指令 elif 'game' in query: games_window_open = show_window("games", True) # 通过win32检查游戏窗口是否已运行,未运行则返回False # 首次打开时games_window_open为False if games_window_open == False: second_window = UI_Games() elif games_window_open: self.ai_dialogue_update.emit("已有一个游戏窗口正在运行") speak("已有一个游戏窗口正在运行")
游戏窗口文件代码:
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton from PyQt5 import uic from PyQt5.QtCore import QThread import sys # 忽略这些小类,它们是可从UI子窗口中以线程方式运行的游戏 class Snake(QThread): def start_game(self): import Games.sanke_game class Space(QThread): def space(self): import Games.space_invader class Target(QThread): def target(self): import Games.target_practice # 子窗口类UI_Games class UI_Games(QMainWindow): def __init__(self): super(UI_Games, self).__init__() # 加载UI文件 uic.loadUi("Resources\\UI_files\\game_window_UI.ui", self) # 定义控件 self.snakeGame = self.findChild(QPushButton, "snakeGame") self.targetGame = self.findChild(QPushButton, "targetGame") self.guessGame = self.findChild(QPushButton, "guessGame") self.spaceGame = self.findChild(QPushButton, "spaceGame") self.ticTacToeGame = self.findChild(QPushButton, "ticTacToeGame") self.snakeGame.clicked.connect(lambda: self.game_choose("snake")) self.targetGame.clicked.connect(lambda: self.game_choose("target")) self.guessGame.clicked.connect(lambda: self.game_choose("guess")) self.spaceGame.clicked.connect(lambda: self.game_choose("space")) self.ticTacToeGame.clicked.connect(lambda: self.game_choose("tic-tac-toe")) # 显示窗口 self.show() # 选择游戏的函数 def game_choose(self, choice): if choice == "target": self.space_worker = Target() self.space_worker.target() elif choice == "snake": self.snake_worker = Snake() self.snake_worker.start_game() elif choice == "guess": pass elif choice == "tic-tac-toe": pass elif choice == "space": space_worker = Space() space_worker.space() if __name__ == "__main__": app = QApplication(sys.argv) UIWindow = UI_Games() app.exec_()
卡顿界面截图:

内容的提问来源于stack exchange,提问作者user20084690
相关产品推荐
相关产品推荐

