군침이 싹 도는 코딩

PySide2 레이아웃 ( Grid Layout ) 본문

Python/Pycharm

PySide2 레이아웃 ( Grid Layout )

mugoori 2023. 5. 26. 14:57
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *

class Form(QWidget):
    def __init__(self):
        super(Form, self).__init__()
        # V 박스 생성
        self.vb = QVBoxLayout()
        # 메인 레이아웃으로 V 박스를 지정
        self.setLayout(self.vb)

        # 라인 에딧 생성
        self.ln = QLineEdit()
        # 라인 에딧의 정렬 속성을 오른쪽 정렬로 변경
        self.ln.setAlignment(Qt.AlignRight)
        # 라인 에딧의 폰트 크기와 굵기를 조절
        self.ln.setStyleSheet('font-size: 24px;' 'font-weight: bold;')
        # V 박스에 라인 에딧 넣기
        self.vb.addWidget(self.ln)

        # 그리드 레이아웃 생성
        self.gl = QGridLayout()
        # V 박스에 그리드 레이아웃 넣기
        self.vb.addLayout(self.gl)

        # 그리드 레이아웃에 들어갈 벨류값 세팅
        self.value = [[QPushButton('++'),[0,0]],
                      [QPushButton('/'),[0,1]],
                      [QPushButton('*'),[0,2]],
                      [QPushButton('-'),[0,3]],
                      [QPushButton('1'),[1,0]],
                      [QPushButton('2'),[1,1]],
                      [QPushButton('3'),[1,2]],
                      [QPushButton('+'),[1,3, 2, 1]],
                      [QPushButton('4'),[2,0]],
                      [QPushButton('5'),[2,1]],
                      [QPushButton('6'),[2,2]],
                      [QPushButton('7'),[3,0]],
                      [QPushButton('8'),[3,1]],
                      [QPushButton('9'),[3,2]],
                      [QPushButton('='),[3,3, 2, 1]],
                      [QPushButton('0'),[4,0, 1, 2]],
                      [QPushButton('.'),[4,2]]]

        # 버튼의 크기 정책 설정을 바꿔준다 ( 버튼은 초기 설정에서는 크기를 변경할 수 없다 )
        for i, p in self.value:
            if len(p) > 2:
                i.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
                self.gl.addWidget(i, p[0], p[1], p[2], p[3])
            else:
                self.gl.addWidget(i, p[0], p[1])
            i.setStyleSheet('font-size: 20px;' 'font-weight: bold;')
            i.clicked.connect(self.clk)

    def clk(self):
        self.ln.setText(self.ln.text()+self.sender().text())


app = QApplication([])
form = Form()
form.show()
sys.exit(app.exec_())

'Python > Pycharm' 카테고리의 다른 글

pycharm 을 이용해서 패키징을 할 때 오류 해결  (0) 2023.06.08
PySide2 레이아웃 ( Form Layout )  (0) 2023.05.26
간단한 팁  (0) 2023.05.26
Pyside2 레이아웃 ( Box Layout )  (0) 2023.05.26
PyQt5 배포 파일 만들기  (0) 2023.05.26