python学习第四天

wxpython写的计算器

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  -*- coding: utf-8 -*-才能支持中文

import os
import wx


class MainWindow(wx.Frame):
	"""docstring for MainWindow"""
	def __init__(self, parent, title):
		super(MainWindow, self).__init__(parent, title = title, size = (270, 250))
		
		self.InitUI()
		self.Center()
		self.Show()

	def InitUI(self):
		# panel = wx.Panel(self)

		box = wx.BoxSizer(wx.VERTICAL)

		# 输入框
		self.display = wx.TextCtrl(self, style = wx.TE_LEFT, value = "0")
		self.display.SetEditable(False)
		self.display.SetInsertionPoint(0)
		self.display.SetThemeEnabled(False)
		box.Add(self.display, flag = wx.EXPAND | wx.TOP | wx.BOTTOM, border = 5)

		self.buttongroup = None
		self.displayButtons()
		box.Add(self.buttongroup, flag = wx.EXPAND, border = 5)

		self.SetSizer(box)
		self.setButton()

	def displayButtons(self):
		self.button0 = wx.Button(self, label = '0')
		self.button1 = wx.Button(self, label = '1')
		self.button2 = wx.Button(self, label = '2')
		self.button3 = wx.Button(self, label = '3')
		self.button4 = wx.Button(self, label = '4')
		self.button5 = wx.Button(self, label = '5')
		self.button6 = wx.Button(self, label = '6')
		self.button7 = wx.Button(self, label = '7')
		self.button8 = wx.Button(self, label = '8')
		self.button9 = wx.Button(self, label = '9')

		self.buttonAdd = wx.Button(self, label = '+')
		self.buttonMunis = wx.Button(self, label = '-')
		self.buttonMutiply = wx.Button(self, label = '*')
		self.buttonDevide = wx.Button(self, label = '/')
		self.buttonBack = wx.Button(self, label = 'back')
		self.buttonDot = wx.Button(self, label = '.')
		self.buttonClear = wx.Button(self, label = 'clear')
		self.buttonLeft = wx.Button(self, label = '(')
		self.buttonRight = wx.Button(self, label = ')')
		self.buttonEqual = wx.Button(self, label = '=')

		self.buttongroup = wx.GridSizer(5, 4, 5, 5)
		self.buttongroup.AddMany([
			(self.button7, 0, wx.EXPAND),
			(self.button8, 0, wx.EXPAND),
			(self.button9, 0, wx.EXPAND),
			(self.buttonAdd, 0, wx.EXPAND),
			(self.button4, 0, wx.EXPAND),
			(self.button5, 0, wx.EXPAND),
			(self.button6, 0, wx.EXPAND),
			(self.buttonMunis, 0, wx.EXPAND),
			(self.button1, 0, wx.EXPAND),
			(self.button2, 0, wx.EXPAND),
			(self.button3, 0, wx.EXPAND),
			(self.buttonMutiply, 0, wx.EXPAND),
			(self.buttonBack, 0, wx.EXPAND),
			(self.button0, 0, wx.EXPAND),
			(self.buttonDot, 0, wx.EXPAND),
			(self.buttonDevide, 0, wx.EXPAND),
			(self.buttonClear, 0, wx.EXPAND),
			(self.buttonLeft, 0, wx.EXPAND),
			(self.buttonRight, 0, wx.EXPAND),
			(self.buttonEqual, 0, wx.EXPAND),
			])

	def clickButton0(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("0")
		else:
			self.display.SetValue(text + "0")

		event.Skip()

	def clickButton1(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("1")
		else:
			self.display.SetValue(text + "1")

		event.Skip()

	def clickButton2(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("2")
		else:
			self.display.SetValue(text + "2")

		event.Skip()

	def clickButton3(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("3")
		else:
			self.display.SetValue(text + "3")

		event.Skip()

	def clickButton4(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("4")
		else:
			self.display.SetValue(text + "4")

		event.Skip()

	def clickButton5(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("5")
		else:
			self.display.SetValue(text + "5")

		event.Skip()

	def clickButton6(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("6")
		else:
			self.display.SetValue(text + "6")

		event.Skip()

	def clickButton7(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("7")
		else:
			self.display.SetValue(text + "7")

		event.Skip()

	def clickButton8(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("8")
		else:
			self.display.SetValue(text + "8")

		event.Skip()

	def clickButton9(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("9")
		else:
			self.display.SetValue(text + "9")

		event.Skip()

	def clickLeftButton(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue("(")
		else:
			self.display.SetValue(text + "(")

		event.Skip()

	def clickRightButton(self, event):
		text = self.display.GetValue()
		if "0" == text:
			self.display.SetValue(")")
		else:
			self.display.SetValue(text + ")")

		event.Skip()

	def clickAddButton(self, event):
		self.display.SetValue(self.display.GetValue() + "+")
		event.Skip()

	def clickMinusButton(self, event):
		self.display.SetValue(self.display.GetValue() + "-")
		event.Skip()

	def clickMutiplyButton(self, event):
		self.display.SetValue(self.display.GetValue() + "*")
		event.Skip()

	def clickDevideButton(self, event):
		self.display.SetValue(self.display.GetValue() + "/")
		event.Skip()

	def clickDotButton(self, event):
		self.display.SetValue(self.display.GetValue() + ".")
		event.Skip()

	def clickClearButton(self, event):
		self.display.SetValue("0")
		event.Skip()

	def clickBackButton(self, event):
		text = self.display.GetValue()
		if 1 == len(text):
			self.display.SetValue("0")
		else:
			self.display.SetValue(text[0 : (len(text) - 1)])
		event.Skip()

	def clickEqualButton(self, event):
		try:
			result = eval(self.display.GetValue())
		except:
			result = "0"

		self.display.SetValue(str(result))
		
		event.Skip()

	def setButton(self):
		self.Bind(wx.EVT_BUTTON, self.clickButton0, self.button0)
		self.Bind(wx.EVT_BUTTON, self.clickButton1, self.button1)
		self.Bind(wx.EVT_BUTTON, self.clickButton2, self.button2)
		self.Bind(wx.EVT_BUTTON, self.clickButton3, self.button3)
		self.Bind(wx.EVT_BUTTON, self.clickButton4, self.button4)
		self.Bind(wx.EVT_BUTTON, self.clickButton5, self.button5)
		self.Bind(wx.EVT_BUTTON, self.clickButton6, self.button6)
		self.Bind(wx.EVT_BUTTON, self.clickButton7, self.button7)
		self.Bind(wx.EVT_BUTTON, self.clickButton8, self.button8)
		self.Bind(wx.EVT_BUTTON, self.clickButton9, self.button9)

		self.Bind(wx.EVT_BUTTON, self.clickAddButton, self.buttonAdd)
		self.Bind(wx.EVT_BUTTON, self.clickMinusButton, self.buttonMunis)
		self.Bind(wx.EVT_BUTTON, self.clickMutiplyButton, self.buttonMutiply)
		self.Bind(wx.EVT_BUTTON, self.clickDevideButton, self.buttonDevide)
		self.Bind(wx.EVT_BUTTON, self.clickEqualButton, self.buttonEqual)

		self.Bind(wx.EVT_BUTTON, self.clickBackButton, self.buttonBack)
		self.Bind(wx.EVT_BUTTON, self.clickClearButton, self.buttonClear)
		self.Bind(wx.EVT_BUTTON, self.clickDotButton, self.buttonDot)
		self.Bind(wx.EVT_BUTTON, self.clickLeftButton, self.buttonLeft)
		self.Bind(wx.EVT_BUTTON, self.clickRightButton, self.buttonRight)
		
		
		
def main():
	app = wx.App(False)
	frame = MainWindow(None, u"计算器")
	app.MainLoop()

if __name__ == '__main__':
	main()


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值