로그인

검색

Python
2013.11.30 18:44

[GUI] wxPython 기본 프로그램

MoA
조회 수 6236 추천 수 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 Views35832
    read more
  2. Keras를 활용한 주식 가격 예측

    Date2022.09.02 CategoryDeeplearning ByOBG Views5529
    Read More
  3. 강화학습 학습 관련 정리

    Date2022.08.10 CategoryDeeplearning ByOBG Views5643
    Read More
  4. 직접 보고 추천하는 머신러닝 & 딥러닝 & 수학 총정리(2022)

    Date2022.07.24 CategoryDeeplearning ByOBG Views10110
    Read More
  5. 파이썬 머신러닝 무료 강의 (7시간)

    Date2022.07.06 CategoryDeeplearning ByOBG Views10288
    Read More
  6. "Node.js를 떠나며" - express를 만든 TJ의 글

    Date2022.06.23 CategoryTool/etc ByOBG Views5423
    Read More
  7. Golang Tutorial for Node.js Developers, Part I.: Getting started

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

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

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

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

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

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

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

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

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

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

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

    Date2016.07.10 CategorySite ByMoA Views6216
    Read More
  19. Windows 10 앱 개발(UWP)

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

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

    Date2015.10.11 CategorySite ByMoA Views6389
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17