Tkinter实现一个简易的计算器

前面学习了tkinter的几种布局方法,现在实现一个简单的计算器吧~~

import tkinter as tk

window=tk.Tk()
window.resizable(0,0)
first_number=0
second_number=0
number_mark=1
method=None
def get_int():
    global first_number,second_number,number_mark
    if number_mark==1:
        first_number=int(entry.get())
    if number_mark==2:
        second_number=int(entry.get())
    print(first_number,second_number)
def calculation_method(cal_method):
    global method
    method = cal_method
    get_int()
    entry.delete(0, "end")
def result():
    global method,first_number,second_number,number_mark
    if method!=None:
        number_mark = 2
        get_int()
        entry.delete(0, "end")
        if method=="add":entry.insert("end",first_number+second_number)
        if method=="decrease":entry.insert("end",first_number-second_number)
        if method=="multi":entry.insert("end",first_number*second_number)
        if method=="division":entry.insert("end",first_number/second_number)
    else:
        pass
def disply(number):
    entry.insert("end",number)
def clear():
    global first_number,second_number,number_mark,method
    entry.delete(0,"end")
    first_number = 0
    second_number = 0
    number_mark = 1
    method = None
entry=tk.Entry(window,width=25)
entry.grid(row=0,column=0,columnspan=4)
tk.Button(window,text="1",font=("微软雅黑",15),width=3,command=lambda: disply(1)).grid(row=1,column=0)
tk.Button(window,text="2",font=("微软雅黑",15),width=3,command=lambda: disply(2)).grid(row=1,column=1)
tk.Button(window,text="3",font=("微软雅黑",15),width=3,command=lambda: disply(3)).grid(row=1,column=2)
tk.Button(window,text="+",font=("微软雅黑",15),width=3,command=lambda: calculation_method("add")).grid(row=1,column=3)
tk.Button(window,text="4",font=("微软雅黑",15),width=3,command=lambda: disply(4)).grid(row=2,column=0)
tk.Button(window,text="5",font=("微软雅黑",15),width=3,command=lambda: disply(5)).grid(row=2,column=1)
tk.Button(window,text="6",font=("微软雅黑",15),width=3,command=lambda: disply(6)).grid(row=2,column=2)
tk.Button(window,text="-",font=("微软雅黑",15),width=3,command=lambda: calculation_method("decrease")).grid(row=2,column=3)
tk.Button(window,text="7",font=("微软雅黑",15),width=3,command=lambda: disply(7)).grid(row=3,column=0)
tk.Button(window,text="8",font=("微软雅黑",15),width=3,command=lambda: disply(8)).grid(row=3,column=1)
tk.Button(window,text="9",font=("微软雅黑",15),width=3,command=lambda: disply(9)).grid(row=3,column=2)
tk.Button(window,text="x",font=("微软雅黑",15),width=3,command=lambda: calculation_method("multi")).grid(row=3,column=3)
tk.Button(window,text="CLE",font=("微软雅黑",15),width=3,command=clear).grid(row=4,column=0)
tk.Button(window,text="0",font=("微软雅黑",15),width=3,command=lambda: disply(0)).grid(row=4,column=1)
tk.Button(window,text="=",font=("微软雅黑",15),width=3,command=result).grid(row=4,column=2)
tk.Button(window,text="/",font=("微软雅黑",15),width=3,command=lambda: calculation_method("division")).grid(row=4,column=3)
window.mainloop()

效果图如下:
Tkinter实现一个简易的计算器_第1张图片

你可能感兴趣的:(python)