[Python] PyQT5 기본템플릿비교(Widget, Dialog, Main Window)
by 담배맛구마QT Desinger를 보면 뭐가 많다. 뭐가 다른지 모르겠어서 그냥 코드를 봤다.
1. 대충 정리
결론적으로 말하면 Application Object가 다를 뿐이다.
QtWidgets.QWidget() # Widget
QtWidgets.QDialog() # Dialog
QtWidgets.QMainWindow() # Main Window
2. 조금 설명
Widget, Dialog, Main Window 모두 Widget이다. Main Windows는 상태바랑 메뉴바 같은 걸 넣을 수 있어 말그대로 Win32 API로 Application 만드는 거랑 똑같다고 보면 되겠고, Widget이랑 Dialog는 비슷하지만 Dialog가 좀더 Top-Level이래... 이건 구글링해봐야 자세히 적을 듯
3. 코드를 보자
PyQT에서 Widget / Dialog / Main Window 뭐가 다른지 몰라서 일단 ui파일을 python으로 변환해서 보자
Widget
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(431, 241)
self.addBtn = QtWidgets.QPushButton(Form)
self.addBtn.setGeometry(QtCore.QRect(350, 10, 75, 23))
self.addBtn.setObjectName("addBtn")
self.textBox = QtWidgets.QLineEdit(Form)
self.textBox.setGeometry(QtCore.QRect(10, 10, 331, 20))
self.textBox.setObjectName("textBox")
self.textList = QtWidgets.QListView(Form)
self.textList.setGeometry(QtCore.QRect(10, 40, 331, 192))
self.textList.setObjectName("textList")
self.exitBtn = QtWidgets.QPushButton(Form)
self.exitBtn.setGeometry(QtCore.QRect(350, 40, 75, 191))
self.exitBtn.setObjectName("exitBtn")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "This is Widget"))
self.addBtn.setText(_translate("Form", "ADD"))
self.exitBtn.setText(_translate("Form", "EXIT"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Dialog
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(429, 241)
self.addBtn = QtWidgets.QPushButton(Dialog)
self.addBtn.setGeometry(QtCore.QRect(350, 10, 75, 23))
self.addBtn.setObjectName("addBtn")
self.textBox = QtWidgets.QLineEdit(Dialog)
self.textBox.setGeometry(QtCore.QRect(10, 10, 331, 20))
self.textBox.setObjectName("textBox")
self.testList = QtWidgets.QListView(Dialog)
self.testList.setGeometry(QtCore.QRect(10, 40, 331, 192))
self.testList.setObjectName("testList")
self.exitBtn = QtWidgets.QPushButton(Dialog)
self.exitBtn.setGeometry(QtCore.QRect(350, 40, 75, 191))
self.exitBtn.setObjectName("exitBtn")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.addBtn.setText(_translate("Dialog", "ADD"))
self.exitBtn.setText(_translate("Dialog", "EXIT"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Main Window
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(430, 244)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.addBtn = QtWidgets.QPushButton(self.centralwidget)
self.addBtn.setGeometry(QtCore.QRect(350, 10, 75, 23))
self.addBtn.setObjectName("addBtn")
self.textBox = QtWidgets.QLineEdit(self.centralwidget)
self.textBox.setGeometry(QtCore.QRect(10, 10, 331, 20))
self.textBox.setObjectName("textBox")
self.textList = QtWidgets.QListView(self.centralwidget)
self.textList.setGeometry(QtCore.QRect(10, 40, 331, 192))
self.textList.setObjectName("textList")
self.exitBtn = QtWidgets.QPushButton(self.centralwidget)
self.exitBtn.setGeometry(QtCore.QRect(350, 40, 75, 191))
self.exitBtn.setObjectName("exitBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.addBtn.setText(_translate("MainWindow", "ADD"))
self.exitBtn.setText(_translate("MainWindow", "EXIT"))
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_())
반응형
'Dev-' 카테고리의 다른 글
[Python] Geoip를 통한 IP 별 국가코드 매칭 (2) | 2016.07.09 |
---|---|
[Python] PyQT4 ComboBox와 TableWidget 제어 (0) | 2016.04.09 |
[Python] Send Mail by my gmail account (0) | 2016.01.10 |
[Python] PyQT5 시작하기 (6) | 2015.10.26 |
[Python] Log Gathering (0) | 2015.10.23 |
블로그의 정보
정윤상이다.
담배맛구마