Python体验(10)-图形界面之计算器

时间:2022-11-11 13:57:38
 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)-图形界面之计算器的更多相关文章

  1. Python体验(07)-图形界面之菜单

    顺序安装以下程序: python解释器:https://www.python.org/downloads/ wxPython图形界面框架包:http://www.wxpython.org/ pycha ...

  2. Python体验(08)-图形界面之工具栏和状态栏

    # coding=utf-8 import wx # 导入必须的Python包 class MenuForm(wx.Frame): def OnQuit(self,event): self.Close ...

  3. 用aardio给python写个图形界面

    前阵子在用python写一些小程序,写完后就开始思考怎么给python程序配一个图形界面,毕竟控制台实在太丑陋了. 于是百度了下python的图形界面库,眼花缭乱的一整页,拣了几件有“特色”有“噱头” ...

  4. 【Python】 用户图形界面GUI wxpython III 更多组件

    wxpython - 更多组件 我写到的这些组件可能一来不是很详细,二来不是最全的,想要更好地用这些组件,应该还是去看看教程和别的示例.比较简单的,推荐http://download.csdn.net ...

  5. 【Python】 用户图形界面GUI wxpython IV 菜单&对话框

    更多组件 ■ 菜单栏 Menu 菜单是很多GUI必不可少的一部分.要建立菜单,必须先创建菜单栏: menuBar = MenuBar() menu = Menu() item1 = menu.Appe ...

  6. 【Python】 用户图形界面GUI wxpython I 基本用法和组件

    wxpython - 基本用法和组件 wxpython是python对跨平台GUI库wxWidgets的封装.wxWidgets是由C++写成的. wxpython被包装进了wx模块中,用它设计GUI ...

  7. python学习之图形界面编程:

    一 tkinter:tkinter是python自带的支持tk的库,python代码调用tkinter->tk->操作系统提供的本地GUI(TKL语言开发))完成界面开发,不需要安装任何第 ...

  8. Python 的简单图形界面编程【草】

    可用方案 Tkinter python官方附带,方便,但听说存在乱码问题 wxPython 更成熟一些,但需要额外安装(大约50M) pyQt 授权不够宽松 最短代码 Tkinter 待补充 wxPy ...

  9. 【Python】 用户图形界面GUI wxpython II 布局和事件

    wxpython - 布局和事件 这章主要记录布局器Sizer以及事件的用法. // 目前还需要记录的:Sizer的Add方法加空白,Sizer的Layout,Sizer的Remove如何有效 ■ 布 ...

随机推荐

  1. mac svn命令使用

    对mac不熟悉 ssd硬盘又小 不想装版本管理软件. #创建目录 svn mkdir svn://ip.xxx.xxx.xxx/client/ios/opengl/imageToll -m &quot ...

  2. Bootstrap3.0学习教程十七:JavaScript插件模态框

    这篇文章中我们主要来学习一下JavaScipt插件模态框.在学习模态框之前,我们先来了解一下JavaScript插件吧. JavaScript插件概览 插件可以单个引入(使用Bootstrap提供的单 ...

  3. Openresty 安装教程

    Openresty的简单安装方法,如需高级编译安装,请参照安装选项 1.安装配置好Yum源,不赘述此步骤 2.安装必要组件 yum install pcre-devel openssl-devel g ...

  4. iOS中的字符串扫描类NSScanner

    新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置. UIColor+Hex.h文件, #import <UIKit/UIKit.h> # ...

  5. pygame实现的黑白块游戏

    运行时需要pygame库. 下载地址:http://files.cnblogs.com/files/zzrom/white.zip 程序截图:

  6. JSON连载java目的

    一. 前台(JS  面向对象) 1. 定义SearchView对象 function SearchView() { } SearchView.prototype.setViewName = funct ...

  7. mac监听Dock激活程序

    mac监听Dock激活程序 涉及库添加: LIBS += -framework CoreFoundation -framework Carbon -lobjc 涉及头文件: #include < ...

  8. htt p第一章概述

    http的概述 1 web客户端与服务器是如何通信 2 web资源来自的何方 3 web事务是怎样的工作的 4 http通信所使用的报文结构 5 底层tcp的传输的结构 6不同的http协议体 什么是 ...

  9. django事务处理

    #导包 from django.db import transaction try: #django默认是自动提交到数据库,此处设置不让其自动提交 transaction.set_autocommit ...

  10. Mac下如何配置环境变量JDK

    1.在英文输入法的状态下,按键盘“Ctrl + 空格”组合键,调出Spotlight搜索,在这里可以快速启动终端,输入ter,然后回车,即可打开终端: 2.如果你是第一次配置环境变量,可以使用“tou ...