로그인

검색

Python
2013.11.30 18:44

[GUI] wxPython 기본 프로그램

MoA
조회 수 1139 추천 수 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으로 설정한다.

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 Tool/etc Programming 게시판 관련 2 MoA 2014.11.01 3876
67 Deeplearning 추천(Recommendation) 시스템 - 알고리즘 Trend 정리 OBG 2021.08.03 327
66 Tool/etc 에디트 플러스, VS 2008 컴파일 환경 설정 너울 2012.04.02 322
65 Site Start Something! - Windows 8 개발 공식 사이트 Naya 2012.08.02 317
64 Deeplearning 강화학습 학습 관련 정리 OBG 2022.08.10 300
63 Deeplearning Top 3 most used Pytorch Ecosystem Libraries you should Know about OBG 2023.08.02 291
62 Tool/etc What does the last “-” (hyphen) mean in options of `bash`? OBG 2021.04.29 289
61 Database What's the difference between comma separated joins and join on syntax in MySQL? OBG 2022.06.09 289
60 Tool/etc How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04 OBG 2023.01.17 270
59 Web Address Bar Install for Progressive Web Apps on the Desktop OBG 2021.12.15 268
58 Deeplearning RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED ... OBG 2022.09.06 256
57 Deeplearning The State of AI & Art 2022 1 OBG 2022.10.06 249
56 서버 Building Pitaya, Wildlife’s own scalable game server framework OBG 2022.06.07 244
55 Web How to send dynamic charts with a Slack bot OBG 2022.05.31 225
54 Site 모든 개발자를위한 10 가지 특별한 GitHub 리포지토리 OBG 2023.12.28 217
53 Web Creating A Fixed-Length Queue In JavaScript Using Arrays OBG 2022.09.14 215
52 Tool/etc How to stop programmers to copy the code from GitHub when they leave the company? OBG 2024.01.02 215
51 Deeplearning 추천 시스템 OBG 2023.03.30 213
50 서버 SSH-Tunneling을 통한 MySQL 서버 연결 OBG 2023.04.21 213
49 Tool/etc AWS 망 분리하기 OBG 2022.09.06 209
48 Deeplearning Stable Diffusion OBG 2022.09.27 205
Board Pagination Prev 1 ... 6 7 8 9 10 11 12 13 14 15 Next
/ 15