티스토리 뷰

PySide Binaries OS X

To install PySide in Mac OS X, you need to install both PySide and the appropriate Qt version.


PySide 1.2.1 / Qt 4.8

  • Qt for Mac OS X Download:

qt-mac-opensource-4.8.5.dmg


PySide를 설치하기 전에 먼저 Qt 4.8.5를 설치한다.

Note: PySide is not yet compatible with Qt 5.x. Please use Qt 4.8 instead.

Qt는 컴퓨터 프로그래밍에서 GUI 프로그램 개발에 널리 쓰이는 크로스 플랫폼 프레임워크이다. 


  • PySide 1.2.1 for Python 2.7 Download:

pyside-1.2.1-qt4.8.5-py27apple-developer-signed.pkg


Verify Installation

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import PySide
 
print(PySide.__version__)
cs
> 1.2.1

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import PySide.QtCore
 
print(PySide.QtCore.__version__)
cs
> 4.8.5

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import PySide
 
print (PySide.QtCore.qVersion())
cs

> 4.8.5



Hello-World-in-PySide


hello.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
cs


With PySide desktop applications, you must always start your file by importing PySide.QtCore and PySide.QtGui classes. These classes have the main functions for building PySide applications. For instance, PySide.QtGui contains functions for dealing with widgets while PySide.QtCore contains methods for handling signals and slots, and controlling the application.

After the imports, you create a QApplication which is the main Qt application. As Qt can receive arguments from command line, you must pass any arguments to the QApplication object. Usually, you don't need to pass any arguments so you can leave it as it is.

After the creation of the application object, we have created a QLabel object. A QLabel is a widget that can present text (simple or rich, like html), and images. Note that after the creation of the label, we are calling the method show which will present the label to the user.

Finally, we call app.exec_() which will enter the Qt main loop and start to execute the Qt code. In reality, it is only here that the label will be shown, but this can be ignored for now.

PySide 프로그램을 작성하려면 PySide.QtCore, PySide.QtGui에 있는 클래스를 가져옵니다. 여기에서 가져오는 클래스에는 PySide 프로그램을 작성하기 위해 필요한 주요 기능이 포함되어 있습니다. 예를 들어 PySide.QtGui는 위젯을 사용하기 위한 함수를 담고 있으며, PySide.QtCore는 시그널-슬롯을 위한 메서드와 프로그램을 제어하기 위한 메서드를 담고 있습니다.

가져온 후에는 QApplication을 생성하며, 이는 Qt 프로그램의 가장 중요한 클래스입니다. Qt는 명령행에서 인자를 받을 수 있으므로 QApplication 객체를 생성할 때 이 인자를 그대로 전달해야 합니다. 아직까지는 코드 내에서 인자를 사용하지는 않으므로, 특별히 신경쓸 부분은 없습니다.

QApplicatino 객체를 생성한 후에는 QLabel 객체를 생성합니다. QLabel은 텍스트나 그림을 표시하는 위젯입니다. 일반 텍스트 및 HTML 형식을 사용하는 서식 있는 텍스트를 표시할 수 있습니다. QLabel을 생성한 다음 show 메서드를 호출하는 것에 주목하세요. 이 메서드를 호출하면 QLabel이 사용자에게 표시됩니다.

마지막으로 app.exec_() 를 호출합니다. Qt의 메인 루프에 진입하여 본격적으로 Qt 프레임워크의 코드가 실행됩니다. 사실은 QLabel이 실제로 사용자에게 보여지도록 하는 처리는 여기에서 이루어집니다. 하지만 아직은 자세한 것에는 신경쓰지 않아도 됩니다.

Displaying html in the label

As mentioned previously, you can insert html tags in the label to present rich text. Try changing the code which creates the label for something like:
위에서 설명했듯이, 서식 있는 텍스트를 표현하기 위해 HTML 태그를 삽입할 수 있습니다. QLabel을 생성하는 부분을 아래처럼 수정해 보세요:
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("<font color=red size=40>Hello World</font>")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
cs
and you will the the "Hello World" now bigger and red. You can try to change to another color, another size, or make it blink! Or you can create other elements instead of the QLabel, like QPushButton, or others.




Hello-World-in-PySide-and-QtQuick


QML is a declarative language designed to describe the user interface of a program: both what it looks like, and how it behaves. In QML, a user interface is specified as a tree of objects with properties. In this tutorial we will show how you can make a simple Hello World application with PySide and QML.

A PySide/QML application consists, at least, of two different files - a file with the QML description of the user interface, and a python file which loads the qml file. To avoid problems for now, don't forget to save both files in the same folder.

Here is a simple QML file, called view.qml:

