python tkinter计算器实例_tkinter写的计算器

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

# -*- coding: cp936 -*-

try:

from tkinter import *

import math, datetime

from tkinter import ttk

root = Tk()

display = StringVar()

lbl = Label(root, relief="sunken", borderwidth=3, anchor=SE)

lbl['textvariable'] = display

lbl.configure(background="white",height=2, width=25)

lbl.grid(row=0, column=0, columnspan=4, sticky=SE)

lbl.bind('', display.set(''))

def callback(num):

furm = display.get() + num

display.set(furm)

def calculate():

furm = display.get()

l = list(furm)

if 'm' in l and u'√' not in l:

lis = list(furm)

while 1:

try:

index = lis.index('m')

lis.insert(index, '%')

lis.remove('m')

lis.remove('o')

lis.remove('d')

except:

break

ind = 0

st = ''

while 1:

try:

st += lis[ind]

ind += 1

except:

break

try:

display.set(eval(st))

except:

display.set(u'无法计算此算式')

if u'√' in l and 'm' not in l:

done = False

jeizhi_gen_houjiwei = 0

intlistfurm1 = list(furm)

intlistfurm1.remove(u'√')

yn_int_ceshi = 0

while not(done):

try:

shiyan = float(intlistfurm1[yn_int_ceshi])

jeizhi_gen_houjiwei += 1

yn_int_ceshi += 1

except:

done = True

a = list(furm)

while 1:

try:

index = a.index(u'√')

a.insert(index, 'math.sqrt(')

a.insert(index + jeizhi_gen_houjiwei + 2, ')')

a.remove(u'√')

except:

break

string = ''

ind = 0

while ind < len(a):

string += a[ind]

ind += 1

try:

display.set(eval(string))

except:

display.set(u'无法计算此算式')

if 'm' not in l and u'√' not in l:

try:

res = eval(furm)

display.set(str(res))

except:

display.set(u'无法计算此算式')

def ac():

display.set("")

def mjia():

nu = display.get()

display.set(nu)

global nu

def mr():

try:

display.set(display.get() + nu)

except:

display.set("你还没有进行M+操作。")

def c():

nu = display.get()

lis = list(nu)

del lis[len(lis) - 1]

string = ''

index = 0

while 1:

try:

string += lis[index]

index += 1

except:

break

display.set(string)

def jiajian():

a = display.get()

try:

if float(display.get()) > 0:

display.set('-' + display.get())

else:

if int(abs(float(a))) != abs(float(a)):

display.set(abs(float(a)))

else:

display.set(int(abs(float(a))))

except:

display.set(u'无法进行该操作')

frame = Frame(root)

root.title(u"计算器")

ttk.Button(root, text="7", width=5, command=lambda: callback("7")).grid(row=3, column=0)

ttk.Button(root, text="8", width=5, command=lambda: callback("8")).grid(row=3, column=1)

ttk.Button(root, text="9", width=5, command=lambda: callback("9")).grid(row=3, column=2)

ttk.Button(root, text="+", width=5, command=lambda: callback("+")).grid(row=3, column=3)

ttk.Button(root, text="4", width=5, command=lambda: callback("4")).grid(row=4, column=0)

ttk.Button(root, text="5", width=5, command=lambda: callback("5")).grid(row=4, column=1)

ttk.Button(root, text="6", width=5, command=lambda: callback("6")).grid(row=4, column=2)

ttk.Button(root, text="-", width=5, command=lambda: callback("-")).grid(row=4, column=3)

ttk.Button(root, text="1", width=5, command=lambda: callback("1")).grid(row=5, column=0)

ttk.Button(root, text="2", width=5, command=lambda: callback("2")).grid(row=5, column=1)

ttk.Button(root, text="3", width=5, command=lambda: callback("3")).grid(row=5, column=2)

ttk.Button(root, text="*", width=5, command=lambda: callback("*")).grid(row=5, column=3)

ttk.Button(root, text="0", width=5, command=lambda: callback("0")).grid(row=6, column=0)

ttk.Button(root, text=".", width=5, command=lambda: callback(".")).grid(row=6, column=1)

ttk.Button(root, text="mod", width=5, command=lambda: callback("mod")).grid(row=6, column=2)

ttk.Button(root, text="/", width=5, command=lambda: callback("/")).grid(row=6, column=3)

ttk.Button(root, text="AC",width = 5, command=lambda:ac()).grid(row=7, column=0)

ttk.Button(root, text="=", width = 5, command=lambda: calculate()).grid(row=7, column=1)

ttk.Button(root, text="M+", width = 5, command=lambda: mjia()).grid(row = 6, column = 4)

ttk.Button(root, text="MR", width = 5, command=lambda: mr()).grid(row = 5, column = 4)

ttk.Button(root, text='→', width = 5, command=lambda: c()).grid(row = 4, column = 4)

ttk.Button(root, text='.', width = 5, command=lambda: callback('.')).grid(row = 3, column = 4)

ttk.Button(root, text='√', width = 5, command=lambda: callback('√')).grid(row = 3, column = 5)

ttk.Button(root, text='(', width = 5, command=lambda: callback('(')).grid(row = 4, column = 5)

ttk.Button(root, text=')', width = 5, command=lambda: callback(')')).grid(row = 5, column = 5)

ttk.Button(root, text='±', width = 5, command=lambda: jiajian()).grid(row = 6, column = 5)

root.resizable(0, 0)

root.mainloop()

except KeyboardInterrupt:

pass

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值