Python 板


LINE

import wx, string char = u"清空 / * 7 8 9 4 5 6 1 2 3".split() other_char = "- + 0 . =".split() #span(rowspan, colspan) #设定位置与是否跨行、列 form = {'-' : {'pos' : (0, 3)}, '+' : {'pos' : (1, 3), 'span' : (2, 1)}, '=' : {'pos' : (3, 3), 'span' : (2, 1)}, '0' : {'pos' : (4, 0), 'span' : (1, 2)}, '.' : {'pos' : (4, 2)}} class Compute(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, '', size=(250, -1)) #设定按钮需要用到的函式或是快速键 self.accel = { u'清空' : {'func' : self.OnEsc}, '/' : {'func' : self.OnDiv, 'ord' : 392}, '*' : {'func' : self.OnMulti, 'ord' : 387}, '-' : {'func' : self.OnDec, 'ord' : 390}, '+' : {'func' : self.OnAdd, 'ord' : 388}, '=' : {'func' : self.OnEnter}, '.' : {'func' : self.OnPoint, 'ord' : 391}, '0' : {'func' : self.OnZero, 'ord' : 48}, '1' : {'func' : self.OnOne, 'ord' : 49}, '2' : {'func' : self.OnTwo, 'ord' : 50}, '3' : {'func' : self.OnThree, 'ord' : 51}, '4' : {'func' : self.OnFour, 'ord' : 52}, '5' : {'func' : self.OnFive, 'ord' : 53}, '6' : {'func' : self.OnSix, 'ord' : 54}, '7' : {'func' : self.OnSeven, 'ord' : 55}, '8' : {'func' : self.OnEight, 'ord' : 56}, '9' : {'func' : self.OnNight, 'ord' : 57}} self.value1 = self.value2 = self.length = 0 self.oper = self.enter = '' self.panel = wx.Panel(self) #设定text有ENTER操作并且是从右边开始输入 self.text = wx.TextCtrl(self.panel, -1, '0', size=(230, -1), style=wx.TE_RIGHT | wx.TE_PROCESS_ENTER) self.text.Bind(wx.EVT_KEY_DOWN, self.OnKey) #宣告Sizer (垂直) VboxSizer = wx.BoxSizer(wx.VERTICAL) #设定Sizer与上框距离 VboxSizer.Add((0, 10)) #宣告Sizer (水平) HboxSizer = wx.BoxSizer(wx.HORIZONTAL) boxSizer = wx.BoxSizer(wx.HORIZONTAL) #将text加入HSizer并设定与右边边框的距离 HboxSizer.Add(self.text) HboxSizer.Add((5, 0)) #将HSizer加入VSizer并设定靠右对齐 VboxSizer.Add(HboxSizer, 0, wx.ALIGN_RIGHT) VboxSizer.Add((0,10)) self.sizer = wx.GridBagSizer(5, 5) self.CharSizer() boxSizer.Add(self.sizer) boxSizer.Add((10,0)) VboxSizer.Add(boxSizer, 0, wx.ALIGN_RIGHT) self.panel.SetSizer(VboxSizer) #配置各个按钮 def CharSizer(self): acceltbl = [] for col in range(3): for row in range(4): ch = char[row*3 + col] button = wx.Button(self.panel, wx.NewId(), size=(35, 30), label=ch) self.sizer.Add(button, (row, col)) #每个按钮对应各自的函式 self.Bind(wx.EVT_BUTTON, self.accel[ch]['func'], button) #快速键 if self.accel[ch].has_key('ord'): acceltbl.append((0, self.accel[ch]['ord'], button.GetId())) for ch in form: button = wx.Button(self.panel, wx.NewId(), size=(35, 30), label=ch) #如果需要跨航、列 if form[ch].has_key('span'): #按钮、位置、跨越、样式 self.sizer.Add(button, form[ch]['pos'], form[ch]['span'], wx.EXPAND) else: self.sizer.Add(button, form[ch]['pos']) if ch in self.accel: self.Bind(wx.EVT_BUTTON, self.accel[ch]['func'], button) if self.accel[ch].has_key('ord'): acceltbl.append((0, self.accel[ch]['ord'], button.GetId())) acceltbl = wx.AcceleratorTable(acceltbl) self.SetAcceleratorTable(acceltbl) #监听text def OnKey(self, evt): code = evt.GetKeyCode() #如果为back或是del键 if code in (8, 127): if self.length: self.text.Remove(self.length, self.length-1) self.length -= 1 else: self.text.SetValue('') #如果为Esc则清空 elif code == 27: self.text.SetValue('0') self.length = self.value1 = self.value2 = 0 self.oper = self.enter = '' #如果为Enter则运算 elif code in (13, 370): self.value2 = self.GetValue() self.Operation() self.oper = '' #小数点判断 def OnPoint(self, evt): if self.length == 0: self.text.SetValue('0.') self.length = 2 self.text.SetInsertionPointEnd() elif '.' not in self.text.GetValue(): self.OnNum('.') #0字元判断 def OnZero(self, evt): text = self.text.GetValue() if (self.length == 1 and text[0] != '0') or '.' in text or self.length == 0 or self.length > 1: self.OnNum('0') def OnOne(self, evt): self.OnNum('1') def OnTwo(self, evt): self.OnNum('2') def OnThree(self, evt): self.OnNum('3') def OnFour(self, evt): self.OnNum('4') def OnFive(self, evt): self.OnNum('5') def OnSix(self, evt): self.OnNum('6') def OnSeven(self, evt): self.OnNum('7') def OnEight(self, evt): self.OnNum('8') def OnNight(self, evt): self.OnNum('9') def OnNum(self, num): if not self.length: self.text.SetValue(num) elif self.length < 30: self.text.SetValue(self.text.GetValue() + num) self.text.SetInsertionPointEnd() self.length += 1 def OnEsc(self, evt): self.text.SetValue('0') self.length = self.value1 = self.value2 = 0 self.oper = '' #除法 def OnDiv(self, evt): if not self.value1 and not self.oper: self.value1 = self.GetValue() elif self.length != 0: self.value2 = self.GetValue() self.Operation() self.oper = '/' self.length = 0 self.text.SetInsertionPointEnd() #乘法 def OnMulti(self, evt): if not self.value1 and not self.oper: self.value1 = self.GetValue() elif self.length != 0: self.value2 = self.GetValue() self.Operation() self.oper = '*' self.length = 0 self.text.SetInsertionPointEnd() #减法 def OnDec(self, evt): if not self.value1 and not self.oper: self.value1 = self.GetValue() elif self.length != 0: self.value2 = self.GetValue() elif self.oper == '-': self.value1 *= -1 self.text.SetValue(str(self.value1)) self.Operation() self.oper = '-' self.length = 0 self.text.SetInsertionPointEnd() #加法 def OnAdd(self, evt): if not self.value1 and not self.oper: self.value1 = self.GetValue() elif self.length != 0: self.value2 = self.GetValue() self.Operation() self.oper = '+' self.length = 0 self.text.SetInsertionPointEnd() def OnEnter(self, evt): self.value2 = self.GetValue() self.Operation() self.oper = '' #从text中传回数值 def GetValue(self): text = self.text.GetValue() if '.' in text: return string.atof(text) else: return string.atol(text) #运算 def Operation(self): if self.length: if self.oper == '+': self.value1 += self.value2 elif self.oper == '-': self.value1 -= self.value2 elif self.oper == '*': self.value1 *= self.value2 elif self.oper == '/': self.value1 /= float(self.value2) if len(str(self.value1)) > 30: self.value1 = float(self.value1) self.text.SetValue(str(self.value1)) self.text.SetInsertionPointEnd() app = wx.PySimpleApp() Compute().Show() app.MainLoop() 主要是想说明Sizer真的很好用XD 里面功能也只有加减乘除很阳春 Sizer中文说明可以看这边 大陆人翻译的 http://www.pythontik.com/blog/article.asp?id=275 大家都来脑力激荡一下( 别炮我0.0 ) --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.127.61.5
1F:推 rs6000:这个真的不错喔^^对python麻瓜的我来说很受用:D 01/23 21:14
2F:推 rexrainbow:推 01/24 01:48







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP