刚开始学习tkinter,试着做了个计算器,记录一些常用的函数和使用方法。
from tkinter import * #导入tkinter库
import re
root =Tk() #给窗体
root.title('calculator') #设置窗体名字
root.geometry('290x427')
root.resizable(width=False, height=False) ###固定窗体大小
c_width=9
c_height=3
frm=Frame(root,bg='white') #新建框架
frm.pack(expand = YES,fill = BOTH) #放置框架
display=StringVar()
e=Entry(frm,textvariable=display,font=("宋体",30,"normal"),width=9) #添加输入框
e.grid(row=0,column=0,rowspan=2,columnspan=4,sticky=W+N+E) #放置输入框位置
def insert_plus():
e.insert(INSERT, '+')
def insert_minus():
e.insert(INSERT, '-')
def insert_multiply():
str_value = display.get()
if str_value != "":
e.insert(INSERT, '*')
def insert_divide():
str_value = display.get()
if str_value != "":
e.insert(INSERT, '/')
def insert_equal():
e.insert(INSERT,'=')
Button(frm,text='CE',width=c_width,height=c_height,bg='white',command= lambda :del_one_str(display)).grid(row=2,column=0,sticky=W) #设置按钮,lambda为匿名函数
Button(frm,text='C ',width=c_width,height=c_height,bg='white',command= lambda :display.set('')).grid(row=2,column=1)
Button(frm,text='<-',width=c_width,height=c_height,command= lambda :del_one_chr(display)).grid(row=2,column=2)
Button(frm,text='%',width=c_width,height=c_height,bg='white',command= lambda :calc_percent(display)).grid(row=3,column=0,sticky=W)
Button(frm,text='(',width=c_width,height=c_height,bg='white',command= lambda :e.insert(INSERT,'(')).grid(row=3,column=1)
Button(frm,text=')',width=c_width,height=c_height,bg='white',command= lambda :e.insert(INSERT,')')).grid(row=3,column=2)
Button(frm,text='1',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'1')).grid(row=4,column=0,sticky=W)
Button(frm,text='2',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'2')).grid(row=4,column=1)
Button(frm,text='3',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'3')).grid(row=4,column=2)
Button(frm,text='4',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'4')).grid(row=5,column=0,sticky=W)
Button(frm,text='5',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'5')).grid(row=5,column=1)
Button(frm,text='6',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'6')).grid(row=5,column=2)
Button(frm,text='7',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'7')).grid(row=6,column=0,sticky=W)
Button(frm,text='8',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'8')).grid(row=6,column=1)
Button(frm,text='9',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'9')).grid(row=6,column=2)
Button(frm,text='±',width=c_width,height=c_height,bg='yellow',command= lambda :negative(display)).grid(row=7,column=0,sticky=W)
Button(frm,text='0 ',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'0')).grid(row=7,column=2)
Button(frm,text='. ',width=c_width,height=c_height,bg='yellow',command= lambda :e.insert(INSERT,'.')).grid(row=7,column=1)
Button(frm,text='+',width=c_width,height=c_height,bg='white',command=insert_plus).grid(row=2,column=3,sticky=E)
Button(frm,text='-',width=c_width,height=c_height,bg='white',command=insert_minus).grid(row=3,column=3,sticky=E)
Button(frm,text='*',width=c_width,height=c_height,bg='white',command=insert_multiply).grid(row=4,column=3,sticky=E)
Button(frm,text='/',width=c_width,height=c_height,bg='white',command=insert_divide).grid(row=5,column=3,sticky=E)
Button(frm,text='=',width=c_width,height=c_height,bg='white',command= lambda :cal(display)).grid(row=6,column=3,sticky=N+S,rowspan=2)
def cal(display):
str_value = display.get()
if str_value != "":
try:
if "%" in str_value:
str_value_change=str_value.replace("%","/100")
value_tmp = eval(str_value_change)
value=value_tmp
if len(str(value_tmp)) > 16:
value="%e" % value_tmp
else:
value_tmp = eval(str_value) #eval:计算字符串表达式
value = value_tmp
if len(str(value_tmp)) > 16:
value = "%e" % value_tmp
display.set("")
e.insert(INSERT, value)
except:
display.set('')
e.insert(INSERT, "para error")
def negative(display):
str_value=display.get()
if str_value != "" and ('-' not in str_value) and ('+' not in str_value) and ('*' not in str_value) and ('/' not in str_value):
value = eval(display.get()) * (-1)
display.set('')
e.insert(INSERT,str(value))
elif str_value != "" and "-" == str_value[0] and ('-' not in str_value[1:-1]) and ('+' not in str_value[1:-1]) and ('*' not in str_value[1:-1]) and ('/' not in str_value[1:-1]):
value = eval(display.get()) * (-1)
display.set('')
e.insert(INSERT, str(value))
else:
e.insert(INSERT, '-')
def calc_percent(display):
str_value=display.get()
if str_value != "" and ('-' not in str_value) and ('+' not in str_value) and ('*' not in str_value) and ('/' not in str_value):
value = eval(display.get()) / (100)
display.set('')
e.insert(INSERT,str(value))
elif str_value != "" and "-" == str_value[0] and ('-' not in str_value[1:-1]) and ('+' not in str_value[1:-1]) and ('*' not in str_value[1:-1]) and ('/' not in str_value[1:-1]):
value = eval(display.get()) / (100)
display.set('')
e.insert(INSERT, str(value))
else:
e.insert(INSERT, '%')
def del_one_chr(display):
str_value = display.get()
if str_value != "":
display.set('')
if "para error" == str_value or "para error" in str_value:
display.set('')
else:
value = str_value[0:-1]
e.insert(INSERT, str(value))
def del_one_str(display):
str_value = display.get()
value = ""
if str_value != "":
str_value_arr = re.split(r"[\+\-\*\/]+", str_value)
value=str_value[0:len(str_value) - len(str_value_arr[-1])]
display.set('')
e.insert(INSERT, str(value))
root.mainloop() #让程序一直循环
运行效果: