본문 바로가기

Language/Python

[wxPython] 미리 정의된 다이얼로그(Dialog)

파이썬의 GUI 를 뭘 쓸까 알아보다가 wxpython이 나은 듯 하여 검색하다 좋은 정보를 발견하여 퍼온다.

혹시나, 행여나, 만약에 해당 사이트가 폐쇄가? 될 경우도 있을 듯 하여 주인장님의 블로그에 퍼가겠다는 의사를 적어놓고 내용 그대로 copy & paste 해옵니다.


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




[wxPython] 미리 정의된 다이얼로그(Dialog)

# -*- coding: cp949 -*-
#!/usr/bin/python

# commondialogs.py

import wx
import os, sys


class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        
        self.CreateStatusBar()
        # 메뉴바 생성
        menuBar = wx.MenuBar()
        # 메뉴 추가
        menu = wx.Menu()
        menu.Append(99,  "&Message Dialog", "Shows a Message Dialog")
        menu.Append(100, "&Color Dialog", "Shows a Color Dialog")
        menu.Append(101, "&File Dialog", "Shows a File Dialog")
        menu.Append(102, "&Page Setup Dialog", "Shows a Page Setup Dialog")
        menu.Append(103, "&Font Dialog", "Shows a Font Dialog")
        menu.Append(104, "&Directory Dialog", "Shows a Directory Dialog")
        menu.Append(105, "&SingleChoice Dialog", "Shows a SingleChoice Dialog")
        menu.Append(106, "&TextEntry Dialog", "Shows a TextEntry Dialog")
        menuBar.Append(menu, "&Dialogs")
        # 프레임에 메뉴 셋팅
        self.SetMenuBar(menuBar)
        
        # 메뉴별 이벤트 함수 연결
        self.Bind(wx.EVT_MENU, self.message, id=99)
        self.Bind(wx.EVT_MENU, self.choosecolor, id=100)
        self.Bind(wx.EVT_MENU, self.openfile, id=101)
        self.Bind(wx.EVT_MENU, self.pagesetup, id=102)
        self.Bind(wx.EVT_MENU, self.choosefont, id=103)
        self.Bind(wx.EVT_MENU, self.opendir, id=104)
        self.Bind(wx.EVT_MENU, self.singlechoice, id=105)
        self.Bind(wx.EVT_MENU, self.textentry, id=106)


    # 메세지 다이얼 로그 띄우기
    def message(self, event):
        # 메시지 다이얼로그 생성(이벤트 처리기)
        dlg = wx.MessageDialog(self, 'To save one life is as if you have saved the world.', 'Talmud', wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()


    # 색상 다이얼 로그 띄우기(이벤트 처리기)
    def choosecolor(self, event):
        # 생상 다이얼로그 생성
        dlg = wx.ColourDialog(self)
        # 풀 색상 선택창 사용 유무 지정(True : 사용 / False : 미사용)
        dlg.GetColourData().SetChooseFull(False)
        # 색상 다이얼로그에서 색상을 선택후 ok 버튼을 누르면
        if dlg.ShowModal() == wx.ID_OK:
            # 선택한 색상값을 얻엄
            data = dlg.GetColourData()
            # 선택한 색상 값을 Frame의 상태바에 출력함
            self.SetStatusText('You selected: %s\n' % str(data.GetColour().Get()))
       # 색상 다이얼로그 파괴
        dlg.Destroy()


   # 파일 오픈 다이얼로그 띄우기(이벤트 처리기)
    def openfile(self, event):
        # 파일 오픈 다이얼로그 생성
        dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN)
       # 파일 다이얼로그에서 파일을 선택후 ok 버튼을 누르면
        if dlg.ShowModal() == wx.ID_OK:
                # 파일의 경로를 구함
                path = dlg.GetPath()
                # 파일 경로에서 파일명만 추출함
                mypath = os.path.basename(path)
               # 선택한 파일의 이름을 Frame의 상태바에 출력함
                self.SetStatusText("You selected: %s" % mypath)
       # 파일 오픈 다이얼로그 파괴
        dlg.Destroy()


    # 페이지 설정 다이얼로그 띄우기(이벤트 처리기)    
    def pagesetup(self, event):
        # 페이지 설정 다이얼로그 생성
        dlg = wx.PageSetupDialog(self)
       # 페이지  다이얼로그에서 페이지 설정 후 ok 버튼을 누르면        
        if dlg.ShowModal() == wx.ID_OK:
           # 페이지 설정 데이타 클래스 구함
            data = dlg.GetPageSetupData()
            # (상단, 왼쪽) 여백을 구함
            tl = data.GetMarginTopLeft()
            # (하단, 오른쪽) 여백을 구함
            br = data.GetMarginBottomRight()
            # 설정한 페이지 설정값을 Frame의 상태바에 출력함
            self.SetStatusText('Margins are: %s %s' % (str(tl), str(br)))
        # 페이지 설정 다이얼로그 파괴
        dlg.Destroy()


   # 폰트 선택 다이얼로그 띄우기(이벤트 처리기)
    def choosefont(self, event):
        # 기본 폰트 생성
        default_font = wx.Font(10, wx.SWISS , wx.NORMAL, wx.NORMAL, False, "Verdana")
        # 폰트정보 클래스 생성
        data = wx.FontData()
        # 시스템의 플랫폼이 Win32라면
        if sys.platform == 'win32':
            #폰트 이펙트 표시함.(False : 표시하지 않음)
            data.EnableEffects(True)
       # 심볼 폰트 선택 할수 없음
        data.SetAllowSymbols(False)
       # 기본 폰트로 초기화함
        data.SetInitialFont(default_font)
       # 폰트 크기 지정 범위
        data.SetRange(10, 30)
       # 폰트 다이얼로그 생성
        dlg = wx.FontDialog(self, data)
       # 폰트  다이얼로그에서 폰트 설정 후 ok 버튼을 누르면
        if dlg.ShowModal() == wx.ID_OK:
           # 설정한 폰트 정보를 얻음
            data = dlg.GetFontData()
            # 설정한 폰트를 구함
            font = data.GetChosenFont()
            # 설정한 폰트 색상을 구함
            color = data.GetColour()
           # 설정한 페이지 설정값을 Frame의 상태바에 출력함
            text = 'Face: %s, Size: %d, Color: %s' % (font.GetFaceName(), font.GetPointSize(),  color.Get())
            self.SetStatusText(text)
       # 폰트 다이얼로그 파괴            
        dlg.Destroy()

   # 디렉토리 다이얼로그 띄우기    

    def opendir(self, event):
        # 디렉토리 다이얼로그 생성
        dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
       # 디렉토리   다이얼로그에서 디렉토리 선택 후 ok 버튼을 누르면 
        if dlg.ShowModal() == wx.ID_OK:
           # 선택한 디렉토리의 경로를 Frame의 상태바에 출력함
            self.SetStatusText('You selected: %s\n' % dlg.GetPath())
        # 디렉토리 다이얼로그 파괴
        dlg.Destroy()

   # 선택 다이얼로그 띄우기

    def singlechoice(self, event):
        # 리스트
        sins = ['Greed', 'Lust', 'Gluttony', 'Pride', 'Sloth', 'Envy', 'Wrath']
       # 선택 다이얼로그 띄우기
        dlg = wx.SingleChoiceDialog(self, 'Seven deadly sins', 'Which one?', sins, wx.CHOICEDLG_STYLE)
       # 선택   다이얼로그에서 항목 하나를  선택 후 ok 버튼을 누르면
        if dlg.ShowModal() == wx.ID_OK:
           # 선택한 항목을 Frame의 상태바에 출력함            
            self.SetStatusText('You chose: %s\n' % dlg.GetStringSelection())
        # 선택5 다이얼로그 파괴
        dlg.Destroy()

   # 텍스트 입력 다이얼로그 띄우기

    def textentry(self, event):
       # 텍스트 다이얼로그 생성
        dlg = wx.TextEntryDialog(self, 'Enter some text','Text Entry')
        # "Default" 글자를 텍스트 상자에 입력함.
        dlg.SetValue("Default")
        # 텍스트 입력  다이얼로그에서 항목 하나를  선택 후 ok 버튼을 누르면
        if dlg.ShowModal() == wx.ID_OK:
            # 입력한 항목을 Frame의 상태바에 출력함
            self.SetStatusText('You entered: %s\n' % dlg.GetValue())
       # 텍스트 입력 다이얼로그 파괴
        dlg.Destroy()

class MyApp(wx.App):
    def OnInit(self):
        myframe = MyFrame(None, -1, "commondialogs.py")
        myframe.CenterOnScreen()
        myframe.Show(True)
        return True



[실행 화면]

<<메인 윈도우>>




<< 메시지 다이얼로그 (wx.MessageDialog ) >>



<< 색상 다이얼로그 (wx.ColourDialog ) >>




<< 파일 다이얼로그 (wx.FileDialog ) >>



<< 페이지 다이얼로그 (wx.PageSetupDialog ) >>




<< 폰트 다이얼로그 (wx.FontDialog ) >>




<< 디렉토리 다이얼로그 (wx.DirDialog ) >>




<< 선택 다이얼로그 (wx.SingleChoiceDialog ) >>