Created
December 10, 2012 11:09
-
-
Save mozillazg/4250018 to your computer and use it in GitHub Desktop.
简单的 Python 计算器(wxPython)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
import wx | |
def onclick(event): | |
button = event.GetEventObject() | |
label = button.GetLabel() | |
nums = [str(i) for i in range(10)] + ['.', '+', '-', 'x', u'÷'] | |
# print repr(label) | |
if label in nums: | |
xyz.AppendText(label) | |
elif label == 'clear': | |
xyz.SetValue('') | |
def resul(event): | |
x = xyz.GetValue() | |
x = x.replace('x', '*').replace(u'÷', '/') | |
if '.' not in x: | |
x = x + '.0' | |
# print repr(x) | |
if x: | |
xyz.SetValue(str(eval(x, {"__builtins__": None}, {}))) | |
if __name__ == '__main__': | |
app = wx.App() | |
win = wx.Frame(None, title='Simple BC', size=(325, 320)) | |
bkg = wx.Panel(win) | |
xyz = wx.TextCtrl(bkg) | |
result_button = wx.Button(bkg, label='=') | |
hbox = wx.BoxSizer() | |
hbox.Add(xyz, proportion=3, flag=wx.EXPAND) | |
hbox.Add(result_button, proportion=1, flag=wx.EXPAND, border=5) | |
xbox = wx.GridSizer(4,4,4,4) | |
buttons = ['7', '8', '9', u'÷', '4', '5', '6', 'x', '1', '2', | |
'3', '-', '.', '0', 'clear', '+'] | |
for x in buttons: | |
button = wx.Button(bkg, label=x) | |
xbox.Add(button, proportion=1, flag=wx.EXPAND, border=5) | |
button.Bind(wx.EVT_BUTTON, onclick) | |
vbox = wx.BoxSizer(wx.VERTICAL) | |
vbox.Add(hbox, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) | |
vbox.Add(xbox, proportion=4, flag=wx.EXPAND|wx.ALL, border=5) | |
bkg.SetSizer(vbox) | |
result_button.Bind(wx.EVT_BUTTON, resul) | |
win.Show() | |
app.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(๑•̀ㅂ•́)و✧