一个简单的计算器

1.要设计好计算器的屏幕布局,行,列,按钮的大小

2.要设计好逻辑,计算功能(加减乘除),清除功能,返回功能,等于功能,屏幕显示功能。

1.导入模块

import tkinter as tk

2.实例化一个窗体对象

root = tk.Tk()

设置窗口的大小长宽为300x300

出现的位置距离窗口左上角+150+150

3.

root.geometry("295x280+150+150")
root.title('计算器')
root.attributes("-alpha", 0.9)
root["background"] = "#ffffff"

先定义一个列表收集输入的内容

lists = []
result_num = tk.StringVar()
result_num.set(0)

返回

def back():#定义返回函数
    del lists[-1]
    result_num.set(lists)

显示数字

def append_num(i):
    lists.append(i)
    result_num.set(''.join(lists))

加减乘除

def operator(i):
    if len(lists) > 0:
        if lists[-1] in ['+', '-', '*', '/']:
            lists[-1] = i
        else:
            lists.append(i)
        result_num.set(''.join(lists))

等于

def equal():
    a = ''.join(lists)
    end_num = eval(a)
    result_num.set(end_num)
    lists.clear()
    lists.append(str(end_num))

清空

def clear():
    lists.clear()
    result_num.set(0)

按钮界面设置一下~

lable1 = tk.Label(root, textvariable=result_num, width=20, height=2, font=('宋体', 20), justify='left', background='#ffffff', anchor='se')
lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)

button_clear = tk.Button(root, text='C', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: clear())
button_back = tk.Button(root, text='←', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: back())
button_division = tk.Button(root, text='/', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('/'))
button_multiplication = tk.Button(root, text='x', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('*'))
button_clear            .grid(padx=4, row=1, column=0)
button_back             .grid(padx=4, row=1, column=1)
button_division         .grid(padx=4, row=1, column=2)
button_multiplication   .grid(padx=4, row=1, column=3)

button_seven = tk.Button(root, text='7', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('7'))
button_eight = tk.Button(root, text='8', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('8'))
button_nine = tk.Button(root, text='9', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('9'))
button_subtraction = tk.Button(root, text='—', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('-'))

别忘记进入消息循环,显示窗口

root.mainloop()

import tkinter as tk#导入tk模块
root = tk.Tk()#创建一个计算器对象
root.title('简单计算器')#计算器的名字
root.geometry('295x280+100+100')#计算器的屏幕大小
root.attributes("-alpha",0.9)#计算器的透明度是0.9
root["background"] = "#ffffff"#设置计算器的背景颜色
font = ('宋体',20)
font_16 = ('宋体',16)
lists = []#屏幕上输入的空列表lists
result_num = tk.StringVar()#
result_num.set('')#计算器开始默认没有任何输入
tk.Label(root,
         textvariable=result_num,font=font,height=2,
         width=20,justify=tk.LEFT,anchor=tk.SE
         ).grid(row=1,column=1,columnspan=4)#设置第一个表格的字体和行位置,默认是第一行开头,跨4列到最后。SE,是右下角
def append_num(i):#定义添加函数
    lists.append(i)
    result_num.set(''.join(lists))
def operator(i):#定义运算函数
    if len(lists) > 0:
        if lists[-1] in ['+', '-', '*', '/']:
            lists[-1] = i
        else:
            lists.append(i)
        result_num.set(''.join(lists))
def equal():#定义求和函数
    a = ''.join(lists)
    end_num = eval(a)
    result_num.set(end_num)
    lists.clear()
    lists.append(str(end_num))

def clear():#定义清除函数
    lists.clear()
    result_num.set(0)

def back():#定义返回函数
    del lists[-1]
    result_num.set(lists)

