【python】tkinter绘制简易计算器

版权声明:本文为博主原创文章,转载请注明出处-- https://blog.csdn.net/qq_38790716/article/details/90770505
题目:

使用 t k i n t e r tkinter 绘图库,绘制一个简易计算器。

(1)导入头文件:

from tkinter import *      #tkinter库
import tkinter.font as tkFont   #导入font,用于修改输入字体大小
from functools import partial   #导入partial函数,用于修改控件的默认配置

(2)绘制窗口:设置窗口大小为不可变,修改窗口标题

root=Tk()
root.title("Calc")   #修改窗口标题为Calc
root.resizable(0,0)  #设置窗口大小不可变

在这里插入图片描述

(3)显示文本框:使用 E n t r y Entry 控件,修改文本框字体大小

entry_font=tkFont.Font(size=12)   #设置文本框中的字体大小
entry=Entry(root, justify="right", font=entry_font)   #设置文本框靠右
#设置文本框的样式
entry.grid(row=0, column=0, columnspan=4, sticky=W+E+N+S, padx=5, pady=5)

在这里插入图片描述

(4)修改按钮默认配置:

#D5E0EE:
在这里插入图片描述
#E5E35B:
在这里插入图片描述

button_font=tkFont.Font(size=10, weight=tkFont.BOLD)  #设置字体及宽度
button_bg='#D5E0EE'  #设置默认按钮颜色
button_active_bg='#E5E35B'    #设置活跃按钮颜色
#修改按钮控件的默认配置
myButton=partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground=button_active_bg)

Python3学习(18)–偏函数(Partial)

(5)设置事件处理函数:’<-’:回退、‘C’:清除、’=’:结果处理,其它按钮:获取按钮输入输出到文本框

#获取按钮输入插入到文本框(调用entry中insert函数)
def get_input(entry, argu):
	entry.insert(END, argu)

#回退操作,调用entry中get函数获取文本框输入字符串,计算其长度,删除第input_len-1位置的字符
def back_sapce(entry):
	input_len = len(entry.get())
	entry.delete(input_len-1)


#清除操作,将从0到END字符全部清除
def delete(entry):
	entry.delete(0, END)

#处理结果,将输入的数据调用strip函数去除左右两边的空格,再调用eval函数处理表达式的结果
#调用delete函数清空文本框,将结果输出到文本框
def calc(entry):
	input=entry.get()
    output=str(eval(input.strip()))
    delete(entry)
    entry.insert(END,output)

(6)绘制按钮:合理设置按钮位置,并采用 l a m b d a lambda 表达式调用事件处理函数

	#设置按钮位置,并采用lambda表达式设置处理函数
    button7=myButton(text='7',command=lambda : get_input(entry, '7'))
    button7.grid(row=1, column=0, pady=5)

    button8=myButton(text='8',command=lambda : get_input(entry, '8'))
    button8.grid(row=1, column=1, pady=5)

    button9=myButton(text='9',command=lambda : get_input(entry, '9'))
    button9.grid(row=1, column=2, pady=5)

    button10=myButton(text='+',command=lambda : get_input(entry, '+'))
    button10.grid(row=1, column=3, pady=5)

    button4=myButton(text='4',command=lambda : get_input(entry, '4'))
    button4.grid(row=2, column=0, pady=5)

    button5=myButton(text='5',command=lambda : get_input(entry, '5'))
    button5.grid(row=2, column=1, pady=5)

    button6=myButton(text='6',command=lambda : get_input(entry, '6'))
    button6.grid(row=2, column=2, pady=5)

    button11=myButton(text='-',command=lambda : get_input(entry, '-'))
    button11.grid(row=2, column=3, pady=5)

    button1=myButton(text='1',command=lambda : get_input(entry, '1'))
    button1.grid(row=3, column=0, pady=5)

    button2=myButton(text='2',command=lambda : get_input(entry, '2'))
    button2.grid(row=3, column=1, pady=5)

    button3=myButton(text='3',command=lambda : get_input(entry, '3'))
    button3.grid(row=3, column=2, pady=5)

    button12=myButton(text='*',command=lambda : get_input(entry, '*'))
    button12.grid(row=3, column=3, pady=5)

    button0=myButton(text='0',command=lambda : get_input(entry, '0'))
    button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=W+E+N+S)

    button13=myButton(text='.',command=lambda : get_input(entry, '.'))
    button13.grid(row=4, column=2, pady=5)

    button14=myButton(text='/',command=lambda : get_input(entry, '/'))
    button14.grid(row=4, column=3, pady=5)

    button15=myButton(text='<-',command=lambda : back_space(entry))
    button15.grid(row=5, column=0, pady=5)

    button16=myButton(text='C',command=lambda : delete(entry))
    button16.grid(row=5, column=1, pady=5)

    button17=myButton(text='=',command=lambda : calc(entry))
    button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=W+E+N+S)

