PySide2 Convert UI to Python

Qt Designer

μœ„μ ― 배치

  • 기본적인 화면을 κΎΈλ―ΈκΈ° μœ„ν•΄ Main Window 폼을 μƒμ„±ν•©λ‹ˆλ‹€.
  • μœ„μ ― μƒμžμ—μ„œ μ‚¬μš©ν•˜κ³ μ‹Άμ€ μœ„μ ―μ„ μ„ νƒν•˜μ—¬ μ λ‹Ήν•œ μœ„μΉ˜μ— λ°°μΉ˜ν•©λ‹ˆλ‹€.
  • 창을 μ„ νƒν•˜κ±°λ‚˜ 객체 νƒμƒ‰κΈ°μ—μ„œ MainWindowλ₯Ό μ„ νƒν•˜λ©΄ 전체 λ ˆμ΄μ•„μ›ƒ 선택이 ν™œμ„±ν™”λ©λ‹ˆλ‹€. μ›ν•˜λŠ” λ ˆμ΄μ•„μ›ƒμ„ μ„ νƒν•©λ‹ˆ λ‹€.
  • λ ˆμ΄μ•„μ›ƒμ— 맞좰 μœ„μ ―μ„ μž¬λ°°μΉ˜ν•©λ‹ˆλ‹€.
  • μΆ”κ°€μ μœΌλ‘œ ν•„μš”ν•œ μœ„μ ―μ΄λ‚˜ λ ˆμ΄μ•„μ›ƒμ„ μΆ”κ°€ν•©λ‹ˆλ‹€.

μœ„μ ― 속성 λ³€κ²½

  • μˆ˜μ •ν•  μœ„μ ―μ„ 폼 μ—λ””ν„°μ—μ„œ μ„ νƒν•˜κ±°λ‚˜, 객체 νƒμƒ‰κΈ°μ—μ„œ μ„ νƒν•©λ‹ˆλ‹€.
  • 속성 νŽΈμ§‘κΈ°μ—μ„œ μˆ˜μ •ν•˜κ³  싢은 λ‚΄μš©μ„ μˆ˜μ •ν•©λ‹ˆλ‹€. objectName을 μˆ˜μ •ν•˜λ©΄ νŒŒμ΄μ¬μ—μ„œ ν•΄λ‹Ή μ΄λ¦„μœΌλ‘œ μœ„μ ―μ— μ ‘κ·Όν•  수 있 μŠ΅λ‹ˆλ‹€.

νŒŒμ΄μ¬μ—μ„œ UI μ ‘κ·Όν•˜κΈ°

UI νŒŒμΌμ„ 파이썬 클래슀둜 λ³€ν™˜

νŒŒμΌμ„ mainwindow.ui둜 μ €μž₯ν•œ 경우 μ•„λž˜ λͺ…λ Ήμ–΄λ‘œ ui_mainwindow.py둜 λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

pyside2-uic mainwindow.ui -o ui_mainwindow.py

λ³€ν™˜λœ νŒŒμΌμ€ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

ui_mainwindow.py
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'mainwindow.ui'
##
## Created by: Qt User Interface Compiler version 5.15.0
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
QPixmap, QRadialGradient)
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(240, 220)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(u"gridLayout")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName(u"pushButton")
self.gridLayout.addWidget(self.pushButton, 3, 1, 1, 1)
self.pushButton2 = QPushButton(self.centralwidget)
self.pushButton2.setObjectName(u"pushButton2")
self.gridLayout.addWidget(self.pushButton2, 3, 2, 1, 1)
self.textBrowser = QTextBrowser(self.centralwidget)
self.textBrowser.setObjectName(u"textBrowser")
self.gridLayout.addWidget(self.textBrowser, 2, 1, 1, 2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 240, 20))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"PushButton", None))
self.pushButton2.setText(QCoreApplication.translate("MainWindow", u"PushButton", None))
# retranslateUi

Ui 클래슀 상속

GUI와 κΈ°λŠ₯을 λΆ„λ¦¬ν•˜κΈ° μœ„ν•΄ Ui 클래슀λ₯Ό 상속받은 클래슀λ₯Ό λ‹€λ₯Έ μ†ŒμŠ€μ— μƒμ„±ν•©λ‹ˆλ‹€.

import sys
from PySide2.QtWidgets import QMainWindow, QApplication
from ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
"""
Ui 클래슀 상속
"""
def __init__(self):
super().__init__()
"""
Ui μ„€μ •
"""
self.setupUi(self)
self.pushButton.setText("λ²„νŠΌ 1")
self.pushButton2.setText("λ²„νŠΌ 2")
"""
λ²„νŠΌ 클릭 μ‹œκ·Έλ„μ— 슬둯 μΆ”κ°€
"""
self.pushButton.clicked.connect(self.pushButtonClicked)
self.pushButton2.clicked.connect(self.pushButton2Clicked)
def pushButtonClicked(self):
"""
self.pushButton 클릭에 λŒ€ν•œ κΈ°λŠ₯ κ΅¬ν˜„
"""
pass
def pushButton2Clicked(self):
"""
self.pushButton2 클릭에 λŒ€ν•œ κΈ°λŠ₯ κ΅¬ν˜„
"""
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
Last updated on