button_clear = tk.Button(root,text='C',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: clear())#设置按钮的大小,字体,颜色
button_back = tk.Button(root,text='←',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: back())
button_division = tk.Button(root,text='/',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: operator('/'))
button_multplicatiom = tk.Button(root,text='x',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: operator('*'))
button_clear.grid(row=2,column=1,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_back.grid(row=2,column=2,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_division.grid(row=2,column=3,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_multplicatiom.grid(row=2,column=4,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_seven = tk.Button(root,text='7',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('7'))#设置按钮的大小,字体,颜色
button_eight = tk.Button(root,text='8',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('8'))
button_nine = tk.Button(root,text='9',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('9'))
button_subtraction = tk.Button(root,text='-',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: operator('-'))
button_seven.grid(row=3,column=1,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_eight.grid(row=3,column=2,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_nine.grid(row=3,column=3,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_subtraction.grid(row=3,column=4,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_four = tk.Button(root,text='4',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('4'))#设置按钮的大小,字体,颜色
button_five = tk.Button(root,text='5',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('5'))
button_six = tk.Button(root,text='6',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('6'))
button_additon = tk.Button(root,text='+',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: operator('+'))
button_four.grid(row=4,column=1,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_five.grid(row=4,column=2,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_six.grid(row=4,column=3,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_additon.grid(row=4,column=4,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_one = tk.Button(root,text='1',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('1'))#设置按钮的大小,字体,颜色
button_two = tk.Button(root,text='2',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('2'))
button_three = tk.Button(root,text='3',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('3'))
button_equal = tk.Button(root,text='=',width=5,height=3,font=font_16,relief=tk.FLAT,bg='#b1b2b2', command=lambda: equal())#height=3把“=按钮所在的”5和6行合并变高了
button_one.grid(row=5,column=1,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_two.grid(row=5,column=2,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_three.grid(row=5,column=3,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_equal.grid(row=5,column=4,padx=4,pady=2,rowspan=2)#布局设置按钮的所在的行和按钮之间的间隔
button_zero = tk.Button(root,text='0',width=12,font=font_16,relief=tk.FLAT,bg='#eacda1')#设置按钮的大小,字体,颜色
#button_zero1 = tk.Button(root,text='0',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1')
button_dot = tk.Button(root,text='.',width=5,font=font_16,relief=tk.FLAT,bg='#eacda1', command=lambda: append_num('.'))
#button_equal2 = tk.Button(root,text='=',width=5,font=font_16,relief=tk.FLAT,bg='#b1b2b2')
button_zero.grid(row=6,column=1,padx=4,pady=2,columnspan=2)#布局设置按钮的所在的行和按钮之间的间隔columnspan=2,把“0按钮所在的”1和2列合并
#button_zero1.grid(row=6,column=2,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
button_dot.grid(row=6,column=3,padx=4,pady=2)#布局设置按钮的所在的行和按钮之间的间隔
#button_equal.grid(row=6,column=4,padx=4,pady=2)#布局设置按钮的所在的行和
""""点击事件"""
def click_button(x):
    print('x:\t',x)#打印x且换行
    #result_num.set(x)#把x的返回值打印在计算器上,即计算机屏幕上的输入
    result_num.set(result_num.get() + x)#这个是字符拼接,之前输入的数字+加上现在输入的数字
def calculation():#定义计算函数
    opt_str = result_num.get()#获取屏幕上的输入
    result = eval(opt_str)#eval函数是计算用的,计算结果返回给result
    result_num.set(str(result))#把结果强行转换为字符型
button_one.config(command=lambda :click_button('1'))
button_two.config(command=lambda :click_button('2'))
button_three.config(command=lambda :click_button('3'))
button_four.config(command=lambda :click_button('4'))
button_five.config(command=lambda :click_button('5'))
button_six.config(command=lambda :click_button('6'))
button_seven.config(command=lambda :click_button('7'))
button_eight.config(command=lambda :click_button('8'))
button_nine.config(command=lambda :click_button('9'))
button_zero.config(command=lambda :click_button('0'))
button_additon.config(command=lambda :click_button('+'))
button_subtraction.config(command=lambda :click_button('-'))
button_multplicatiom.config(command=lambda :click_button('*'))
button_division.config(command=lambda :click_button('/'))

"""加减乘除的屏幕上输出"""
button_equal.config(command=calculation)#相应等于事件,开始计算


root.mainloop()

你可能感兴趣的:(java,服务器,html)