基于wxpython开发的简单gui计算器实例


Posted in Python onMay 30, 2015

本文实例讲述了基于wxpython开发的简单gui计算器。分享给大家供大家参考。具体如下:

# wxCalc1 a simple GUI calculator using wxPython
# created with the Boa Constructor which generates all the GUI components
# all I had to do is add some code for each button click event
# Boa free from: http://boa-constructor.sourceforge.net/
# note that  boa-constructor-0.3.1.win32.exe
# still uses  wxPythonWIN32-2.4.2.4-Py23.exe
# but is expected to work with wxPython version 2.5 soon
# tested with Python23  vegaseat  26feb2005
from wxPython.wx import *
# some Boa generated global IDs ...
[wxID_WXFRAME1, wxID_WXFRAME1BTN0, wxID_WXFRAME1BTN1, wxID_WXFRAME1BTN2,
 wxID_WXFRAME1BTN3, wxID_WXFRAME1BTN4, wxID_WXFRAME1BTN5, wxID_WXFRAME1BTN6,
 wxID_WXFRAME1BTN7, wxID_WXFRAME1BTN8, wxID_WXFRAME1BTN9,
 wxID_WXFRAME1BTNCLEAR, wxID_WXFRAME1BTNDIV, wxID_WXFRAME1BTNDOT,
 wxID_WXFRAME1BTNEQUAL, wxID_WXFRAME1BTNMINUS, wxID_WXFRAME1BTNMULTI,
 wxID_WXFRAME1BTNPLUS, wxID_WXFRAME1EDIT,
] = map(lambda _init_ctrls: wxNewId(), range(19))
class wxFrame1(wxFrame):
#startregion, below this marker is Boa generated code do not edit!!!
  def _init_ctrls(self, prnt):
    # generated method, don't edit
    wxFrame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,
       pos=wxPoint(306, 270), size=wxSize(266, 265),
       style=wxDEFAULT_FRAME_STYLE, title='Calculator1')
    self.SetClientSize(wxSize(258, 225))
    self.SetBackgroundColour(wxColour(0, 128, 0))
    self.btn1 = wxButton(id=wxID_WXFRAME1BTN1, label='1', name='btn1',
       parent=self, pos=wxPoint(16, 136), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn1, wxID_WXFRAME1BTN1, self.OnBtn1Button)
    self.btn2 = wxButton(id=wxID_WXFRAME1BTN2, label='2', name='btn2',
       parent=self, pos=wxPoint(64, 136), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn2, wxID_WXFRAME1BTN2, self.OnBtn2Button)
    self.btn3 = wxButton(id=wxID_WXFRAME1BTN3, label='3', name='btn3',
       parent=self, pos=wxPoint(112, 136), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn3, wxID_WXFRAME1BTN3, self.OnBtn3Button)
    self.btn4 = wxButton(id=wxID_WXFRAME1BTN4, label='4', name='btn4',
       parent=self, pos=wxPoint(16, 96), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn4, wxID_WXFRAME1BTN4, self.OnBtn4Button)
    self.btn5 = wxButton(id=wxID_WXFRAME1BTN5, label='5', name='btn5',
       parent=self, pos=wxPoint(64, 96), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn5, wxID_WXFRAME1BTN5, self.OnBtn5Button)
    self.btn6 = wxButton(id=wxID_WXFRAME1BTN6, label='6', name='btn6',
       parent=self, pos=wxPoint(112, 96), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn6, wxID_WXFRAME1BTN6, self.OnBtn6Button)
    self.btn7 = wxButton(id=wxID_WXFRAME1BTN7, label='7', name='btn7',
       parent=self, pos=wxPoint(16, 56), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn7, wxID_WXFRAME1BTN7, self.OnBtn7Button)
    self.btn8 = wxButton(id=wxID_WXFRAME1BTN8, label='8', name='btn8',
       parent=self, pos=wxPoint(64, 56), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn8, wxID_WXFRAME1BTN8, self.OnBtn8Button)
    self.btn9 = wxButton(id=wxID_WXFRAME1BTN9, label='9', name='btn9',
       parent=self, pos=wxPoint(112, 56), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn9, wxID_WXFRAME1BTN9, self.OnBtn9Button)
    self.btn0 = wxButton(id=wxID_WXFRAME1BTN0, label='0', name='btn0',
       parent=self, pos=wxPoint(16, 176), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btn0, wxID_WXFRAME1BTN0, self.OnBtn0Button)
    self.btnDot = wxButton(id=wxID_WXFRAME1BTNDOT, label='.', name='btnDot',
       parent=self, pos=wxPoint(64, 176), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btnDot, wxID_WXFRAME1BTNDOT, self.OnBtnDotButton)
    self.btnEqual = wxButton(id=wxID_WXFRAME1BTNEQUAL, label='=',
       name='btnEqual', parent=self, pos=wxPoint(112, 176),
       size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btnEqual, wxID_WXFRAME1BTNEQUAL, self.OnBtnEqualButton)
    self.edit = wxTextCtrl(id=wxID_WXFRAME1EDIT, name='edit', parent=self,
       pos=wxPoint(16, 16), size=wxSize(224, 24), style=0, value='')
    self.btnPlus = wxButton(id=wxID_WXFRAME1BTNPLUS, label='+',
       name='btnPlus', parent=self, pos=wxPoint(160, 56), size=wxSize(32,
       32), style=0)
    EVT_BUTTON(self.btnPlus, wxID_WXFRAME1BTNPLUS, self.OnBtnPlusButton)
    self.btnMinus = wxButton(id=wxID_WXFRAME1BTNMINUS, label='-',
       name='btnMinus', parent=self, pos=wxPoint(160, 96),
       size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btnMinus, wxID_WXFRAME1BTNMINUS, self.OnBtnMinusButton)
    self.btnMulti = wxButton(id=wxID_WXFRAME1BTNMULTI, label='*',
       name='btnMulti', parent=self, pos=wxPoint(160, 136),
       size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btnMulti, wxID_WXFRAME1BTNMULTI, self.OnBtnMultiButton)
    self.btnDiv = wxButton(id=wxID_WXFRAME1BTNDIV, label='/', name='btnDiv',
       parent=self, pos=wxPoint(160, 176), size=wxSize(32, 32), style=0)
    EVT_BUTTON(self.btnDiv, wxID_WXFRAME1BTNDIV, self.OnBtnDivButton)
    self.btnClear = wxButton(id=wxID_WXFRAME1BTNCLEAR, label='C',
       name='btnClear', parent=self, pos=wxPoint(208, 56),
       size=wxSize(32, 32), style=0)
    self.btnClear.SetToolTipString('btnClear')
    EVT_BUTTON(self.btnClear, wxID_WXFRAME1BTNCLEAR, self.OnBtnClearButton)
  def __init__(self, parent):
    self._init_ctrls(parent)
