PySide2 Convert UI to Python

Qt Designer

μœ„μ ― 배치

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

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

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

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

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

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

python3 -m PySide2.scripts.uic -x mainwindow.ui -o ui_mainwindow.py

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

ui_mainwindow.py
from PySide2 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(228, 218)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 3, 1, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 3, 2, 1, 1)
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setObjectName("textBrowser")
self.gridLayout.addWidget(self.textBrowser, 2, 1, 1, 2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 228, 18))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(
QtWidgets.QApplication.translate(
"MainWindow", "MainWindow", None, -1
)
)
self.pushButton.setText(
QtWidgets.QApplication.translate("MainWindow", "λ²„νŠΌ 1", None, -1)
)
self.pushButton_2.setText(
QtWidgets.QApplication.translate("MainWindow", "λ²„νŠΌ 2", None, -1)
)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Ui 클래슀 상속

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

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