본문 바로가기

Language/Python

[wxPython] wx.ManuBar 만들기(체크, 라디오, 서브 매뉴)

본 내용의 출처는 이글루 블로그의 '하린아빠' 라는 네임을 쓰시는 분의 블로그입니다.

[출처] - http://pythondev.egloos.com/84591


행여나 이글루 블로그가 없어지면 참고할 곳이 사라지기에 주인장님께 댓글을 남기고 퍼 옵니다.




[wxPython] wx.ManuBar 만들기(체크, 라디오, 서브 매뉴)



#!/usr/bin/python

# menu1.py

import wx

class MyMenu(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(200, 150))

        # menubar 생성
        menubar = wx.MenuBar()
        # file 메뉴 생성
        file = wx.Menu()
        # edit 메뉴 생성
        edit = wx.Menu()
        # help 메뉴 생성
        help = wx.Menu()
        # file / Open 추가
        file.Append(101, '&Open', 'Open a new document')
        # file / Save 추가
        file.Append(102, '&Save', 'Save the document')
        # file에 구분선 추가
        file.AppendSeparator()
        # quit 메뉴 아이템 생성
        quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the Application')
        # quit 메뉴에 Bitmap 아이콘 추가 (ConvertToBitmap -> png를 bitmap으로 변경)
        quit.SetBitmap(wx.Image('icons/stock_exit-16.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
        # file / Quit 추가
        file.AppendItem(quit)
        # edit 메뉴 추가(CheckBox 형식)
        edit.Append(201, 'check item1', '', wx.ITEM_CHECK)
        edit.Append(202, 'check item2', kind=wx.ITEM_CHECK)
        # submenu 생성
        submenu = wx.Menu()
        # submenu 추가(Radio 형식)
        submenu.Append(301, 'radio item1', kind=wx.ITEM_RADIO)
        submenu.Append(302, 'radio item2', kind=wx.ITEM_RADIO)
        submenu.Append(303, 'radio item3', kind=wx.ITEM_RADIO)
        # edit menu에 submenu 추가
        edit.AppendMenu(203, 'submenu', submenu)

        # menubar에 생성한 menu들을 추가함
        menubar.Append(file, '&File')
        menubar.Append(edit, '&Edit')
        menubar.Append(help, '&Help')
        # menubar 셋팅
        self.SetMenuBar(menubar)
        # 중앙 정렬
        self.Center()
        # 105번 메뉴를 OnQuit 이벤트 함수와 연결
        self.Bind(wx.EVT_MENU, self.OnQuit, id=105)

        # 상태바를 생성한다.
        self.CreateStatusBar()


    # Quit 이벤트 함수

    def OnQuit(self, event):
        self.Close()


class MyApp(wx.App):
    def OnInit(self):
        # menu를 가진 frame 생성
        frame = MyMenu(None, -1, 'menu1.py')
        frame.Show(True)
        return True

app = wx.App()
mainapp = MyApp(app)
mainapp.MainLoop()

[실행 결과]