#endregion, above this marker is Boa generated code, do not edit!!!
# now respond to all the button click events ...
  def OnBtn0Button(self, event):
    val = '0'
    # get existing edit box text
    txt = self.edit.GetValue()
    # append text
    txt = txt + val
    # update edit box text
    self.edit.SetValue(txt)
  def OnBtn1Button(self, event):
    val = '1'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn2Button(self, event):
    val = '2'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn3Button(self, event):
    val = '3'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn4Button(self, event):
    val = '4'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
    
  def OnBtn5Button(self, event):
    val = '5'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn6Button(self, event):
    val = '6'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn7Button(self, event):
    val = '7'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn8Button(self, event):
    val = '8'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtn9Button(self, event):
    val = '9'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtnDotButton(self, event):
    val = '.'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtnEqualButton(self, event):
    txt = self.edit.GetValue()
    # needs to contain a float so eg. 3/5 is 3/5.0
    # otherwise division 3/5 would result in zero
    if '/' in txt:
      if '.' not in txt:
        txt = txt + '.0'
    # now evaluate the math string
    txt = repr(eval(txt))
    # and show result in edit box
    self.edit.SetValue(txt)
  def OnBtnPlusButton(self, event):
    val = '+'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtnMinusButton(self, event):
    val = '-'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtnMultiButton(self, event):
    val = '*'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtnDivButton(self, event):
    val = '/'
    txt = self.edit.GetValue()
    txt = txt + val
    self.edit.SetValue(txt)
  def OnBtnClearButton(self, event):
    self.edit.SetValue('')
# -------------------- end of class wxFrame1 ----------------------
def create(parent):
  return wxFrame1(parent)
