콘솔워크

[pyqt5] 로그창이 있는 기본적인 형태의 GUI 본문

프로그래밍/python

[pyqt5] 로그창이 있는 기본적인 형태의 GUI

이휘재123 2022. 7. 13. 16:26
반응형

pip install pyqt5 설치 필요

 

from tkinter import *  # __all__
import sys
from tracemalloc import start
import warnings
warnings.simplefilter("ignore", UserWarning)
sys.coinit_flags = 2
from tkinter import HORIZONTAL, Button, Scrollbar
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from datetime import *


class MainUI(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


    def log_append(self, text):
        now = str(datetime.now())[0:-7]
        self.browser.append(f'[{now}] {text}')
    

    def initUI(self):

        # 상단 좌측
        top_left_groupbox = QGroupBox('상단 좌측')

        top_left_inner_layout = QHBoxLayout()

        top_left_groupbox.setLayout(top_left_inner_layout)

        # 상단 우측
        top_right_groupbox = QGroupBox('상단 우측')

        top_right_inner_layout = QHBoxLayout()

        top_right_groupbox.setLayout(top_right_inner_layout)


        # 중단 좌측
        center_left_groupbox = QGroupBox('중단 좌측')

        center_left_inner_layout = QHBoxLayout()

        center_left_groupbox.setLayout(center_left_inner_layout)


        # 중단 우측
        center_right_groupbox = QGroupBox('중단 우측')

        center_right_inner_layout = QHBoxLayout()

        center_right_groupbox.setLayout(center_right_inner_layout)
    

        # 하단 (로그)
        bottom_groupbox = QGroupBox('로그')
        self.browser = QTextBrowser()

        bottom_inner_layout = QGridLayout()
        bottom_inner_layout.addWidget(self.browser)

        bottom_groupbox.setLayout(bottom_inner_layout)


        # 레이아웃 배치
        top_layout = QHBoxLayout()
        top_layout.addWidget(top_left_groupbox, 1)
        top_layout.addWidget(top_right_groupbox, 1)

        center_layout = QHBoxLayout()
        center_layout.addWidget(center_left_groupbox, 1)
        center_layout.addWidget(center_right_groupbox, 1)

        bottom_layout = QHBoxLayout()
        bottom_layout.addWidget(bottom_groupbox)

        layout = QGridLayout()
        layout.addLayout(top_layout, 0, 0)
        layout.addLayout(center_layout, 1, 0)
        layout.addLayout(bottom_layout, 2, 0)

        self.setLayout(layout)


        # 앱 기본 설정
        self.setWindowTitle('GUI sample')
        self.resize(1000, 800)
        self.center()
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainUI()
    sys.exit(app.exec_())

요소를 추가하고싶으면 각각의 inner_layout에 addWidget으로 추가해주면 된다.

 

위 코드 실행결과

 

 

 

반응형