QML은 프로그램 상에서 유저 인터페이스를 기술하기 위해 만들어진 선언형 언어입니다. 즉 무엇을 표시할 지, 어떻게 동작할 지에 대해 선언형으로 프로그래밍을 합니다. QML상에서 유저 인터페이스는 속성(Property)를 갖는 오브젝트가 하나의 트리 구조를 갖도록 규정됩니다. 이 튜토리얼에서는 PySide와 QML을 이용하여 Hello World 어플리케이션을 작성하는 방법에 대해 설명합니다.

PySide/QML 어플리케이션을 생성하기 위해서는 최소한 두 개의 파일을 작성해야 합니다. 하나는 유저 인터페이스를 기술하는 QML 파일이고 다른 하나는 qml 파일을 로드하기 위한 파이썬 파일입니다. 지금 여기서는 이 두 파일을 같은 폴더에 저장하도록 하여 괜한 문제가 일어나지 않도록 합시다.

아래와 같이 QML 코드를 작성하여 view.qml 라는 이름으로 저장합니다:

import QtQuick 1.0
 
Rectangle {
    width: 200
    height: 200
    color: "black"
    Text {
        text: "Hello World"
        color: "white"
        anchors.centerIn: parent
    }
}
cs



We start by importing QtQuick 1.0, since in theory, QtQuick has a different release schedule than Qt/PySide.

The rest of the QML code is pretty straightforward for those who have previously used HTML or XML files. Basically, we are creating a red rectangle with the size 200*200 and, inside that rectangle, we are adding a Text element which says Hello World. The code anchors.centerIn: parent just makes the text appear centered in relation to its immediate parent.

Now, let's see how the code looks on the PySide. Let's call it main.py:

첫 문장에서 Qt 4.7을 임포트합니다. QtQuick이 Qt/PySide와 빌드 스케쥴이 분리됨에 따라, 머지 않아 import QtQuick 1.0 로 표기법이 바뀔 예정입니다. 하지만 당분간은 위와 같이 임포트합니다.

그 아래부터는 코드가 HTML이나 XML 파일과 상당히 유사한 형식으로 되어 있는 것을 알 수 있습니다. 간단히, 200*200의 크기를 갖는 빨간색 사각형을 만들어 보도록 하겠습니다. 사각형 안에는 Hello World 텍스트를 추가합니다. 텍스트가 사각형의 정중앙에 위치하도록, anchors.centerIn: parent 코드를 추가합니다.

다음엔, main.py 라는 이름으로 PySide 코드를 작성합니다 :

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import QDeclarativeView
 
# Create Qt application and the QDeclarative view
app = QApplication(sys.argv)
view = QDeclarativeView()
# Create an URL to the QML file
url = QUrl('view.qml')
# Set the QML file and show
view.setSource(url)
view.show()
# Enter Qt main loop
sys.exit(app.exec_())
cs

If you are already familiar with PySide and have followed our tutorials, much of this code is already familiar. The only novelties are that you must import QDeclarativeView and set the source of the QDeclarativeView object to the URL of your QML file. Then, as any Qt widget, you call QDeclarativeView.show().

Hint: If you are programming for desktop, you should consider adding view.setResizeMode(QDeclarativeView.SizeRootObjectToView) before showing the view. This will force the outer QML rectangle to resize along with the outer window.

PySide 튜터리얼 등을 통해서 이미 PySide에 익숙한 사람이라면, 위의 코드 역시 큰 지장 없이 이해할 수 있을 것입니다. 다른 것이 있다면, QDeclarativeView를 임포트하고 위에서 만든 QML 파일의 URL을 QDeclarativeView의 소스로 설정하는 부분입니다. 그 후에는 다른 Qt 위젯과 마찬가지로 QDeclarativeView.show() 를 호출합니다.

참고: 만약 모바일환경이 아닌 데스크탑에서 프로그램을 작성하려면 뷰를 표시하기 전에 view.setResizeMode(QDeclarativeView.SizeRootObjectToView) 를 호출하여, 윈도우 크기가 변경됨에 따라 QML 표시 영역도 따라서 변경되도록 설정합니다.



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

(PEP 8) Style Guide for Python Code  (0) 2016.08.15
Python Modules  (0) 2016.01.16
Scapy 사용하기  (0) 2015.04.11
Scapy 설치하기  (0) 2015.04.11
PySide - Combine (Show, About, Close)  (0) 2015.04.07
PySide - Show Licence (File Open)  (0) 2015.04.07
PySide - About Box  (0) 2015.04.07
PySide - Close Button  (0) 2015.04.05
PySide - QMessageBox  (0) 2015.04.05
쉘 스크립트(shell script)의 시작 #!(shebang)  (0) 2014.11.14
댓글
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
링크
공지사항
Total
Today
Yesterday