[Python] PyQt 튜토리얼

Python 2014. 10. 8. 16:55

python 과 Qt를 이용하여 GUI 프로그래밍하는 법을 알아보도록 하겠습니다.


python Qt를 이용하여 GUI 프로그래밍 하는 방법을 알아보도록 하겠습니다.


1. Qt를 설치 합니다.

http://www.qt.io/download/




파일 다운로드 한 후 인스톨을 완료 합니다.


2. PyQt4 를 설치합니다.

http://www.riverbankcomputing.co.uk/software/pyqt/download




파일을 다운로드 한 후 인스톨을 완료 합니다. 

3. Qt를 이용하여 레이아웃을 작성해 봅시다.




Designer 를 실행한 후 Widget을 생성 합니다.


Vertical Layout을 선택하여 올립니다.

Layout 을 클릭한 후 빈공간에 오른쪽 마우스 버튼을 누른 후 배치>수평으로 배치 를 누릅니다.

그 후 버튼을 드래그 하여 레이아웃 위에 올립니다.

버튼을 클릭한 후 objectName을 수정하거나 objectName이 무엇인지 확인 합니다.


그 후 ui를 저장합니다.

ui 파일이 있는 곳에서 pyuic4 -o 파이썬파일(사용자가 정함).py  UI파일.ui 을 실행 하면 파이썬 파일이 생성 됩니다.

참고) 만일 실해이 되지 않는다면 C:\Python27\Lib\site-packages\PyQt4 에 ui 파일을 저장하고 위 명령어를 사용하면 작동합니다.

 


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Wed Oct 08 16:11:24 2014
#      by: PyQt4 UI code generator 4.9.6
#
# WARNING! All changes made in this file will be lost!

#coding!!
#=============================
import sys
#=============================

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget): # Ui_Form(object) =>  Ui_Form(QtGui.QWidget):

    #coding!!
    #=============================
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
    #=============================


    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.horizontalLayout = QtGui.QHBoxLayout(Form)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        self.horizontalLayout.addLayout(self.verticalLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "PushButton", None))

    #coding!!
    #=============================
        self.pushButton.clicked.connect(self.printQt)        


    def printQt(self):
        print "hello Qt"
    #=============================

#coding!!
#=============================    
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

#=============================    
    


주석 안에 있는 코드가 새로 넣은 코드 입니다.

위에 코드를 작성하고 저장한 뒤 파이썬 파일을 더블 클릭하면 실행됩니다.



Posted by ElvinKim
,