class BoaApp(wxApp):
  def OnInit(self):
    wxInitAllImageHandlers()
    self.main = create(None)
    self.main.Show()
    self.SetTopWindow(self.main)
    return True
def main():
  application = BoaApp(0)
  application.MainLoop()
if __name__ == '__main__':
  main()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python实现的各种排序算法代码
Mar 04 Python
selenium python浏览器多窗口处理代码示例
Jan 15 Python
Python基础教程之内置函数locals()和globals()用法分析
Mar 16 Python
解决pandas中读取中文名称的csv文件报错的问题
Jul 04 Python
使用Python监视指定目录下文件变更的方法
Oct 15 Python
浅谈python连续赋值可能引发的错误
Nov 10 Python
用python标准库difflib比较两份文件的异同详解
Nov 16 Python
使用python实现滑动验证码功能
Aug 05 Python
python3中sys.argv的实例用法
Apr 24 Python
Python实现中英文全文搜索的示例
Dec 04 Python
10个python爬虫入门基础代码实例 + 1个简单的python爬虫完整实例
Dec 16 Python
OpenCV项目实践之停车场车位实时检测
Apr 11 Python
python图像处理之镜像实现方法
May 30 #Python
python图像处理之反色实现方法
May 30 #Python
python中字典(Dictionary)用法实例详解
May 30 #Python
python集合用法实例分析
May 30 #Python
基于wxpython实现的windows GUI程序实例
May 30 #Python
python简单实现旋转图片的方法
May 30 #Python
Python实现控制台输入密码的方法
May 29 #Python
You might like
ThinkPHP的Widget扩展实例
2014/06/19 PHP
php rsa加密解密使用详解
2015/01/14 PHP
PHP设计模式之原型模式定义与用法详解
2018/04/03 PHP
微信公众号之主动给用户发送消息功能
2019/06/22 PHP
JavaScript的类型转换(字符转数字 数字转字符)
2010/08/30 Javascript
jquery实现类似EasyUI的页面布局可改变左右的宽度
2020/09/12 Javascript
javascript解析ajax返回的xml和json格式数据实例详解
2017/01/05 Javascript
在ABP框架中使用BootstrapTable组件的方法
2017/07/31 Javascript
vue跨域解决方法
2017/10/15 Javascript
jQuery实现页码跳转式动态数据分页
2017/12/31 jQuery
vue左右侧联动滚动的实现代码
2018/06/06 Javascript
微信公众号平台接口开发 获取微信服务器IP地址方法解析
2019/08/14 Javascript
vue 实现微信浮标效果
2019/09/01 Javascript
[01:41]DOTA2超级联赛专访YYF 称一辈子难忘TI2
2013/05/28 DOTA
python将ip地址转换成整数的方法
2015/03/17 Python
pandas.DataFrame选取/排除特定行的方法
2018/07/03 Python
python正则表达式之对号入座篇
2018/07/24 Python
python 画三维图像 曲面图和散点图的示例
2018/12/29 Python
python pytest进阶之fixture详解
2019/06/27 Python
Python 使用PyQt5 完成选择文件或目录的对话框方法
2019/06/27 Python
python爬虫的一个常见简单js反爬详解
2019/07/09 Python
解决django 新增加用户信息出现错误的问题
2019/07/28 Python
python django中8000端口被占用的解决
2019/12/17 Python
Python实现隐马尔可夫模型的前向后向算法的示例代码
2019/12/31 Python
Pytorch 实现sobel算子的卷积操作详解
2020/01/10 Python
Python格式化输出--%s,%d,%f的代码解析
2020/04/29 Python
浅析移动设备HTML5页面布局
2015/12/01 HTML / CSS
Ramy Brook官网:美国现代女装品牌
2019/06/18 全球购物
测试工程师岗位职责
2013/11/28 职场文书
毕业生实习证明
2014/09/19 职场文书
个人向公司借款协议书
2014/10/09 职场文书
2014年流动人口工作总结
2014/11/26 职场文书
2015年度销售个人工作总结
2015/03/31 职场文书
老员工辞职信范文
2015/05/12 职场文书
健身房被搭讪?用python写了个小米计时器助人为乐
2021/06/08 Python
windows10声卡驱动怎么安装?win10声卡驱动安装操作步骤教程
2022/08/05 数码科技