본 내용의 출처는 이글루 블로그의 '하린아빠' 라는 네임을 쓰시는 분의 블로그입니다.
[출처] - http://pythondev.egloos.com/102928
행여나 이글루 블로그가 없어지면 참고할 곳이 사라지기에 주인장님께 댓글을 남기고 퍼 옵니다.
[wxPython] 계산기 만들기
#!/usr/bin/python
# -*- coding: cp949 -*-
# layout.py
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(300,250))
self.formula = False
# 메뉴바 생성
menubar = wx.MenuBar()
# 파일 메뉴 생성
file = wx.Menu()
# 메뉴 추가
file.Append(22, '&Quit', 'Exit Calculator')
# 메뉴바에 file 메뉴 추가
menubar.Append(file, '&File')
# frame에 메뉴바 셋팅
self.SetMenuBar(menubar)
# close 메뉴 이벤트 연결
wx.EVT_MENU(self, 22, self.OnClose)
sizer = wx.BoxSizer(wx.VERTICAL)
# Text Editor 생성(오른쪽 정렬)
self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
# 박스에 Text Editor 추가
sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)
# Grid 박스 생성(가로, 세로, 가로여백, 세로여백)
gs = wx.GridSizer(4, 4, 3, 3)
# Grid 박스에 버튼 추가
gs.AddMany([(wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
(wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
(wx.StaticText(self, -1, ''), 0, wx.EXPAND),
(wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
(wx.Button(self, 1, '7'), 0, wx.EXPAND),
(wx.Button(self, 2, '8'), 0, wx.EXPAND),
(wx.Button(self, 3, '9'), 0, wx.EXPAND),
(wx.Button(self, 4, '/'), 0, wx.EXPAND),
(wx.Button(self, 5, '4'), 0, wx.EXPAND),
(wx.Button(self, 6, '5'), 0, wx.EXPAND),
(wx.Button(self, 7, '6'), 0, wx.EXPAND),
(wx.Button(self, 8, '*'), 0, wx.EXPAND),
(wx.Button(self, 9, '1'), 0, wx.EXPAND),
(wx.Button(self, 10, '2'), 0, wx.EXPAND),
(wx.Button(self, 11, '3'), 0, wx.EXPAND),
(wx.Button(self, 12, '-'), 0, wx.EXPAND),
(wx.Button(self, 13, '0'), 0, wx.EXPAND),
(wx.Button(self, 14, '.'), 0, wx.EXPAND),
(wx.Button(self, 15, '='), 0, wx.EXPAND),
(wx.Button(self, 16, '+'), 0, wx.EXPAND) ])
# GridSizer를 BoxSizer에 추가함
sizer.Add(gs, 1, wx.EXPAND)
# Frame에 BoxSizer를 셋팅함
self.SetSizer(sizer)
# Frame 중앙 정렬
self.Centre()
# 이벤트 연결
self.Bind(wx.EVT_BUTTON, self.OnClear, id=20)
self.Bind(wx.EVT_BUTTON, self.OnBackspace, id=21)
self.Bind(wx.EVT_BUTTON, self.OnClose, id=22)
self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
self.Bind(wx.EVT_BUTTON, self.OnDivide, id=4)
self.Bind(wx.EVT_BUTTON, self.OnFour, id=5)
self.Bind(wx.EVT_BUTTON, self.OnFive, id=6)
self.Bind(wx.EVT_BUTTON, self.OnSix, id=7)
self.Bind(wx.EVT_BUTTON, self.OnMultiply, id=8)
self.Bind(wx.EVT_BUTTON, self.OnOne, id=9)
self.Bind(wx.EVT_BUTTON, self.OnTwo, id=10)
self.Bind(wx.EVT_BUTTON, self.OnThree, id=11)
self.Bind(wx.EVT_BUTTON, self.OnMinus, id=12)
self.Bind(wx.EVT_BUTTON, self.OnZero, id=13)
self.Bind(wx.EVT_BUTTON, self.OnDot, id=14)
self.Bind(wx.EVT_BUTTON, self.OnEqual, id=15)
self.Bind(wx.EVT_BUTTON, self.OnPlus, id=16)
# 화면 클리어
def OnClear(self, event):
self.display.Clear()
# BackSpace 버튼 이번트 처리함수
def OnBackspace(self, event):
# 현재 display의 값을 보관한다.
formula = self.display.GetValue()
# Display 화면 클리어
self.display.Clear()
# 맨오른쪽 값을 뺀 나머지 값을 셋팅함
self.display.SetValue(formula[:-1])
# 창닫음 버튼의 이벤트 처리
def OnClose(self, event):
# Frame 닫기
self.Close()
# 나누기 버튼 이벤트 처리
def OnDivide(self, event):
if self.formula:
return
self.display.AppendText('/')
# 곱하기 버튼의 이벤트 처리
def OnMultiply(self, event):
if self.formula:
return
self.display.AppendText('*')
# 빼기 버튼의 이벤트 처리
def OnMinus(self, event):
if self.formula:
return
self.display.AppendText('-')
# 더하기 버튼의 이벤트 처리
def OnPlus(self, event):
if self.formula:
return
self.display.AppendText('+')
# 소수점 버튼의 이벤트 처리
def OnDot(self, event):
if self.formula:
return
self.display.AppendText('.')
# 이퀄버튼의 이벤트 처리
def OnEqual(self, event):
if self.formula:
return
formula = self.display.GetValue()
self.formula = False
try:
self.display.Clear()
output = eval(formula)
self.display.AppendText(str(output))
except StandardError:
self.display.AppendText("Error")
# '0'버튼의 이벤트 처리
def OnZero(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('0')
# '1'버튼의 이벤트 처리
def OnOne(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('1')
# '2'버튼의 이벤트 처리
def OnTwo(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('2')
# '3'버튼의 이벤트 처리
def OnThree(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('3')
# '4'버튼의 이벤트 처리
def OnFour(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('4')
# '5'버튼의 이벤트 처리
def OnFive(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('5')
# '6'버튼의 이벤트 처리
def OnSix(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('6')
# '7'버튼의 이벤트 처리
def OnSeven(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('7')
# '8'버튼의 이벤트 처리
def OnEight(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('8')
# '9'버튼의 이벤트 처리
def OnNine(self, event):
if self.formula:
self.display.Clear()
self.formula = False
self.display.AppendText('9')
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'calculator.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = wx.App()
mainapp = MyApp(app)
mainapp.MainLoop()
[실행 화면]
'Language > Python' 카테고리의 다른 글
[wxPython] 기본 Object - 커서(Cursor) (0) | 2017.02.06 |
---|---|
[wxPython] wx.GridBagSizer를 이용한 wx.Button 정렬하기 (0) | 2017.02.06 |
[wxPython] wx.BoxSizer 이용하기 (0) | 2017.02.01 |
[wxPython] wx.Button(버튼) 만들기 (0) | 2017.02.01 |
[wxPython] wx.ToolBar 만들기 (0) | 2017.02.01 |