【闲鱼接单】讲解代码 python的 界面程序

讲解代码

"""
说明:
The following are the libraries and their use:
    1. tkinter :- 创造用户界面
    2. math :- 基本运算
    3. parser :-
    4. tkinter.messagebox :- 特别的消息盒子,可以改变信息内容
"""

from tkinter import *
import math
import parser
import tkinter.messagebox


class Calc:
    def __init__(self):
        self.total = 0
        self.current = ""
        self.input_value = True
        self.check_sum = False
        self.op = ""
        self.result = False

    def numberEnter(self, num):
        self.result = False
        firstnum = txtDisplay.get()
        secondnum = str(num)
        if self.input_value:
            self.current = secondnum
            self.input_value = False
        else:
            if secondnum == ".":
                if secondnum in firstnum:
                    return
            self.current = firstnum + secondnum
        self.display(self.current)

    def sum_of_total(self):
        self.result = True
        self.current = float(self.current)
        if self.check_sum == True:
            self.valid_function()
        else:
            self.total = float(txtDisplay.get())

    def display(self, value):
        txtDisplay.delete(0, END)
        txtDisplay.insert(0, value)

    def valid_function(self):
        if self.op == "add":
            self.total += self.current
        if self.op == "sub":
            self.total -= self.current
        if self.op == "multi":
            self.total *= self.current
        if self.op == "divide":
            self.total /= self.current
        if self.op == "mod":
            self.total %= self.current
        self.input_value = True
        self.check_sum = False
        self.display(self.total)

    def operation(self, op):
        self.current = float(self.current)
        if self.check_sum:
            self.valid_function()
        elif not self.result:
            self.total = self.current
            self.input_value = True
        self.check_sum = True
        self.op = op
        self.result = False

    def Clear_Entry(self):
        self.result = False
        self.current = "0"
        self.display(0)
        self.input_value = True

    def all_Clear_Entry(self):
        self.Clear_Entry()
        self.total = 0

    def mathsPM(self):
        self.result = False
        self.current = -(float(txtDisplay.get()))
        self.display(self.current)

    def squared(self):
        self.result = False
        self.current = math.sqrt(float(txtDisplay.get()))
        self.display(self.current)

    def exp(self):
        self.result = False
        self.current = math.exp(float(txtDisplay.get()))
        self.display(self.current)

    def pi(self):
        self.result = False
        self.current = math.pi
        self.display(self.current)

    def tau(self):
        self.result = False
        self.current = math.tau
        self.display(self.current)

    def e(self):
        self.result = False
        self.current = math.e
        self.display(self.current)

    def cos(self):
        self.result = False
        self.current = math.cos(math.radians(float(txtDisplay.get())))
        self.display(self.current)

    def sin(self):
        self.result = False
        self.current = math.sin(math.radians(float(txtDisplay.get())))
        self.display(self.current)

    def tan(self):
        self.result = False
        self.current = math.tan(math.radians(float(txtDisplay.get())))
        self.display(self.current)

    def asinh(self):
        self.result = False
        self.current = math.asinh(math.radians(float(txtDisplay.get())))
        self.display(self.current)

    def acosh(self):
        self.result = False
        self.current = math.acosh(math.radians(float(txtDisplay.get())))
        self.display(self.current)

    def atanh(self):
        self.result = False
        self.current = math.atanh(math.radians(float(txtDisplay.get())))
        self.display(self.current)

    def log(self):
        self.result = False
        self.current = math.log(float(txtDisplay.get()))
        self.display(self.current)

    def exmp1(self):
        self.result = False
        self.current = math.expm1(float(txtDisplay.get()))
        self.display(self.current)

    def lgamma(self):
        self.result = False
        self.current = math.lgamma(float(txtDisplay.get()))
        self.display(self.current)

    def degrees(self):
        self.result = False
        self.current = math.degrees(float(txtDisplay.get()))
        self.display(self.current)

    def log2(self):
        self.result = False
        self.current = math.log2(float(txtDisplay.get()))
        self.display(self.current)

    def log10(self):
        self.result = False
        self.current = math.log10(float(txtDisplay.get()))
        self.display(self.current)

    def log1p(self):
        self.result = False
        self.current = math.log1p(float(txtDisplay.get()))
        self.display(self.current)

    def sinh(self):
        self.result = False
        self.current = math.sinh(float(txtDisplay.get()))
        self.display(self.current)

    def cosh(self):
        self.result = False
        self.current = math.cosh(float(txtDisplay.get()))
        self.display(self.current)

    def tanh(self):
        self.result = False
        self.current = math.log1p(float(txtDisplay.get()))
        self.display(self.current)


