Python体验(10)-图形界面之计算器
import wx
class Form(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()
mnuFile = wx.Menu()
mnuFile.Append( 22, '&Quit', 'Exit Calculator' )
menuBar.Append( mnuFile, '&File' )
wx.EVT_MENU( self, 22, self.OnClose )
self.SetMenuBar( menuBar ) self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
gs = wx.GridSizer(5, 4, 3, 3)
gs.AddMany(
[
(wx.Button(self, 12, '-'), 0, wx.EXPAND),
(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, ''), 0, wx.EXPAND),
(wx.Button(self, 2, ''), 0, wx.EXPAND),
(wx.Button(self, 3, ''), 0, wx.EXPAND),
(wx.Button(self, 4, '/'), 0, wx.EXPAND),
(wx.Button(self, 5, ''), 0, wx.EXPAND),
(wx.Button(self, 6, ''), 0, wx.EXPAND),
(wx.Button(self, 7, ''), 0, wx.EXPAND),
(wx.Button(self, 8, '*'), 0, wx.EXPAND),
(wx.Button(self, 10, ''), 0, wx.EXPAND),
(wx.Button(self, 11, ''), 0, wx.EXPAND),
(wx.Button(self, 9, ''), 0, wx.EXPAND),
(wx.Button(self, 16, '+'), 0, wx.EXPAND),
(wx.Button(self, 15, '='), 0, wx.EXPAND),
(wx.Button(self, 14, '.'), 0, wx.EXPAND),
(wx.Button(self, 13, ''), 0, wx.EXPAND)
]
)
sizer = wx.BoxSizer( wx.VERTICAL )
sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)
sizer.Add(gs, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Centre() wx.EVT_BUTTON(self, 20, self.OnClear)
wx.EVT_BUTTON(self, 21, self.OnBackspace)
wx.EVT_BUTTON(self, 22, self.OnClose)
wx.EVT_BUTTON(self, 1, self.OnNumber)
wx.EVT_BUTTON(self, 2, self.OnNumber)
wx.EVT_BUTTON(self, 3, self.OnNumber)
wx.EVT_BUTTON(self, 4, self.OnFormula)
wx.EVT_BUTTON(self, 5, self.OnNumber)
wx.EVT_BUTTON(self, 6, self.OnNumber)
wx.EVT_BUTTON(self, 7, self.OnNumber)
wx.EVT_BUTTON(self, 8, self.OnFormula)
wx.EVT_BUTTON(self, 9, self.OnNumber)
wx.EVT_BUTTON(self, 10, self.OnNumber)
wx.EVT_BUTTON(self, 11, self.OnNumber)
wx.EVT_BUTTON(self, 12, self.OnFormula)
wx.EVT_BUTTON(self, 13, self.OnNumber)
wx.EVT_BUTTON(self, 14, self.OnFormula)
wx.EVT_BUTTON(self, 15, self.OnEqual)
wx.EVT_BUTTON(self, 16, self.OnFormula) def OnClear(self, event):
self.display.Clear()
def OnBackspace(self, event):
formula = self.display.GetValue()
self.display.Clear()
self.display.SetValue(formula[:-1])
def OnClose(self, event):
self.Close()
def OnEqual(self,event):
if self.formula:
return
formula = self.display.GetValue()
self.formula = True
try:
self.display.Clear()
output = eval(formula)
self.display.AppendText(str(output))
except StandardError:
self.display.AppendText("Error") def OnFormula(self,event):
if self.formula:
return
self.display.AppendText(event.EventObject.LabelText) def OnNumber(self,event):
if self.formula:
self.display.Clear()
self.formula=False
self.display.AppendText(event.EventObject.LabelText) class MyApp(wx.App):
def OnInit(self):
frame = Form(None, -1, "Phoenix Caculator")
frame.Show(True)
self.SetTopWindow(frame)
return True app = MyApp(0)
app.MainLoop()
import wx
class Form(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()
mnuFile = wx.Menu()
mnuFile.Append( 22, '&Quit', 'Exit Calculator' )
menuBar.Append( mnuFile, '&File' )
wx.EVT_MENU( self, 22, self.OnClose )
self.SetMenuBar( menuBar ) self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
gs = wx.GridSizer(5, 4, 3, 3)
gs.AddMany(
[
(wx.Button(self, 12, '-'), 0, wx.EXPAND),
(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, ''), 0, wx.EXPAND),
(wx.Button(self, 2, ''), 0, wx.EXPAND),
(wx.Button(self, 3, ''), 0, wx.EXPAND),
(wx.Button(self, 4, '/'), 0, wx.EXPAND),
(wx.Button(self, 5, ''), 0, wx.EXPAND),
(wx.Button(self, 6, ''), 0, wx.EXPAND),
(wx.Button(self, 7, ''), 0, wx.EXPAND),
(wx.Button(self, 8, '*'), 0, wx.EXPAND),
(wx.Button(self, 10, ''), 0, wx.EXPAND),
(wx.Button(self, 11, ''), 0, wx.EXPAND),
(wx.Button(self, 9, ''), 0, wx.EXPAND),
(wx.Button(self, 16, '+'), 0, wx.EXPAND),
(wx.Button(self, 15, '='), 0, wx.EXPAND),
(wx.Button(self, 14, '.'), 0, wx.EXPAND),
(wx.Button(self, 13, ''), 0, wx.EXPAND)
]
)
sizer = wx.BoxSizer( wx.VERTICAL )
sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)
sizer.Add(gs, 1, wx.EXPAND)
self.SetSizer(sizer)
self.Centre() wx.EVT_BUTTON(self, 20, self.OnClear)
wx.EVT_BUTTON(self, 21, self.OnBackspace)
wx.EVT_BUTTON(self, 22, self.OnClose)
wx.EVT_BUTTON(self, 1, self.OnNumber)
wx.EVT_BUTTON(self, 2, self.OnNumber)
wx.EVT_BUTTON(self, 3, self.OnNumber)
wx.EVT_BUTTON(self, 4, self.OnFormula)
wx.EVT_BUTTON(self, 5, self.OnNumber)
wx.EVT_BUTTON(self, 6, self.OnNumber)
wx.EVT_BUTTON(self, 7, self.OnNumber)
wx.EVT_BUTTON(self, 8, self.OnFormula)
wx.EVT_BUTTON(self, 9, self.OnNumber)
wx.EVT_BUTTON(self, 10, self.OnNumber)
wx.EVT_BUTTON(self, 11, self.OnNumber)
wx.EVT_BUTTON(self, 12, self.OnFormula)
wx.EVT_BUTTON(self, 13, self.OnNumber)
wx.EVT_BUTTON(self, 14, self.OnFormula)
wx.EVT_BUTTON(self, 15, self.OnEqual)
wx.EVT_BUTTON(self, 16, self.OnFormula) def OnClear(self, event):
self.display.Clear()
def OnBackspace(self, event):
formula = self.display.GetValue()
self.display.Clear()
self.display.SetValue(formula[:-1])
def OnClose(self, event):
self.Close()
def OnEqual(self,event):
if self.formula:
return
formula = self.display.GetValue()
self.formula = True
try:
self.display.Clear()
output = eval(formula)
self.display.AppendText(str(output))
except StandardError:
self.display.AppendText("Error") def OnFormula(self,event):
if self.formula:
return
self.display.AppendText(event.EventObject.LabelText) def OnNumber(self,event):
if self.formula:
self.display.Clear()
self.formula=False
self.display.AppendText(event.EventObject.LabelText) class MyApp(wx.App):
def OnInit(self):
frame = Form(None, -1, "Phoenix Caculator")
frame.Show(True)
self.SetTopWindow(frame)
return True app = MyApp(0)
app.MainLoop()
Python体验(10)-图形界面之计算器的更多相关文章
- Python体验(07)-图形界面之菜单
顺序安装以下程序: python解释器:https://www.python.org/downloads/ wxPython图形界面框架包:http://www.wxpython.org/ pycha ...
- Python体验(08)-图形界面之工具栏和状态栏
# coding=utf-8 import wx # 导入必须的Python包 class MenuForm(wx.Frame): def OnQuit(self,event): self.Close ...
- 用aardio给python写个图形界面
前阵子在用python写一些小程序,写完后就开始思考怎么给python程序配一个图形界面,毕竟控制台实在太丑陋了. 于是百度了下python的图形界面库,眼花缭乱的一整页,拣了几件有“特色”有“噱头” ...
- 【Python】 用户图形界面GUI wxpython III 更多组件
wxpython - 更多组件 我写到的这些组件可能一来不是很详细,二来不是最全的,想要更好地用这些组件,应该还是去看看教程和别的示例.比较简单的,推荐http://download.csdn.net ...
- 【Python】 用户图形界面GUI wxpython IV 菜单&对话框
更多组件 ■ 菜单栏 Menu 菜单是很多GUI必不可少的一部分.要建立菜单,必须先创建菜单栏: menuBar = MenuBar() menu = Menu() item1 = menu.Appe ...
- 【Python】 用户图形界面GUI wxpython I 基本用法和组件
wxpython - 基本用法和组件 wxpython是python对跨平台GUI库wxWidgets的封装.wxWidgets是由C++写成的. wxpython被包装进了wx模块中,用它设计GUI ...
- python学习之图形界面编程:
一 tkinter:tkinter是python自带的支持tk的库,python代码调用tkinter->tk->操作系统提供的本地GUI(TKL语言开发))完成界面开发,不需要安装任何第 ...
- Python 的简单图形界面编程【草】
可用方案 Tkinter python官方附带,方便,但听说存在乱码问题 wxPython 更成熟一些,但需要额外安装(大约50M) pyQt 授权不够宽松 最短代码 Tkinter 待补充 wxPy ...
- 【Python】 用户图形界面GUI wxpython II 布局和事件
wxpython - 布局和事件 这章主要记录布局器Sizer以及事件的用法. // 目前还需要记录的:Sizer的Add方法加空白,Sizer的Layout,Sizer的Remove如何有效 ■ 布 ...
随机推荐
- ArrayList集合 、特殊集合
一.ArrayList集合 集合内可以放不同类型的元素 另:object类型为所有数据类型的基类 添加元素:.add(); 清空集合:al.clear(); 克隆集合:.clone(); 判断是否包含 ...
- Oracle执行时间与sql格式
今天碰到一个很奇怪的问题,直接在eclipse中将sql拷出,然后直接粘贴复制在数据库中就会执行的非常慢,但是在利用plsql对sql语句进行格式整理之后,执行的速度就非常的快,之后我where条件中 ...
- python学习之关键字
1.assert 语句形式:assert 布尔表达式[,msg] 解释:assert是检验一个表达式是否为真.若不为真,则触发一个错误. 例如: assert False assert 1==2 as ...
- 解决java使用https协议请求出现证书不信任问题(PKIX path building failed)
解决https请求时出现pkix path building fail错误 方法 将submail.cer 安全证书导入到java中的cacerts证书库 (sumail是我从https://api. ...
- linux下安装安装pcre-8.32
linux下安装安装pcre-8.32 ./configure --prefix=/usr/local/pcre 出现以下错误 configure: error: You need a C++ com ...
- IE弹出窗口显示URL地址栏
工具-->Internet 选项-->安全-->自定义级别-->允许网站打开没有地址栏或状态栏的窗口-->禁止
- Android 手机蓝牙接收文件夹
网上很多都没用用,只能自己研究了 /mnt/emmc/downloads/bluetooth
- 为毛无法创建文本文件COM1.txt
是的,windows系统下,右键,新建文本文件,将文件名编辑为COM1.txt,确认,保存……THEN....你会发现无论如何也无法创建一个文本文件,名为COM1.txt. 猜是COM1被作为了IO设 ...
- lumen Response
1.基本响应 所有路由和控制器都会返回某种被发送到用户浏览器的响应,Lumen提供了多种不同的方式来返回响应,最基本的响应就是从路由或控制器返回一个简单的字符串: $app->get('/', ...
- bzoj4750: 密码安全
Description 有些人在社交网络中使用过许多的密码,我们通过将各种形式的信息转化为 01 信号,再转化为整数,可以将这个 人在一段时间内使用过的密码视为一个长度为 n 的非负整数序列 A_1, ...