python tkinter计算器实例_基于python的Tkinter实现一个简易计算器

本文实例介绍了基于python的Tkinter实现简易计算器的详细代码,分享给大家供大家参考,具体内容如下

第一种:使用python 的 Tkinter实现一个简易计算器

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

#coding:utf-8

from Tkinterimport *

import time

root= Tk()

def cacl(input_str):

if "x" in input_str:

ret= input_str.split("x")

return int(ret[0])* int(ret[1])

def callback(n):

print n

def callback1(n):

print n

class App:

def __init__(self, master):

frame1= Frame(master)

frame1.pack()

frame= Frame(master)

frame.pack()

Button(frame, text="1",command=lambda: callback(1) ).grid(row=0,column=0)

Button(frame, text="2",command=lambda: callback(2) ).grid(row=0,column=1)

Button(frame, text="3",command=lambda: callback(3) ).grid(row=0,column=2)

Button(frame, text="4",command=lambda: callback(4) ).grid(row=1,column=0)

Button(frame, text="5",command=lambda: callback(5) ).grid(row=1,column=1)

Button(frame, text="6",command=lambda: callback(6) ).grid(row=1,column=2)

Button(frame, text="7",command=lambda: callback(7) ).grid(row=2,column=0)

Button(frame, text="8",command=lambda: callback(8) ).grid(row=2,column=1)

Button(frame, text="9",command=lambda: callback(9) ).grid(row=2,column=2)

Button(frame, text="0",command=lambda: callback(0) ).grid(row=3,column=0)

Button(frame, text="+",command=lambda: callback1("+") ).grid(row=3,column=1)

Button(frame, text="-",command=lambda: callback1("-") ).grid(row=3,column=2)

Button(frame, text="*",command=lambda: callback1("*") ).grid(row=4,column=1)

Button(frame, text="/",command=lambda: callback1("/") ).grid(row=4,column=2)

Button(frame, text="=", command=self.say_hi).grid(row=4,column=0)

w= Label(frame1,text="输入结果")

w.pack()

self.e= Entry(frame1)

self.e.pack(padx=5)

w1= Label(frame1,text="计算结果")

w1.pack()

v= StringVar()

e1= Entry(frame1, textvariable=v)

v.set("")

self.v= v

e1.pack()

def say_hi(self):

print "hi there, everyone!",self.e.get()

input_str= self.e.get()

self.v.set(cacl(input_str))

app= App(root)

root.mainloop()

第二种:基于Tkinter用50行Python代码实现简易计算器

Tkinter一般是python自带的,所以代码不需要其他组件,本程序是在python2.7版本实现的。

主要涉及了tkinter的使用,函数定义和调用,匿名函数的使用,类成员函数定义等python基础知识,适合新手学习。

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

from Tkinterimport *

#创建横条型框架

def frame(root, side):

w= Frame(root)

w.pack(side= side, expand= YES, fill= BOTH)

return w

#创建按钮

def button(root, side, text, command= None):

w= Button(root, text= text, command= command)

w.pack(side= side, expand= YES, fill= BOTH)

return w

#继承了Frame类,初始化程序界面的布局

class Calculator(Frame):

def __init__(self):

Frame.__init__(self)

self.pack(expand= YES, fill= BOTH)

self.master.title('Simple Calculater')

display= StringVar()

#添加输入框

Entry(self, relief= SUNKEN,

textvariable= display).pack(side= TOP, expand= YES,

fill= BOTH)

#添加横条型框架以及里面的按钮

for keyin('123','456','789','-0.'):

keyF= frame(self, TOP)

for charin key:

button(keyF, LEFT, char,lambda w= display, c= char:w.set(w.get()+ c))

#添加操作符按钮

opsF= frame(self, TOP)

for charin '+-*/=':

if char== '=':

btn= button(opsF, LEFT, char)

btn.bind('',lambda e, s= self, w= display:s.calc(w),'+')

else:

btn= button(opsF, LEFT, char,lambda w= display, s= '%s' %char:w.set(w.get()+ s))

#添加清除按钮

clearF= frame(self, BOTTOM)

button(clearF, LEFT,'clear',lambda w= display:w.set(''))

#调用eval函数计算表达式的值

def calc(self, display):

try:

display.set(eval(display.get()))

except:

display.set("ERROR")

#程序的入口

if __name__== '__main__':

print('ok')

Calculator().mainloop()

实现效果如下图:

1-200P510045X45.jpg

以上就是本文的全部内容,希望对大家的学习Python程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值