# We will be creating a simple window using tkinter object 'Tk()' function.
root = Tk()
root.title("Scientific Calculator coding by geanyeong")  # Giving title to our window.
root.configure(background="grey")  # Defining the background color for our window.
root.resizable(width=False, height=False)
root.geometry("480x568+0+0")

calc = Frame(root)
calc.grid()

added_value = Calc()

txtDisplay = Entry(calc, font=("arial", 20, "bold"), bg="grey", bd=30, width=29, justify=RIGHT)
txtDisplay.grid(columnspan=4, pady=1)
txtDisplay.insert(0, "0")

numberpad = "789456123"  # 创建键盘
i = 0
btn = []
for j in range(2, 5):
    for k in range(3):
        btn.append(Button(calc, width=6, height=2, font=("arial", 20, "bold"),
                          bd=4, text=numberpad[i]))
        btn[i].grid(row=j, column=k, pady=1)
        btn[i]["command"] = lambda x=numberpad[i]: added_value.numberEnter(x)
        i += 1
# ===============================================Standard Buttons========================================================
btnClear = Button(calc, text=chr(67), width=6, height=2, font=("arial", 20, "bold"),
                  bd=4, bg="grey", command=added_value.Clear_Entry).grid(row=1, column=0, pady=1)

btnAllClear = Button(calc, text=chr(67) + chr(69), width=6, height=2, font=("arial", 20, "bold"),
                     bd=4, bg="grey", command=added_value.all_Clear_Entry) \
    .grid(row=1, column=1, pady=1)

btnsq = Button(calc, text="√", width=6, height=2, font=("arial", 20, "bold"),
               bd=4, bg="grey", command=added_value.squared).grid(row=1, column=2, pady=1)

btnadd = Button(calc, text="+", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=lambda: added_value.operation("add")).grid(row=1, column=3, pady=1)

btnsub = Button(calc, text="-", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=lambda: added_value.operation("sub")).grid(row=2, column=3, pady=1)

btnmul = Button(calc, text="*", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=lambda: added_value.operation("multi")).grid(row=3, column=3, pady=1)

btndiv = Button(calc, text=chr(247), width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=lambda: added_value.operation("divide")).grid(row=4, column=3, pady=1)

btnDot = Button(calc, text=".", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=lambda: added_value.numberEnter(".")).grid(row=5, column=0, pady=1)

btn0 = Button(calc, text="0", width=6, height=2, font=("arial", 20, "bold"),
              bd=4, command=lambda: added_value.numberEnter(0)).grid(row=5, column=1, pady=1)

btnsign = Button(calc, text=chr(177), width=6, height=2, font=("arial", 20, "bold"),
                 bd=4, bg="grey", command=added_value.mathsPM).grid(row=5, column=2, pady=1)

btneq = Button(calc, text="=", width=6, height=2, font=("arial", 20, "bold"),
               bd=4, bg="grey", command=added_value.sum_of_total).grid(row=5, column=3, pady=1)
# ===================================Scientific Buttons==================================================================

btnPi = Button(calc, text="π", width=6, height=2, font=("arial", 20, "bold"),
               bd=4, bg="grey", command=added_value.pi).grid(row=1, column=4, pady=1)

btnsin = Button(calc, text="sin", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=added_value.sin).grid(row=1, column=5, pady=1)

btncos = Button(calc, text="cos", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=added_value.cos).grid(row=1, column=6, pady=1)

btntan = Button(calc, text="tan", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=added_value.tan).grid(row=1, column=7, pady=1)

# ---------------------------------------------------------------------------------------------------------------=======

btn2pi = Button(calc, text="2π", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=added_value.tau).grid(row=2, column=4, pady=1)

btnsinh = Button(calc, text="sinh", width=6, height=2, font=("arial", 20, "bold"),
                 bd=4, bg="white", command=added_value.sinh).grid(row=2, column=5, pady=1)

btncosh = Button(calc, text="cosh", width=6, height=2, font=("arial", 20, "bold"),
                 bd=4, bg="white", command=added_value.cosh).grid(row=2, column=6, pady=1)

btntanh = Button(calc, text="tanh", width=6, height=2, font=("arial", 20, "bold"),
                 bd=4, bg="white", command=added_value.tanh).grid(row=2, column=7, pady=1)
# -----------------------------------------------------------------------------------------------------------------------
btnlog = Button(calc, text="log", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="grey", command=added_value.log).grid(row=3, column=4, pady=1)

