佈局管理2

布局管理layout management — wx.GridSizer

顾名思义,wx.GridSizer 对其子部件以二维表的形式进行布局. 最高的那个子部件的宽度,决定每个格子的宽度. 每个格子的高度也是由最高的那个子部件的高度决定的.[more…]

wx.GridSizer(integer rows, integer cols, integer vgap, integer hgap)

在它的构建器中,我们需要提供行数、列数以及各子部件在水平和垂直方向的空白距离. 通过 AddMany() 方法将子部件插入进这个表格. 子部件按从左到右、从上到下顺序插入.

wx.GridSizer(integer rows, integer cols, integer vgap, integer hgap)
#!/usr/bin/python
#coding=utf-8

#calculator.py

import wx

class MyFrame(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()
        file = wx.Menu()
        file.Append(22, '退出(&Q)', '退出计算器')
        menubar.Append(file, '文件(&F)')
        self.SetMenuBar(menubar)
        wx.EVT_MENU(self, 22, self.OnClose)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
        sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)

        gs = wx.GridSizer(4, 4, 3, 3)
        gs.AddMany([
                    (wx.Button(self, 20, '清除'), 0, wx.EXPAND),
                    (wx.Button(self, 21, '退格'), 0, wx.EXPAND),
                    (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
                    (wx.Button(self, 22, '关闭'), 0, wx.EXPAND),
                    (wx.Button(self, 1, '7'), 0, wx.EXPAND),
                    (wx.Button(self, 2, '8'), 0, wx.EXPAND),
                    (wx.Button(self, 3, '9'), 0, wx.EXPAND),
                    (wx.Button(self, 4, '/'), 0, wx.EXPAND),
                    (wx.Button(self, 5, '4'), 0, wx.EXPAND),
                    (wx.Button(self, 6, '5'), 0, wx.EXPAND),
                    (wx.Button(self, 7, '6'), 0, wx.EXPAND),
                    (wx.Button(self, 8, '*'), 0, wx.EXPAND),
                    (wx.Button(self, 9, '1'), 0, wx.EXPAND),
                    (wx.Button(self, 10, '2'), 0, wx.EXPAND),
                    (wx.Button(self, 11, '3'), 0, wx.EXPAND),
                    (wx.Button(self, 12, '-'), 0, wx.EXPAND),
                    (wx.Button(self, 13, '0'), 0, wx.EXPAND),
                    (wx.Button(self, 14, '.'), 0, wx.EXPAND),
                    (wx.Button(self, 15, '='), 0, wx.EXPAND),
                    (wx.Button(self, 16, '+'), 0, wx.EXPAND),
                    ])
        
        sizer.Add(gs, 1, wx.EXPAND)
        
        self.SetSizer(sizer)
        self.Centre()

        self.Bind(wx.EVT_BUTTON, self.OnClear, id=20)
        self.Bind(wx.EVT_BUTTON, self.OnBackspace, id=21)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=22)
        self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
        self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
        self.Bind(wx.EVT_BUTTON, self.OnDivide, id=4)
        self.Bind(wx.EVT_BUTTON, self.OnFour, id=5)
        self.Bind(wx.EVT_BUTTON, self.OnFive, id=6)
        self.Bind(wx.EVT_BUTTON, self.OnSix, id=7)
        self.Bind(wx.EVT_BUTTON, self.OnMultiply, id=8)
        self.Bind(wx.EVT_BUTTON, self.OnOne, id=9)
        self.Bind(wx.EVT_BUTTON, self.OnTwo, id=10)
        self.Bind(wx.EVT_BUTTON, self.OnThree, id=11)
        self.Bind(wx.EVT_BUTTON, self.OnMinus, id=12)
        self.Bind(wx.EVT_BUTTON, self.OnZero, id=13)
        self.Bind(wx.EVT_BUTTON, self.OnDot, id=14)
        self.Bind(wx.EVT_BUTTON, self.OnEqual, id=15)
        self.Bind(wx.EVT_BUTTON, self.OnPlus, id=16)
        
    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 OnDivide(self, event):
        if self.formula:
            return
        self.display.AppendText('/')

    def OnMultiply(self, event):
        if self.formula:
            return
        self.display.AppendText('*')
        
    def OnMinus(self, event):
        if self.formula:
            return
        self.display.AppendText('-')
        
    def OnPlus(self, event):
        if self.formula:
            return
        self.display.AppendText('+')
    
    def OnDot(self, event):
        if self.formula:
            return
        self.display.AppendText('.')
        
    def OnEqual(self, event):
        if self.formula:
            return
        formula = self.display.GetValue()
        self.formula = False
        try:
            self.display.Clear()
            output = eval(formula)
            self.display.AppendText(str(output))
        except StandardError:
            self.display.AppendText('错误')
            
    def OnZero(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('0')

    def OnOne(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('1')
        
    def OnTwo(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('2')
        
    def OnThree(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('3')
        
    def OnFour(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('4')
        
    def OnFive(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('5')
        
    def OnSix(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('6')

    def OnSeven(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('7')

    def OnEight(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('8')
        
    def OnNine(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('9')
        
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'calculator.py')
        frame.Show(True)
        return True
    
app = MyApp(0)
app.MainLoop()

我们输入的算式 (formula) 由 python 内建的 eval() 函数进行处理.

output = eval(formula)

如在算式中出现了错误,将会示出一个错误消息. 这里要注意的是如何在“退格” 和 “关闭” 按钮之间放入一个空白的. 我们只是简单地放了一个空的 wx.StaticText 在那. 像这样的把戏(tricks)是很常见的.

图:calculator.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值