프로젝트

일반

사용자정보

Actions

At 업비트 개요창

소스코드

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from pyupbit import WebSocketManager

class OverViewWorker(QThread):
    dataMidSent = pyqtSignal(int, float, float)
    data24Sent = pyqtSignal(float, int, float, int, int)

    def __init__(self, ticker):
        super().__init__()
        self.ticker = ticker
        self.alive = True

    def run(self):
        wm = WebSocketManager("ticker", [f"{self.ticker}"])
        while self.alive:
            data = wm.get()

            self.dataMidSent.emit(int(data['trade_price']),  # 현재가
                               float(data['signed_change_rate'] * 100),      # 등락률
                               float(data['acc_bid_volume'] / data['acc_ask_volume'] * 100))  # 채결강도 Volume Power = Bid Volume/Ask Volume × 100%

            self.data24Sent.emit(float(data['acc_trade_volume_24h']),  # 거래량
                           int(data['high_price']),       # 고가(당일)
                           float(data['acc_trade_price_24h']),         # 거래금액
                           int(data['low_price']),        # 저가(당일)
                           int(data['prev_closing_price']))  # 전일종가

        wm.terminate() # 해당코드가 없으면 종료가 제대로 이루어지지않음.

    def close(self):
        self.alive = False

class OverviewWidget(QWidget):
    def __init__(self, parent=None, ticker="KRW-BTC"):
        super().__init__(parent)
        uic.loadUi("resource/overview.ui", self)
        self.setWindowTitle("실시간 개요창")

        self.ticker = ticker

        self.label_3.setText("거래량(24H)")
        self.label_5.setText("고가(당일)")
        self.label_7.setText("거래금액(24H)")
        self.label_9.setText("저가(당일)")
        self.label_11.setText("채결강도(24H)")
        self.label_13.setText("전일종가")

        self.ovw = OverViewWorker(ticker)
        self.ovw.dataMidSent.connect(self.fillMidData)
        self.ovw.data24Sent.connect(self.fill24Data)
        self.ovw.start()

    def closeEvent(self, event):
        self.ovw.close()

    def fillMidData(self, currPrice, chgRate, volumePower):
        self.label_1.setText(f"{currPrice:,}")
        self.label_2.setText(f"{chgRate:+.2f}%")
        self.label_12.setText(f"{volumePower:.2f}%")
        self.__updateStyle()

    def fill24Data(self, volume, highPrice, value, lowPrice, prevClosePrice):
        self.label_4.setText(f"{volume:,.3f} {"BTC"}")
        self.label_6.setText(f"{highPrice:,}")
        self.label_8.setText(f"{value/100000000:,.1f} 억")
        self.label_10.setText(f"{lowPrice:,}")
        self.label_14.setText(f"{prevClosePrice:,}")
        self.__updateStyle()

    def __updateStyle(self):
        if '-' in self.label_2.text():
            self.label_1.setStyleSheet("color:blue;")
            self.label_2.setStyleSheet("background-color:blue;color:white")
        else:
            self.label_1.setStyleSheet("color:red;")
            self.label_2.setStyleSheet("background-color:red;color:white")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ob = OverviewWidget()
    ob.show()
    exit(app.exec_())

결과

이태훈이(가) 10달 전에 변경 · 1 revisions