btnexp = Button(calc, text="Exp", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="white", command=added_value.exp).grid(row=3, column=5, pady=1)

btnmod = Button(calc, text="Mod", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="white", command=lambda: added_value.operation("mod")).grid(row=3, column=6, pady=1)

btne = Button(calc, text="e", width=6, height=2, font=("arial", 20, "bold"),
              bd=4, bg="white", command=added_value.e).grid(row=3, column=7, pady=1)
# -----------------------------------------------------------------------------------------------------------------------

btnlog2 = Button(calc, text="log2", width=6, height=2, font=("arial", 20, "bold"),
                 bd=4, bg="grey", command=added_value.log2).grid(row=4, column=4, pady=1)

btndeg = Button(calc, text="deg", width=6, height=2, font=("arial", 20, "bold"),
                bd=4, bg="white", command=added_value.degrees).grid(row=4, column=5, pady=1)

btnasinh = Button(calc, text="asinh", width=6, height=2, font=("arial", 20, "bold"),
                  bd=4, bg="white", command=added_value.asinh).grid(row=4, column=6, pady=1)

btnacosh = Button(calc, text="acosh", width=6, height=2, font=("arial", 20, "bold"),
                  bd=4, bg="white", command=added_value.acosh).grid(row=4, column=7, pady=1)
# -----------------------------------------------------------------------------------------------------------------------

btnlog10 = Button(calc, text="log10", width=6, height=2, font=("arial", 20, "bold"),
                  bd=4, bg="grey", command=added_value.log10).grid(row=5, column=4, pady=1)

btnlog1p = Button(calc, text="log1p", width=6, height=2, font=("arial", 20, "bold"),
                  bd=4, bg="grey", command=added_value.log1p).grid(row=5, column=5, pady=1)

btnaexmpl = Button(calc, text="exmpl", width=6, height=2, font=("arial", 20, "bold"),
                   bd=4, bg="grey", command=added_value.exmp1).grid(row=5, column=6, pady=1)

btnlgamma = Button(calc, text="lgamma", width=6, height=2, font=("arial", 20, "bold"),
                   bd=4, bg="grey", command=added_value.lgamma).grid(row=5, column=7, pady=1)


# =====================================MENU  and functions===============================================================

def iExit():
    iExit = tkinter.messagebox.askyesno("Scientific Calculator", "Confirm if you want to exit.")  # 询问选择对话窗
    if iExit > 0:
        root.destroy()
        return


def Scientific():
    root.resizable(width=False, height=False)
    root.geometry("944x568+0+0")   # 切换 Scientific


def Standard():
    root.resizable(width=False, height=False)
    root.geometry("480x568+0+0")  # 切换 Standard


menubar = Menu(calc)  # 创建一个菜单栏,这里我们可以把他理解成一个容器,在窗口的上方

filemenu = Menu(menubar, tearoff=0)  # 定义一个空菜单单元
menubar.add_cascade(label="File", menu=filemenu)  # 将上面定义的空菜单命名为`File`,放在菜单栏中,就是装入那个容器中
filemenu.add_command(label="Standard", command=Standard)   # 在`File`中加入`Standard`小菜单
filemenu.add_command(label="Scientific", command=Scientific)   # 在`File`中加入`Scientific`小菜单
filemenu.add_separator()  # 这里就是一条分割线
filemenu.add_command(label="Exit", command=iExit)    # 在`File`中加入`Exit`小菜单

editmenu = Menu(menubar, tearoff=0)  # 定义一个空菜单单元
menubar.add_cascade(label="Edit", menu=editmenu)  # 将上面定义的空菜单命名为`Edit`,放在菜单栏中
editmenu.add_command(label="Cut")   # 在`Edit`中加入`Cut`小菜单
editmenu.add_command(label="Copy")   # 在`Edit`中加入`Copy`小菜单
editmenu.add_separator()    # 这里就是一条分割线
editmenu.add_command(label="Paste")   # 在`Edit`中加入`Paste`小菜单

helpmenu = Menu(menubar, tearoff=0)  # 定义一个空菜单单元
menubar.add_cascade(label="Help", menu=helpmenu)   # 将上面定义的空菜单命名为`Help`,放在菜单栏中
helpmenu.add_command(label="View Help")   # 在`Help`中加入`View Help`小菜单
helpmenu.add_separator()  # 这里就是一条分割线
# ================================================================================================================================================================

root.config(menu=menubar)
root.mainloop()

你可能感兴趣的:(计算机网络)