2024-04-26 20:25:31 发布
网友
我是Python新手。我正在尝试更改按钮上的图像,当它被单击时,我希望在执行任何其他代码之前呈现新图像。我尝试使用锁和信号量等,但没有任何工作(例如,下面代码中的do_pause函数似乎是在图像呈现之前执行的)。我试图使用单独的线程,但是join()挂起了程序。就像是在一个无限循环中,但我不知道为什么。在
找到了一个解决方案(不太确定它为什么起作用)。我将game_button_click事件中t1.join()之后的所有内容移动到另一个名为check_for_match的方法。然后我改变了游戏中的代码“按钮”点击事件。dou暂停和show_图像代码基本保持不变。就像我说的,不知道为什么有用,但它确实有用。新代码如下:
# Game button click event def game_button_click(self, index): lock = Lock() with lock: self.show_image(index) t = Thread(target=self.check_for_match, args=(index,)) t.start() # function to check for matches def check_for_match(self, index): self.clicks += 1 if self.clicks % 2 == 0 and self.uncovered % 2 == 0: self.moves += 1 moves = self.MOVE_TEXT if self.moves == 1 else self.MOVES_TEXT self.lbl_moves.config(text=str(self.moves) + moves) if self.scrambled_names[self.prev_index] == self.scrambled_names[index]: self.uncovered += 2 else: self.do_pause() self.buttons[self.prev_index].config(image=self.default_img, command=lambda myIndex=self.prev_index: self.game_button_click(myIndex)) self.buttons[index].config(image=self.default_img, command=lambda myIndex=index: self.game_button_click(myIndex)) self.prev_index = index
找到了一个解决方案(不太确定它为什么起作用)。我将game_button_click事件中t1.join()之后的所有内容移动到另一个名为check_for_match的方法。然后我改变了游戏中的代码“按钮”点击事件。dou暂停和show_图像代码基本保持不变。就像我说的,不知道为什么有用,但它确实有用。新代码如下:
相关问题 更多 >
编程相关推荐