这个迷你demo主要用到的是Tkinter模块。
准备工作:pip3 install tkinter
from tkinter import Tk, END, Entry, N, E, S, W
from tkinter import font
from tkinter import Label
from functools import partial
# 使用tkmacosx模块中的Button,是因为tkinter无法在mac上设置按钮颜色
from tkmacosx import Button
def get_input(entry, argu):
# 将输入的值或符号添加到文本框末尾
entry.insert(END, argu)
def backspace(entry):
# 点击'CE'时,删除最后一个字符
# delete通过传入参数来指定删除文本,(input_len-1)为起始,默认last为终止
input_len = len(entry.get())
entry.delete(input_len-1)
def clear(entry):
# 删除从0到END的内容,即全部清除。
entry.delete(0, END)
def calc(entry):
input_info = entry.get()
try:
# 当点击'='号时,通过eval去计算表达式的值
output = str(eval(input_info.strip()))
except ZeroDivisionError:
popupmsg()
output = ""
clear(entry)
entry.insert(END, output)
def popupmsg():
"""
发生除以0时,弹出异常
:return:
"""
popup = Tk()
popup.resizable(0, 0)
popup.geometry("120x100")
popup.title("Alert")
lable = Label(popup, text="Connot divide by 0 ! \n Enter vaild values")
lable.pack(side="top", fill="x", pady=10)
B1 = Button(popup, text="OKay", bg="#DDDDDD", command=popup.destroy)
B1.pack()
def cal():
root = Tk()
root.title("Calc")
root.resizable(0, 0)
entry_font = font.Font(size=15)
# Entry:创建单行文本框,用来显示要运算的公式和输出的结果
entry = Entry(root, justify="right", font=entry_font)
# 设置Entry的属性
entry.grid(row=0, column=0, columnspan=4,
sticky=N + W + S + E, padx=5, pady=5)
# 设置颜色
cal_button_bg = 'black'
num_button_bg = 'green'
other_button_bg = 'orange'
text_fg = 'red'
button_active_bg = 'orange'
# 对数字按钮和运算符按钮中相同的属性提前进行封装
num_button = partial(Button, root, fg=text_fg, bg=num_button_bg, padx=10,
pady=3, activebackground=button_active_bg)
cal_button = partial(Button, root, fg=text_fg, bg=cal_button_bg, padx=10,
pady=3, activebackground=button_active_bg)
# 第1行为 'CE','C','/'
button16 = Button(root, text='CE', bg=other_button_bg, padx=10, pady=3,
command=lambda: backspace(entry), activebackground=button_active_bg)
button16.grid(row=1, column=0, columnspan=2,
padx=3, pady=5, sticky=N + W + S + E)
button17 = Button(root, text='C', bg=other_button_bg, padx=10, pady=3,
command=lambda: clear(entry), activebackground=button_active_bg)
button17.grid(row=1, column=2, pady=5)
button18 = Button(text='/', fg=text_fg, bg=cal_button_bg, padx=10, pady=3,
command=lambda: get_input(entry, '/'))
button18.grid(row=1, column=3, pady=5)
# 第二行为数字7-9和'*'
button7 = num_button(text='7', command=lambda: get_input(entry, '7'))
button7.grid(row=2, column=0, pady=5)
button8 = num_button(text='8', command=lambda: get_input(entry, '8'))
button8.grid(row=2, column=1, pady=5)
button9 = num_button(text='9', command=lambda: get_input(entry, '9'))
button9.grid(row=2, column=2, pady=5)
button10 = cal_button(text='*', command=lambda: get_input(entry, '*'))
button10.grid(row=2, column=3, pady=5)
# 第三行为数字4-6和'-'
button4 = num_button(text='4', command=lambda: get_input(entry, '4'))
button4.grid(row=3, column=0, pady=5)
button5 = num_button(text='5', command=lambda: get_input(entry, '5'))
button5.grid(row=3, column=1, pady=5)
button6 = num_button(text='6', command=lambda: get_input(entry, '6'))
button6.grid(row=3, column=2, pady=5)
button11 = cal_button(text='-', command=lambda: get_input(entry, '-'))
button11.grid(row=3, column=3, pady=5)
# 第三行为数字1-3和'+'
button1 = num_button(text='1', command=lambda: get_input(entry, '1'))
button1.grid(row=4, column=0, pady=5)
button2 = num_button(text='2', command=lambda: get_input(entry, '2'))
button2.grid(row=4, column=1, pady=5)
button3 = num_button(text='3', command=lambda: get_input(entry, '3'))
button3.grid(row=4, column=2, pady=5)
button12 = cal_button(text='+', command=lambda: get_input(entry, '+'))
button12.grid(row=4, column=3, pady=5)
# 第五行为'.',0,幂,'='
button13 = num_button(text='.', command=lambda: get_input(entry, '.'))
button13.grid(row=5, column=0, pady=5)
button0 = num_button(text='0', command=lambda: get_input(entry, '0'))
button0.grid(row=5, column=1, pady=5)
button14 = Button(root, text='^', fg=text_fg, bg=cal_button_bg, padx=10, pady=3,
command=lambda: get_input(entry, '**'))
button14.grid(row=5, column=2, pady=5)
button15 = Button(root, text='=', fg=text_fg, bg=cal_button_bg, padx=10, pady=3,
command=lambda: calc(entry), activebackground=button_active_bg)
button15.grid(row=5, column=3, pady=5)
root.mainloop()
if __name__ == '__main__':
cal()
?
|