728x90
반응형
if __name__ == '__main__':
    from PySide2.QtCore import QCoreApplication
    QCoreApplication.setLibraryPaths(['C:\\Users\\rogue\\OneDrive\\바탕 화면\\cpt\\python\\test\\venv\\Lib\\site-packages\\PySide2\\plugins'])

# import imp
from PySide2.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout,
                               QLabel, QFrame, QSizePolicy, QPushButton,
                               QFileDialog, QMessageBox, QLineEdit, QComboBox,
                               QSpinBox, QSlider, QProgressBar,
                               QCheckBox, QGroupBox, QTextEdit)

from PySide2.QtGui import (QPixmap, QImage, QColorSpace, QIntValidator, 
                           QDoubleValidator, QRegExpValidator)

from PySide2.QtCore import QRegExp, Qt, QThread

import sys
import serial
import time

class WorkerSumP1(QThread):
    def __init__(self, low, high, widget):
        super().__init__()
        
        self.low = low
        self.high = high
        self.widget:QTextEdit = widget
        
    def run(self) -> None:
        
        # while True:
        #     print(f'스레드 시작 {self.low}부터 {self.high}까지 덧셈')
            
        #     now = time.time()
        #     total = 0
        #     for i in range(self.low, self.high):
        #         total += i
        #     # print('sum: ', total)
            
        #     delta = time.time() - now
            
        #     resultMSG = f'스레드 끝: 결과는 {total}, 걸린시간: {delta}sec'
            
        print(f'스레드 시작 {self.low}부터 {self.high}까지 덧셈')
        now = time.time()

        total = 0
        for i in range(self.low, self.high):
            total += i
        # print('sum: ', total)

        delta = time.time() - now

        resultMSG = f'스레드끝: 결과는 {total}, 걸린시간: {delta}sec'
        print(resultMSG)

        self.widget.append(resultMSG)    
        
        pass
    
    pass

# class WorkerSumP2(QThread):
#     def __init__(self, low, high, widget):
#         super().__init__()
        
#         self.low = low
#         self.high = high
#         self.widget:QTextEdit = widget
        
#     def run(self) -> None:
            
#         print(f'스레드 시작 {self.low}부터 {self.high}까지 덧셈')
#         now = time.time()

#         total = 0
#         for i in range(self.low, self.high):
#             total += i
#         # print('sum: ', total)

#         delta = time.time() - now

#         resultMSG = f'스레드끝: 결과는 {total}, 걸린시간: {delta}sec'
#         print(resultMSG)

#         self.widget.append(resultMSG)    
        
#         pass
    
#     pass

class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        # 사용자 입력값 받고 출력
        self.inputLineEdit = QLineEdit()
        self.printTextEdit = QTextEdit()
        self.printTextEdit.setReadOnly(True)
        
        self.inputLineEdit.returnPressed.connect(self.sendMessage)
        
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.inputLineEdit)
        mainLayout.addWidget(self.printTextEdit)
        
        self.setLayout(mainLayout)
        self.setWindowTitle("시리얼 통신")
        self.setMinimumSize(500, 500)
        # self.resize(500, 500)

        pass
    
    def sendMessage(self):
        inputText = self.inputLineEdit.text()

        # 입력된 문구가 숫자인지 파악
        
        
        # 입력된 문구를 숫자형으로 변경
        
        
        # 입력된 문구가 일정 범위 안에 있는지 파악
                
        # 숫자를 문자로 변경
        
        # 시리얼 통신을 통해 문자를 전달
        
        # 스레드 생성 및 시작
        self.workerSumP1 = WorkerSum(1, 100000000, self.printTextEdit)
        self.workerSumP1.start()

        self.inputLineEdit.setText('')
        
        # self.printTextEdit.setText(self.printTextEdit.toPlainText() + inputText)
        # self.printTextEdit.append(inputText)
        
        pass
    
# # Thread Test code
# def sum(row, high):
#     while True:
    
#         total = 0
#         for i in range(row, high):
#             total += i
#         print('sum: ', total)
        
        

PORT = "COM8"
BAUDRATE = 9600

ser:serial.Serial = None

def prepare():
    global ser
    
    ser = serial.Serial(PORT, baudrate=BAUDRATE)
    
    pass

if __name__ == '__main__':
    prepare()
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    app.exec_()

    pass
728x90
반응형

'Qt' 카테고리의 다른 글

login  (0) 2022.11.04
QPushbutton exam..  (0) 2022.11.03
[Qt] Login.py _linux  (0) 2022.11.03
Serial Communication (from Arduino to Python)  (0) 2022.11.01
qt_groupbox_frame.py  (0) 2022.10.28

+ Recent posts