(Python을 이용한 Quant 투자) PyQt

Posted by : at

Category : Python



PyQt

  • GetCode

  • 트레이딩 시스템 구축을 위해 GUI 기반으로 동작해야 하기에 PyQt를 사용한다

import sys
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)    # QApplication 객체 생성

'''
label = QLabel("Hello")
label.show()
'''
btn = QPushButton("Hello")
btn.show()

app.exec_()                     # 이벤트 루프 생성


통상적으로 PyQt는 아래와 같이 시작된다.

import sys
from PyQt5.QtWidgets import *

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 200, 300, 400)
        self.setWindowTitle("PyQt")
        # self.setWindowIcon(QIcon("icon.png"))

        # 버튼추가
        btn = QPushButton("버튼1", self)
        btn.move(10, 10)

        btn2 = QPushButton("버튼2", self)
        btn2.move(10, 40)

        # 버튼에 이벤트 연결
        btn.clicked.connect(self.btn_clicked)

    def btn_clicked(self):
        print("버튼 클릭")

app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()


Qt Designer 활용

  • GetCode

  • Qt Designer를 통해 .ui파일을 생성한다

.ui파일을 사용해보자

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic

from_class = uic.loadUiType("./qtDesignerTest.ui")[0]

class MyWindow(QMainWindow, from_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()


이벤트 연결하기

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic

from_class = uic.loadUiType("./qtDesignerTest.ui")[0]

class MyWindow(QMainWindow, from_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.eventBtn.clicked.connect(self.btn_clicked)

    def btn_clicked(self):
        print("버튼 클릭")

app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()

Example - 코인시세받아오기

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import pykorbit

from_class = uic.loadUiType("./qtDesignerTest.ui")[0]

class MyWindow(QMainWindow, from_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.getCoinBtn.clicked.connect(self.inquiry)

    def inquiry(self):
        price = pykorbit.get_current_price("BTC")
        self.lineEdit.setText(str(price))

app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()


QTimer

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
 
 
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)
 
    def timeout(self):
        cur_time = QTime.currentTime()
        str_time = cur_time.toString("hh:mm:ss")
        self.statusBar().showMessage(str_time)
 
 
app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import *
import pykorbit
 
form_class = uic.loadUiType("window.ui")[0]
 
class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
 
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.inquiry)
 
    def inquiry(self):
        cur_time = QTime.currentTime()
        str_time = cur_time.toString("hh:mm:ss")
        self.statusBar().showMessage(str_time)
        price = pykorbit.get_current_price("BTC")
        self.lineEdit.setText(str(price))
 
app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()

Singal and Slot

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class MySignal(QObject):
    signal1 = pyqtSignal()

    def run(self):
        self.signal1.emit()


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        mysignal = MySignal()
        mysignal.signal1.connect(self.signal1_emitted)
        mysignal.run()

    @pyqtSlot()
    def signal1_emitted(self):
        print("signal1 emitted")


app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class MySignal(QObject):
    signal1 = pyqtSignal()
    signal2 = pyqtSignal(int, int)  # 이런식으로 데이터를 내보낼 수 있음.

    def run(self):
        self.signal1.emit()
        self.signal2.emit(1, 2)


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        mysignal = MySignal()
        mysignal.signal1.connect(self.signal1_emitted)
        mysignal.signal2.connect(self.signal2_emitted)
        mysignal.run()

    @pyqtSlot()
    def signal1_emitted(self):
        print("signal1 emitted")

    @pyqtSlot(int, int)
    def signal2_emitted(self, arg1, arg2):
        print("signal2 emitted", arg1, arg2)


app = QApplication(sys.argv)
window = MyWindow()
window.show()
app.exec_()

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links