KevinFire2030 / Fire2025

0 stars 0 forks source link

18.1.3 PyTrader 구현 #8

Open KevinFire2030 opened 1 year ago

KevinFire2030 commented 1 year ago

form_class 로드하기

코드


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

form_class = uic.loadUiType("pytrader.ui")[0]

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

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()

해석

from Kiwoom import *
form_class = uic.loadUiType("pytrader.ui")[0]

uic.loadUiType 메서드를 통해 해당 ui 파일을 class 형태로 변환합니다. 0번은 Ui_MainWindow 클래스 이고, 1번은 PyQt5.QtWidgets.QMainWindow인데, uic.loadUiType(form)[0]을 통해 0번 으로 선택하여 form_class 변수에 입력해줍니다.

KevinFire2030 commented 1 year ago

import , import as, from import, from import * 의 차이

import로 라이브러리에서 수학 모듈 math를 들고 와서 원주율을 출력하면

import math
math.pi
#출력 : 3.141592653589793

모듈 함수는 "모듈. 함수()" 형식으로 사용

import math
math.sqrt(4.0)
#출력: 2.0
math.sqrt(2.0)
#출력: 1.4142135623730951

import as import 모듈 as 이름

모듈의 함수를 사용할 때 math.sqrt처럼 함수마다 math를 입력하기 귀찮습니다.

그래서 import as로 모듈의 이름을 지정할 수 있습니다.

import math as m
m.sqrt(4.0)
#출력 : 2.0
m.sqrt(2.0
#출력 : 1.4142135623730951

from import from 모듈 import 변수 from 모듈 import 함수 from 모듈 import 클래스 from 모듈 import 변수, 함수, 클래스 from 모듈 import *

import as로 이름을 지정하는 거보다 편한 방법이 있습니다.

그건 바로 from import인데 from 뒤에 모듈 이름을 지정하고 import 뒤에 가져올 변수를 입력합니다.

그럼 이후에 가져온 변수를 사용할 때 pi와 같이 모듈 이름을 붙이지 않아도 사용 가능합니다

from math import pi
pi
#출력 : 3.141592653589793

이번에는 함수를 가져와서 사용해보겠습니다.

from math import sqrt
sqrt(4.0)
#출력 : 2.0
sqrt(2.0)
#출력 : 1.4142135623730951
이와 같이 math를 붙이지 않고 함수를 바로 사용할 수 있습니다.
from math import pi, sqrt
pi
#출력 : 3.141592653589793
sqrt(4.0)
#출력 : 2.0
sqrt(2.0)
#출력 : 1.4142135623730951
이와 같이 두 개를 한꺼번에 가져올 수도 있습니다

from import *
하지만 변수, 함수, 클래스가 수십 개라면 어떻게 될까요?

from import *을 입력하면 모듈의 모든 변수, 함수, 클래스를 가져올 수 있습니다.

```python
from math import *
pi
#출력 : 3.141592653589793
sqrt(4.0)
#출력 : 2.0
sqrt(2.0)
#출력 : 1.4142135623730951
*(asterisk, 애스터리스크) 기호는 보통 컴퓨터에서 모든 것이라는 뜻으로 사용됩니다.

from import as from 모듈 import 변수 as 이름 from 모듈 import 함수 as 이름 from 모듈 import 클래스 as 이름 from 모듈 import 변수 as 이름 1, 함수 as 이름 2, 클래스 as 이름 3

math모듈에서 sqrt함수의 이름을 s로 지정합니다.

from math import sqrt as s
s(4.0)
#출력 : 2.0
s(2.0)
#출력 : 1.4142135623730951

여러 개를 가져왔을 때 각각 이름 저장하는 법

이번에는 math모듈의 pi를 가져오면서 이름은 p로, sqrt은 이름을 s로 지정합니다

from math import pi as p, sqrt as s
p
#출력 : 3.141592653589793
s(4.0)
#출력 : 2.0
s(2.0)
#출력 : 1.4142135623730951
KevinFire2030 commented 1 year ago

statusBar 위젯에 서버 연결 상태 및 현재 시간 출력

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

        """
        self.a = a
        self.b = b

        self.c = pysum(self.a, self.b)

        print(self.a, self.b, self.c)

        """

        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

    def timeout(self):
        current_time = QTime.currentTime()
        text_time = current_time.toString("hh:mm:ss")
        time_msg = "현재시간: " + text_time

        state = self.kiwoom.get_connect_state()
        if state == 1:
            state_msg = "서버 연결 중"
        else:
            state_msg = "서버 미 연결 중"

        self.statusbar.showMessage(state_msg + " | " + time_msg)