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

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

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

#coding:utf-8
from tkinter import *
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基础知识,适合新手学习。

代码如下:

from tkinter import *
#创建横条型框架
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 key in(‘123’, ‘456’, ‘789’, ‘-0.’):
keyf = frame(self, top)
for char in key:
button(keyf, left, char, lambda w = display, c = char:w.set(w.get() + c))
#添加操作符按钮
opsf = frame(self, top)
for char in ‘+-*/=’:
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()

实现效果如下图:

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