在这里插入图片描述

完整代码

from tkinter import *
import tkinter.font as tkFont
from functools import partial

#向文本框中插入数据
def get_input(entry, argu):
    entry.insert(END, argu)

#回退操作,即将文本款中字符串删除一个
def back_space(entry):
    input_len=len(entry.get())
    entry.delete(input_len-1)

#清空操作,即将文本框中所有字符删除
def delete(entry):
    entry.delete(0, END)

#计算表达式,get得到文本框中的表达式,使用eval函数计算表达式的值
def calc(entry):
    input=entry.get()
    output=str(eval(input.strip()))
    delete(entry)
    entry.insert(END,output)
    
def cal():
    root=Tk()
    root.title("Calc")
    root.resizable(0,0)

    entry_font=tkFont.Font(size=12)   #设置文本框中的字体大小
    entry=Entry(root, justify="right", font=entry_font)
    #设置文本框的样式
    entry.grid(row=0, column=0, columnspan=4, sticky=W+E+N+S, padx=5, pady=5)

    #更改按钮的默认设置
    button_font=tkFont.Font(size=10, weight=tkFont.BOLD)
    button_bg='#D5E0EE'
    button_active_bg='#E5E35B'

    myButton=partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground=button_active_bg)
    

    #设置按钮位置,并采用lambda表达式设置处理函数
    button7=myButton(text='7',command=lambda : get_input(entry, '7'))
    button7.grid(row=1, column=0, pady=5)

    button8=myButton(text='8',command=lambda : get_input(entry, '8'))
    button8.grid(row=1, column=1, pady=5)

    button9=myButton(text='9',command=lambda : get_input(entry, '9'))
    button9.grid(row=1, column=2, pady=5)

    button10=myButton(text='+',command=lambda : get_input(entry, '+'))
    button10.grid(row=1, column=3, pady=5)

    button4=myButton(text='4',command=lambda : get_input(entry, '4'))
    button4.grid(row=2, column=0, pady=5)

    button5=myButton(text='5',command=lambda : get_input(entry, '5'))
    button5.grid(row=2, column=1, pady=5)

    button6=myButton(text='6',command=lambda : get_input(entry, '6'))
    button6.grid(row=2, column=2, pady=5)

    button11=myButton(text='-',command=lambda : get_input(entry, '-'))
    button11.grid(row=2, column=3, pady=5)

    button1=myButton(text='1',command=lambda : get_input(entry, '1'))
    button1.grid(row=3, column=0, pady=5)

    button2=myButton(text='2',command=lambda : get_input(entry, '2'))
    button2.grid(row=3, column=1, pady=5)

    button3=myButton(text='3',command=lambda : get_input(entry, '3'))
    button3.grid(row=3, column=2, pady=5)

    button12=myButton(text='*',command=lambda : get_input(entry, '*'))
    button12.grid(row=3, column=3, pady=5)

    button0=myButton(text='0',command=lambda : get_input(entry, '0'))
    button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=W+E+N+S)

    button13=myButton(text='.',command=lambda : get_input(entry, '.'))
    button13.grid(row=4, column=2, pady=5)

    button14=myButton(text='/',command=lambda : get_input(entry, '/'))
    button14.grid(row=4, column=3, pady=5)

    button15=myButton(text='<-',command=lambda : back_space(entry))
    button15.grid(row=5, column=0, pady=5)

    button16=myButton(text='C',command=lambda : delete(entry))
    button16.grid(row=5, column=1, pady=5)

    button17=myButton(text='=',command=lambda : calc(entry))
    button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=W+E+N+S)
    
    root.mainloop()

if __name__ == '__main__':
    cal()
测试


在这里插入图片描述在这里插入图片描述


在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述

回退
在这里插入图片描述在这里插入图片描述

清空
在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38790716/article/details/90770505