import cv2 import sys, time, os import random import numpy as np from PyQt5 import QtCore, QtGui, QtWidgets from test_Qthread_0716_ui import Ui_MainWindow from PyQt5.QtCore import QDate,QTime from PyQt5.QtWidgets import QApplication, QFileDialog, QLabel,QMainWindow, QWidget, QPushButton from PyQt5.QtCore import QThread, pyqtSignal class ReadTime(QtCore.QThread): # 讀取時間 time_out = pyqtSignal(str) # 聲明一個帶字串參數的信號 def __init__(self, parent=None): super().__init__(parent) def run(self): while True: result = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 讀取當下時間 self.time_out.emit(f'{result}') # 傳送信号 self.msleep(500) # 休眠0.5秒 class img_out(QtCore.QThread): img_data = QtCore.pyqtSignal(np.ndarray) data=pyqtSignal(str) def __init__(self, parent=None): super().__init__(parent) self.img_path_list=['img/1.jpg','img/2.jpg','img/3.jpg'] def run(self): data_list=["stone","scissors","cloth"] while True: img_num = random.randint(0,len(self.img_path_list)-1) img = cv2.imread(self.img_path_list[img_num]) self.img_data.emit(img) self.data.emit(data_list[img_num]) time.sleep(1) # 轉Qlabel的格式 def label_to_view(img): # 原圖 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換 Ny, Nx, _ = img.shape img = QtGui.QImage(img.data, Nx, Ny, Nx * 3, QtGui.QImage.Format_RGB888) # 須改格式 return img class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): #按鍵設定 super(MainWindow, self).__init__(parent) self.setupUi(self) self.time = ReadTime() self.time.start() self.time.time_out.connect(self.settimeText) self.person_1 = "" self.person_2 = "" self.img_out_1 = img_out() self.img_out_1.start() self.img_out_1.img_data.connect(self.show_view_1) self.img_out_1.data.connect(self.show_data_1) self.img_out_2 = img_out() self.img_out_2.start() self.img_out_2.img_data.connect(self.show_view_2) self.img_out_2.data.connect(self.show_data_2) def settimeText(self, str_in): str_in = f'{str_in}' self.view_time.setText(str_in) def show_view_1(self,img): img = label_to_view(img) self.view_1.setPixmap(QtGui.QPixmap.fromImage(img)) def show_view_2(self,img): img = label_to_view(img) self.view_2.setPixmap(QtGui.QPixmap.fromImage(img)) def show_data_1(self,str): self.person_1 = str self.who_win() def show_data_2(self,str): self.person_2 = str self.who_win() def who_win(self): if self.person_1=="scissors" and self.person_2 =="cloth": self.textEdit.setText("Person_1_Win") if self.person_1=="stone" and self.person_2 =="scissors": self.textEdit.setText("Person_1_Win") if self.person_1=="cloth" and self.person_2 =="stone": self.textEdit.setText("Person_1_Win") if self.person_2=="scissors" and self.person_1 =="cloth": self.textEdit.setText("Person_2_Win") if self.person_2=="stone" and self.person_1 =="scissors": self.textEdit.setText("Person_2_Win") if self.person_2=="cloth" and self.person_1 =="stone": self.textEdit.setText("Person_2_Win") if self.person_1 ==self.person_2: self.textEdit.setText("NO_WIN") if __name__=='__main__': app = QtWidgets.QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_())