로그인

검색

Python
2013.11.30 18:44

[GUI] wxPython 기본 프로그램

MoA
조회 수 4669 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄 첨부

시작하기 전에


GUI 프로그래밍에서 중요한 요소 중의 하나는 이벤트이다.

이벤트를 다루기 위해서는 꼭 클래스의 기본 개념에 대해서 알고 있어야 한다.

그러므로 이 강좌를 시작하기 전 클래스에 대한 내용을 숙지하도록 한다.


소스 코드


import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        self.Bind(wx.EVT_MOVE, self.OnMove)
        self.Bind(wx.EVT_SIZE, self.OnSize)

    def OnSize(self, event):
        size = event.GetSize()
        print "size:", size.width, size.height

    def OnMove(self, event):
        pos = event.GetPosition()
        print "pos:", pos.x, pos.y



class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "This is a test")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True


def main():
    app = MyApp(0)
    app.MainLoop()


if __name__ == "__main__":
    main()


분석


소스는 wxPython 홈페이지에서 제공하는 minimal 어플리케이션이다. 처음부터 하나씩 분석해보자.


import wx

class MyFrame(wx.Frame):

wxPython 모듈을 불러오는 부분과 클래스를 정의하는 부분이다.

클래스를 정의하는 부분에서 wx.Frame 클래스를 상속받는 부분이 있는데 wx.Frame을 위젯이라 부른다.

대표적인 위젯으로 wx.Frame과 wx.Dialog 두 가지가 있는데 다음의 특징이 있다.


wx.Frame
- 사용자가 window의 사이즈와 위치를 변경할 수 있다.
- Title bar를 가지고, menu bar, status bar등은 옵션으로 가질 수 있다.
- Frame은 frame이나 다이얼로그가 아닌 다른 window를 가질 수 있다.


wx.Dialog
- Title bar와 system menu를 가진 window이다.
- 사용자가 해당 dialog를 이동할 수 있다.
- 다른 control과 다른 window를 가질 수 있다.


Visual Studio로 MFC Application을 만들어본 사람이라면 쉽게 두 가지를 구분할 수 있을 것이다.

(SDI, MDI vs Dialog)


    def __init__(self, parent, id, title):
         wx.Frame.__init__(self, parent, id, title)
         self.Bind(wx.EVT_MOVE, self.OnMove)
         self.Bind(wx.EVT_SIZE, self.OnSize)

초기화하는 부분이다. 먼저 wx.Frame 클래스의 초기화 함수로 초기화를 수행 후 이벤트를 바인딩한다.

바인딩이란 1:1 대응을 시키는 거라고 생각하면 된다.

wx.EVT_MOVE, wx.EVT_SIZE 이벤트는 각각 창 이동, 창 사이즈 변경 이벤트이다.

이를 MyFrame 클래스의 OnMove, OnSize 함수에 바인딩하였다.


    def OnSize(self, event):
        size = event.GetSize()
        print "size:", size.width, size.height

창의 사이즈가 변경될 때 실행되는 코드이다.

변경 후 사이즈 값을 얻어온 후 콘솔창에 출력한다.


    def OnMove(self, event):
        pos = event.GetPosition()
        print "pos:", pos.x, pos.y

창이 이동될 때 실행되는 코드이다.

이동 후 x, y 좌표값을 콘솔창에 출력한다.


class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "This is a test")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

프로그램의 메인이 되는 클래스이다.


wx.App 위젯을 상속받아 선언하였다.

OnInit은 프로그램이 실행될 때 호출되며 앞서 선언한 클래스를 보여주게 된다.

title 매개변수를 This is a test로 하였으므로 프로그램 제목이 This is a test가 된다.

SetTopWindow는 지금 알 필요는 없으며 굳이 선언하지 않아도 된다.

말그대로 프로그램의 Top이 되는 윈도우를 설정하는 함수이며 호출하지 않으면 처음 선언한 프레임을 top으로 설정한다.

?

  1. Programming 게시판 관련

    Date2014.11.01 CategoryTool/etc ByMoA Views22874
    read more
  2. Golang Tutorial for Node.js Developers, Part I.: Getting started

    Date2022.06.16 Category서버 ByOBG Views3320
    Read More
  3. What's the difference between comma separated joins and join on syntax in MySQL?

    Date2022.06.09 CategoryDatabase ByOBG Views3688
    Read More
  4. Building Pitaya, Wildlife’s own scalable game server framework

    Date2022.06.07 Category서버 ByOBG Views3676
    Read More
  5. How to send dynamic charts with a Slack bot

    Date2022.05.31 CategoryWeb ByOBG Views3413
    Read More
  6. [Javascript] 비동기, Promise, async, await 확실하게 이해하기

    Date2022.05.27 CategoryWeb ByOBG Views3613
    Read More
  7. Address Bar Install for Progressive Web Apps on the Desktop

    Date2021.12.15 CategoryWeb ByOBG Views3538
    Read More
  8. 추천(Recommendation) 시스템 - 알고리즘 Trend 정리

    Date2021.08.03 CategoryDeeplearning ByOBG Views4156
    Read More
  9. What does set -e mean in a bash script?

    Date2021.04.29 CategoryTool/etc ByOBG Views4523
    Read More
  10. What does the last “-” (hyphen) mean in options of `bash`?

    Date2021.04.29 CategoryTool/etc ByOBG Views4044
    Read More
  11. 2016년에 자바스크립트를 배우는 기분

    Date2016.12.27 CategoryTool/etc ByMoA Views3659
    Read More
  12. 서비스중인 게임 DB 설계(쿠키런) 기초

    Date2016.07.12 CategoryDatabase ByMoA Views4439
    Read More
  13. PHP: 잘못된 디자인의 프랙탈

    Date2016.07.10 CategorySite ByMoA Views4669
    Read More
  14. Windows 10 앱 개발(UWP)

    Date2015.10.13 CategoryAPI/MFC ByMoA Views4963
    Read More
  15. 정신나간 정렬 알고리즘

    Date2015.10.13 CategoryC/C++ ByMoA Views4701
    Read More
  16. 비트윈 PC 버전 개발기

    Date2015.10.11 CategorySite ByMoA Views4789
    Read More
  17. Machine Learning for Video Games

    Date2015.07.27 CategoryTool/etc ByMoA Views5102
    Read More
  18. [액션게임 만들기] 10. 캐릭터 기술 구현

    Date2014.05.07 CategoryPython ByMoA Views4505
    Read More
  19. [액션게임 만들기] 9. 캐릭터 액션 구현 2

    Date2014.05.07 CategoryPython ByMoA Views4360
    Read More
  20. [액션게임 만들기] 8. 캐릭터 액션 구현 1

    Date2014.05.07 CategoryPython ByMoA Views4283
    Read More
  21. [액션게임 만들기] 7. 캐릭터 출력

    Date2014.05.07 CategoryPython ByMoA Views4572
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17