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 BeautifulSoup使用方法详解
Nov 21 Python
Python中比较特别的除法运算和幂运算介绍
Apr 05 Python
九步学会Python装饰器
May 09 Python
git进行版本控制心得详谈
Dec 10 Python
python3+PyQt5实现拖放功能
Apr 24 Python
对Python中type打开文件的方式介绍
Apr 28 Python
Python求解任意闭区间的所有素数
Jun 10 Python
python 对给定可迭代集合统计出现频率,并排序的方法
Oct 18 Python
python线程定时器Timer实现原理解析
Nov 30 Python
keras中的卷积层&池化层的用法
May 22 Python
Python多分支if语句的使用
Sep 03 Python
python中doctest库实例用法
Dec 31 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
强烈声明: 不要使用(include/require)_once
2013/06/06 PHP
PHP使用feof()函数读文件的方法
2014/11/07 PHP
yii2缓存Caching基本用法示例
2016/07/18 PHP
详解PHP实现定时任务的五种方法
2016/07/25 PHP
总结PHP中数值计算的注意事项
2016/08/14 PHP
Laravel使用RabbitMQ的方法示例
2019/06/18 PHP
FireFox JavaScript全局Event对象
2009/06/14 Javascript
JQuery入门——事件切换之toggle()方法应用介绍
2013/02/05 Javascript
String.prototype实现的一些javascript函数介绍
2013/11/22 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
JavaScript中instanceof运算符的使用示例
2016/06/08 Javascript
微信小程序登录态控制深入分析
2017/04/12 Javascript
JavaScript实现预览本地上传图片功能完整示例
2019/03/08 Javascript
python 查找文件夹下所有文件 实现代码
2009/07/01 Python
Python中实现的RC4算法
2015/02/14 Python
Python解析json文件相关知识学习
2016/03/01 Python
python中的随机函数random的用法示例
2018/01/27 Python
python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix实现
2018/06/11 Python
Python3.5 处理文本txt,删除不需要的行方法
2018/12/10 Python
python实现贪吃蛇游戏
2020/03/21 Python
使用 python pyautogui实现鼠标键盘控制功能
2019/08/04 Python
python 进程的几种创建方式详解
2019/08/29 Python
python接口自动化如何封装获取常量的类
2019/12/24 Python
Windows下python3安装tkinter的问题及解决方法
2020/01/06 Python
使用jupyter notebook直接打开.md格式的文件
2020/04/10 Python
Anaconda和ipython环境适配的实现
2020/04/22 Python
如何使用PyCharm将代码上传到GitHub上(图文详解)
2020/04/27 Python
Django windows使用Apache实现部署流程解析
2020/10/12 Python
荷兰家电销售网站:Welhof
2020/12/08 全球购物
信息管理专业学生自荐信格式
2013/09/22 职场文书
文秘专业自荐信
2013/10/14 职场文书
数学系个人求职信范文
2014/01/30 职场文书
冰淇淋店的创业计划书
2014/02/07 职场文书
自愿离婚协议书范本
2014/09/13 职场文书
Javascript之datagrid查询详解
2021/09/15 Javascript
《乙女游戏世界对路人角色很不友好》OP主题曲无字幕动画MV公开
2022/04/05 日漫