• 简易版计算器


    # 简易版计算器
    import tkinter as tk
    from tkinter import messagebox
    
    root = tk.Tk()
    root.title('计算器')
    root.geometry('315x310+100+100')
    root.attributes('-alpha', 0.9)  # 设置透明度
    # root.config(background='#FFFFFF')
    root.resizable(width=False, height=False)
    font = ('宋体', 24)
    font_18 = ('宋体', 18)
    
    
    def show_author():
        prt_str = 'Author: maked by yx迈克\nDate: 2022年4月9日'
        messagebox.showinfo(title='关于作者', message=prt_str)
    
    
    def about():
        prt_str = '此款计算器是简易版的计算器,仅可计算加减乘除。\n版本号:V1.0'
        messagebox.showinfo(title='关于计算器', message=prt_str)
    
    
    menu = tk.Menu(root)
    about_menu = tk.Menu(menu, tearoff=False)
    about_menu.add_command(label='作者', command=show_author)
    about_menu.add_command(label='关于', command=about)
    menu.add_cascade(label='帮助', menu=about_menu)
    root.config(menu=menu)  # 一定要加这句,否则菜单就不会展现出来
    
    # 以下是计算器按钮页面
    result_num = tk.StringVar()
    result_num.set('')
    result_lbl = tk.Label(root, textvariable=result_num, font=font, height=2, wraplength=300,
                          width=19, justify=tk.LEFT, anchor=tk.SE)
    result_lbl.grid(row=1, column=1, columnspan=4)
    
    button_clear = tk.Button(root, text='C', font=font_18, width=5, bg='#b1b2b2', relief=tk.FLAT)
    button_back = tk.Button(root, text='←', font=font_18, width=5, bg='#b1b2b2', relief=tk.FLAT)
    button_divsion = tk.Button(root, text='/', font=font_18, width=5, bg='#b1b2b2', relief=tk.FLAT)
    button_mulitplation = tk.Button(root, text='*', font=font_18, width=5, bg='#b1b2b2', relief=tk.FLAT)
    button_clear.grid(row=2, column=1, padx=4, pady=2)
    button_back.grid(row=2, column=2, padx=4, pady=2)
    button_divsion.grid(row=2, column=3, padx=4, pady=2)
    button_mulitplation.grid(row=2, column=4, padx=4, pady=2)
    
    button_seven = tk.Button(root, text='7', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_eight = tk.Button(root, text='8', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_nine = tk.Button(root, text='9', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_substraction = tk.Button(root, text='-', font=font_18, width=5, bg='#b1b2b2', relief=tk.FLAT)
    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_substraction.grid(row=3, column=4, padx=4, pady=2)
    
    button_four = tk.Button(root, text='4', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_five = tk.Button(root, text='5', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_six = tk.Button(root, text='6', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_addition = tk.Button(root, text='+', font=font_18, width=5, bg='#b1b2b2', relief=tk.FLAT)
    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_addition.grid(row=4, column=4, padx=4, pady=2)
    
    button_one = tk.Button(root, text='1', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_two = tk.Button(root, text='2', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_three = tk.Button(root, text='3', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    button_equal = tk.Button(root, text='=', font=font_18, width=5, height=3, bg='#b1b2b2', relief=tk.FLAT)
    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', font=font_18, width=12, bg='#EECD9F', relief=tk.FLAT)
    # button_zero1 = tk.Button(root,text='0',font=font_18,width=5,bg='#EECD9F',relief=tk.FLAT)
    button_dot = tk.Button(root, text='.', font=font_18, width=5, bg='#EECD9F', relief=tk.FLAT)
    # button_equal1 = tk.Button(root,text='=',font=font_18,width=5,bg='#b1b2b2',relief=tk.FLAT)
    button_zero.grid(row=6, column=1, padx=4, pady=2, columnspan=2)
    # button_zero1.grid(row=6,column=2,padx=4,pady=2)
    button_dot.grid(row=6, column=3, padx=4, pady=2)
    # button_equal1.grid(row=6,column=4,padx=4,pady=2)
    
    """点击事件"""
    
    
    def click_btn(x):
        # print('x:{}'.format(x))
        result_num.set(result_num.get() + x)
    
    
    def caculate():
        res_str = result_num.get()
        result = eval(res_str)
        result_num.set(result)
    
    
    def clear():
        result_num.set('')
    
    
    def back():
        res = result_num.get()
        if len(res):
            res = res[:-1]
        result_num.set(res)
    
    
    button_one.config(command=lambda: click_btn('1'))  # 匿名函数,传参数
    button_two.config(command=lambda: click_btn('2'))
    button_three.config(command=lambda: click_btn('3'))
    button_four.config(command=lambda: click_btn('4'))
    button_five.config(command=lambda: click_btn('5'))
    button_six.config(command=lambda: click_btn('6'))
    button_seven.config(command=lambda: click_btn('7'))
    button_eight.config(command=lambda: click_btn('8'))
    button_nine.config(command=lambda: click_btn('9'))
    button_zero.config(command=lambda: click_btn('0'))
    button_addition.config(command=lambda: click_btn('+'))
    button_substraction.config(command=lambda: click_btn('-'))
    button_mulitplation.config(command=lambda: click_btn('*'))
    button_divsion.config(command=lambda: click_btn('/'))
    button_dot.config(command=lambda: click_btn('.'))
    button_equal.config(command=caculate)
    button_clear.config(command=clear)
    button_back.config(command=back)
    
    root.mainloop()
  • 相关阅读:
    Java核心技术(初阶)知识点复习——[2]面向对象思想
    Java核心技术(初阶)知识点复习——[1]Java的类结构和main函数
    printStream与printWriter
    java反射的初步探索
    JDKJREJVM的关系
    树链剖分模板
    树状数组模板2
    树状数组模板1
    树状数组+欧拉降幂
    线段树模板二
  • 原文地址:https://www.cnblogs.com/yuexiao/p/16122980.html
Copyright © 2020-2023  润新知