Python使用wxPython实现计算器


Posted in Python onJanuary 30, 2018

本文实例为大家分享了wxPython实现计算器的具体代码,供大家参考,具体内容如下

# -*- coding: utf-8 -*-

##########################################
## Python code generated with wxFormBuilder (version Feb 16 2016)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
##########################################

import wx
import wx.xrc
import math


#############################################
## Class MyFrame1
#############################################

class MyFrame1(wx.Frame):
 def __init__(self, parent):
 wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
  size=wx.Size(486, 448), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

 self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)

 bSizer1 = wx.BoxSizer(wx.VERTICAL)

 self.m_textCtrl1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(600, 60), style=wx.TE_RIGHT)
 self.m_textCtrl1.SetMinSize(wx.Size(470, 60))

 bSizer1.Add(self.m_textCtrl1, 0, wx.ALL, 5)

 bSizer2 = wx.BoxSizer(wx.HORIZONTAL)

 self.m_button1 = wx.Button(self, wx.ID_ANY, u"退格", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer2.Add(self.m_button1, 0, wx.ALL, 5)

 self.m_button2 = wx.Button(self, wx.ID_ANY, u"清屏", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer2.Add(self.m_button2, 0, wx.ALL, 5)

 self.m_button3 = wx.Button(self, wx.ID_ANY, u"sqrt", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer2.Add(self.m_button3, 0, wx.ALL, 5)

 self.m_button4 = wx.Button(self, wx.ID_ANY, u"/", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer2.Add(self.m_button4, 0, wx.ALL, 5)

 bSizer1.Add(bSizer2, 0, wx.EXPAND, 5)

 bSizer6 = wx.BoxSizer(wx.HORIZONTAL)

 self.m_button10 = wx.Button(self, wx.ID_ANY, u"7", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer6.Add(self.m_button10, 0, wx.ALL, 5)

 self.m_button11 = wx.Button(self, wx.ID_ANY, u"8", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer6.Add(self.m_button11, 0, wx.ALL, 5)

 self.m_button12 = wx.Button(self, wx.ID_ANY, u"9", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer6.Add(self.m_button12, 0, wx.ALL, 5)

 self.m_button13 = wx.Button(self, wx.ID_ANY, u"*", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer6.Add(self.m_button13, 0, wx.ALL, 5)

 bSizer1.Add(bSizer6, 0, wx.EXPAND, 5)

 bSizer7 = wx.BoxSizer(wx.HORIZONTAL)

 self.m_button15 = wx.Button(self, wx.ID_ANY, u"4", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer7.Add(self.m_button15, 0, wx.ALL, 5)

 self.m_button16 = wx.Button(self, wx.ID_ANY, u"5", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer7.Add(self.m_button16, 0, wx.ALL, 5)

 self.m_button17 = wx.Button(self, wx.ID_ANY, u"6", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer7.Add(self.m_button17, 0, wx.ALL, 5)

 self.m_button18 = wx.Button(self, wx.ID_ANY, u"-", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer7.Add(self.m_button18, 0, wx.ALL, 5)

 bSizer1.Add(bSizer7, 0, wx.EXPAND, 5)

 bSizer34 = wx.BoxSizer(wx.HORIZONTAL)

 self.m_button140 = wx.Button(self, wx.ID_ANY, u"1", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer34.Add(self.m_button140, 0, wx.ALL, 5)

 self.m_button141 = wx.Button(self, wx.ID_ANY, u"2", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer34.Add(self.m_button141, 0, wx.ALL, 5)

 self.m_button142 = wx.Button(self, wx.ID_ANY, u"3", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer34.Add(self.m_button142, 0, wx.ALL, 5)

 self.m_button143 = wx.Button(self, wx.ID_ANY, u"+", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer34.Add(self.m_button143, 0, wx.ALL, 5)

 bSizer1.Add(bSizer34, 0, wx.EXPAND, 5)

 bSizer35 = wx.BoxSizer(wx.HORIZONTAL)

 self.m_button145 = wx.Button(self, wx.ID_ANY, u"0", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer35.Add(self.m_button145, 0, wx.ALL, 5)

 self.m_button148 = wx.Button(self, wx.ID_ANY, u".", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer35.Add(self.m_button148, 0, wx.ALL, 5)

 self.m_button149 = wx.Button(self, wx.ID_ANY, u"+/-", wx.DefaultPosition, wx.Size(110, 60), 0)
 bSizer35.Add(self.m_button149, 0, wx.ALL, 5)

 self.m_button150 = wx.Button(self, wx.ID_ANY, u"=", wx.DefaultPosition, wx.Size(110, 600), 0)
 self.m_button150.SetMinSize(wx.Size(110, 60))

 bSizer35.Add(self.m_button150, 0, wx.ALL, 5)

 bSizer1.Add(bSizer35, 0, wx.EXPAND, 5)

 self.SetSizer(bSizer1)
 self.Layout()

 self.Centre(wx.BOTH)

 # Connect Events
 self.m_button1.Bind(wx.EVT_BUTTON, self.m_button1OnButtonClick)
 self.m_button2.Bind(wx.EVT_BUTTON, self.m_button2OnButtonClick)
 self.m_button3.Bind(wx.EVT_BUTTON, self.m_button3OnButtonClick)
 self.m_button4.Bind(wx.EVT_BUTTON, self.m_button4OnButtonClick)
 self.m_button10.Bind(wx.EVT_BUTTON, self.m_button10OnButtonClick)
 self.m_button11.Bind(wx.EVT_BUTTON, self.m_button11OnButtonClick)
 self.m_button12.Bind(wx.EVT_BUTTON, self.m_button12OnButtonClick)
 self.m_button13.Bind(wx.EVT_BUTTON, self.m_button13OnButtonClick)
 self.m_button15.Bind(wx.EVT_BUTTON, self.m_button15OnButtonClick)
 self.m_button16.Bind(wx.EVT_BUTTON, self.m_button16OnButtonClick)
 self.m_button17.Bind(wx.EVT_BUTTON, self.m_button17OnButtonClick)
 self.m_button18.Bind(wx.EVT_BUTTON, self.m_button18OnButtonClick)
 self.m_button140.Bind(wx.EVT_BUTTON, self.m_button140OnButtonClick)
 self.m_button141.Bind(wx.EVT_BUTTON, self.m_button141OnButtonClick)
 self.m_button142.Bind(wx.EVT_BUTTON, self.m_button142OnButtonClick)
 self.m_button143.Bind(wx.EVT_BUTTON, self.m_button143OnButtonClick)
 self.m_button145.Bind(wx.EVT_BUTTON, self.m_button145OnButtonClick)
 self.m_button148.Bind(wx.EVT_BUTTON, self.m_button148OnButtonClick)
 self.m_button149.Bind(wx.EVT_BUTTON, self.m_button149OnButtonClick)
 self.m_button150.Bind(wx.EVT_BUTTON, self.m_button150OnButtonClick)

 def __del__(self):
 pass

 # Virtual event handlers, overide them in your derived class
 def m_button1OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result[:-1]
 self.m_textCtrl1.SetValue(result)

 def m_button2OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=''
 self.m_textCtrl1.SetValue(result)

 def m_button3OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=int(result)
 result=math.sqrt(result)
 self.m_textCtrl1.SetValue(str(result))

 def m_button4OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'/'
 self.m_textCtrl1.SetValue(result)

 def m_button10OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'7'
 self.m_textCtrl1.SetValue(result)

 def m_button11OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'8'
 self.m_textCtrl1.SetValue(result)

 def m_button12OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'9'
 self.m_textCtrl1.SetValue(result)

 def m_button13OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'*'
 self.m_textCtrl1.SetValue(result)

 def m_button15OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'4'
 self.m_textCtrl1.SetValue(result)

 def m_button16OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'5'
 self.m_textCtrl1.SetValue(result)

 def m_button17OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'6'
 self.m_textCtrl1.SetValue(result)

 def m_button18OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'-'
 self.m_textCtrl1.SetValue(result)

 def m_button140OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'1'
 self.m_textCtrl1.SetValue(result)

 def m_button141OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'2'
 self.m_textCtrl1.SetValue(result)

 def m_button142OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'3'
 self.m_textCtrl1.SetValue(result)

 def m_button143OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'+'
 self.m_textCtrl1.SetValue(result)

 def m_button145OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'0'
 self.m_textCtrl1.SetValue(result)

 def m_button148OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=result+'.'
 self.m_textCtrl1.SetValue(result)

 def m_button149OnButtonClick(self, event):
 result=self.m_textCtrl1.GetValue()
 result=-int(result)
 self.m_textCtrl1.SetValue(str(result))

 def m_button150OnButtonClick(self, event):
 self.m_textCtrl1.SetValue(str(eval(self.m_textCtrl1.GetValue())))

app=wx.App()
window=MyFrame1(None)
window.Show(True)
app.MainLoop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现给数组按片赋值的方法
Jul 28 Python
python3中bytes和string之间的互相转换
Feb 09 Python
利用python爬取斗鱼app中照片方法实例
Dec 03 Python
详解python中的json和字典dict
Jun 22 Python
Python 读写文件的操作代码
Sep 20 Python
Python列表(List)知识点总结
Feb 18 Python
一篇文章彻底搞懂Python中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念
May 13 Python
python中几种自动微分库解析
Aug 29 Python
Python concurrent.futures模块使用实例
Dec 24 Python
没编程基础可以学python吗
Jun 17 Python
Python爬虫如何应对Cloudflare邮箱加密
Jun 24 Python
python如何利用cv2模块读取显示保存图片
Jun 04 Python
python链接oracle数据库以及数据库的增删改查实例
Jan 30 #Python
python实现简易版计算器
Jun 22 #Python
python列表的增删改查实例代码
Jan 30 #Python
Python+tkinter使用40行代码实现计算器功能
Jan 30 #Python
Python Tkinter实现简易计算器功能
Jan 30 #Python
python使用tkinter实现简单计算器
Jan 30 #Python
Python实现简单遗传算法(SGA)
Jan 29 #Python
You might like
php获取参数的几种方法总结
2014/02/18 PHP
php中二维数组排序问题方法详解
2015/08/28 PHP
PHP正则表达式入门教程(推荐)
2016/05/18 PHP
Javascript结合css实现网页换肤功能
2009/11/02 Javascript
jQuery登陆判断简单实现代码
2013/04/21 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
JavaScript生成GUID的多种算法小结
2013/08/18 Javascript
jQuery实现的淡入淡出二级菜单效果代码
2015/09/15 Javascript
node.js实现登录注册页面
2017/04/08 Javascript
jQuery设置图片等比例缩小的方法
2017/04/29 jQuery
jQuery validata插件实现方法
2017/06/25 jQuery
nodejs搭建本地服务器轻松解决跨域问题
2018/03/21 NodeJs
Vue.directive使用注意(小结)
2018/08/31 Javascript
微信小程序视图容器(swiper)组件创建轮播图
2020/06/19 Javascript
vue项目添加多页面配置的步骤详解
2019/05/22 Javascript
element-ui树形控件后台返回的数据+生成组织树的工具类
2020/03/05 Javascript
在vue中使用Base64转码的案例
2020/08/07 Javascript
Nuxt.js的路由跳转操作(页面跳转nuxt-link)
2020/11/06 Javascript
[15:57]教你分分钟做大人:斧王
2014/10/30 DOTA
python正则匹配抓取豆瓣电影链接和评论代码分享
2013/12/27 Python
python脚本设置系统时间的两种方法
2016/02/21 Python
python基础教程之匿名函数lambda
2017/01/17 Python
Python与Java间Socket通信实例代码
2017/03/06 Python
python编程之requests在网络请求中添加cookies参数方法详解
2017/10/25 Python
浅谈Python处理PDF的方法
2017/11/10 Python
Pandas的read_csv函数参数分析详解
2019/07/02 Python
如何使用Python破解ZIP或RAR压缩文件密码
2020/01/09 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
html5 css3实例教程 一款html5和css3实现的小机器人走路动画
2014/10/20 HTML / CSS
Canvas 像素处理之改变透明度的实现代码
2019/01/08 HTML / CSS
C语言变量的命名规则都有哪些
2013/12/27 面试题
阿尔卡特(中国)的面试题目
2014/08/20 面试题
2014年个人技术工作总结
2014/12/08 职场文书
实习协议书
2015/01/27 职场文书
2015年班干部工作总结
2015/04/29 职场文书
高中团支书竞选稿
2015/11/21 职场文书