python计算器实现 Yeah

Debug了一整天 总算是出了个小东西,虽然现在来说还有点小问题 但是毕竟成功了。后面的再慢慢完善吧。其实小问题就是,现在这个程序运行一次之后就挂了。我要想办法让它一直活着

import math
from tkinter import *
class GUI:
    def __init__(self):
        #the self function 
        window=Tk()
        window.title("Calculator made by HustWolf")
        menubar=Menu(window)
        window.config(menu=menubar)
        operationMenu=Menu(menubar,tearoff=0)
        menubar.add_cascade(label="Operation",menu=operationMenu)
        operationMenu.add_command(label="operation1",command=self.operation1)
        operationMenu.add_command(label="operation2",command=self.operation2)
        operationMenu.add_command(label="operation3",command=self.operation3)

        exitmenu=Menu(menubar,tearoff=0)
        menubar.add_cascade(label="Exit",menu=exitmenu)
        exitmenu.add_command(label="exit",command=window.quit)

        frame0=Frame(window)
        frame0.grid(row=1,column=1,sticky=W)
        # image1=PhotoImage(file="image/1.jpg")
        # image2=PhotoImage(file="image/2.jpg")
        # image3=PhotoImage(file="image/3.jpg")


        # Button(frame0,image=image1,command=self.operation1).grid(row=1,column=1,sticky=W)
        # Button(frame0,image=image2,command=self.operation2).grid(row=1,column=2)
        # Button(frame0,image=image3,command=self.operation3).grid(row=1,column=3)

        Button(frame0,text="operation1",bg="red",command=self.operation1).grid(row=1,column=1,sticky=W)
        Button(frame0,text="operation2",bg="green",command=self.operation2).grid(row=1,column=2)
        Button(frame0,text="operation3",bg="blue",command=self.operation3).grid(row=1,column=3)

        frame1=Frame(window)
        frame1.grid(row=2,column=1,pady=50)
        Label(frame1,text="number1:").pack(side=LEFT)
        self.v1=StringVar()
        Entry(frame1,width=5,textvariable=self.v1,justify=RIGHT).pack(side=LEFT)
        Label(frame1,text="operation").pack(side=LEFT)
        self.v2=StringVar()
        Entry(frame1,width=5,textvariable=self.v2,justify=RIGHT).pack(side=LEFT)

        Label(frame1,text="number2:").pack(side=LEFT)
        self.v3=StringVar()
        Entry(frame1,width=5,textvariable=self.v3,justify=RIGHT).pack(side=LEFT)
        
        # Label(frame1,text="Result:").pack(side=LEFT)
        self.v4=StringVar()
        Label(frame1,text="Result: ").pack(side=LEFT)
        result=Label(frame1,width=5,textvariable=self.v4,justify=RIGHT).pack(side=LEFT)
        

         # frame2=Frame(window)
         # frame2.grid(row=3,column=1,pady=50)
         # Button(frame2,text="calculate",command=self.operation1)

        window.mainloop()


# the main part of the calculator
    # def calculator(self,a,b,operate,x):
    #   judge=x
    #   if judge==1:
    #       calculate(a,operate,b)
    #   elif judge==2:
    #       calculate2(a,operate,b)
    #   elif judge==3:
    #       calculate3(a,operate,b)

    # def calculate(self,a,operate,b):
    #   a=float(a)
    #   b=float(b)
    #   operate=str(operate)
    #   res=0.00
    #   if operate=='+':
    #       res=a+b
    #
    #   elif operate=='-':
    #       res=a-b
    #
    #   elif operate=='*':
    #       res=a*b
    #
    #   elif operate=='/':
    #       res=a/b
    #
    #   elif operate=='%':
    #       res=a%b
    #
    # def calculate2(self,a,operate,b):
    #   a=str(a)
    #   b=float(b)
    #   operate=str(operate)
    #   if operate=='sin':
    #       return math.sin(b)
    #   elif operate=='cos':
    #       return math.cos(b)
    #   elif operate=='tan':
    #       return math.tan(b)
    #   elif operate=='arcsin':
    #       return math.asin(b)
    #   elif operate=='arccos':
    #       return math.acos(b)
    #   elif operate=='arctan':
    #       return math.atan(b)
    #   elif operate=='sinh':
    #       return math.sinh(b)
    #   elif operate=='asinh':
    #       return math.asinh(b)
    #   elif operate=='cosh':
    #       return math.cosh(b)
    #   elif operate=='acosh':
    #       return math.acosh(b)
    #   elif operate=='tanh':
    #       return math.tanh(b)
    #   elif operate=='atanh':
    #       return math.atanh(b)
    #   elif operate=='ln':
    #       return math.log(b)
    #   elif operate=='log':
    #       return math.log10(b)
    #   elif operate=='sqrt':
    #       return math.sqrt(b)

    # def calculate3(self,a,operate,b):
    #   a=float(a)
    #   b=float(b)
    #   operate=str(operate)
    #   if operate=='pow':
    #       res=math.pow(a,b)
    #   elif operate=='log':
    #       res=math.log(b,a)



#operations defintions

    def operation1(self):
        judge=1
        a=float(self.v1.get())
        b=float(self.v3.get())
        operate=self.v2.get()
        res=0.00
        if operate=='+':
            res=a+b

        elif operate=='-':
            res=a-b

        elif operate=='*':
            res=a*b

        elif operate=='/':
            res=a/b

        elif operate=='%':
            res=a%b

        self.v4.set(str(res))

    def operation2(self):
        judge=2
        a=float(self.v1.get())
        a=1
        b=float(self.v3.get())
        operate=self.v2.get()
        res=0.00
        if operate=='sin':
            res=math.sin(a*b)

        elif operate=='cos':
            res=math.cos(a*b)

        elif operate=='tan':
            res=math.tan(a*b)

        elif operate=='arcsin':
            res=math.asin(a*b)

        elif operate=='arccos':
            res=math.acos(a*b)

        elif operate=='arctan':
            res=math.atan(a*b)

        elif operate=='sinh':
            res=math.sinh(a*b)

        elif operate=='asinh':
            res=math.asinh(a*b)

        elif operate=='cosh':
            res=math.cosh(a*b)

        elif operate=='acosh':
            res=math.acosh(a*b)

        elif operate=='tanh':
            res=math.tanh(a*b)

        elif operate=='atanh':
            res=math.atanh(a*b)

        elif operate=='ln':
            res=math.log(a*b)

        elif operate=='log':
            res=math.log10(a*b)

        elif operate=='sqrt':
            res=math.sqrt(a*b)

        self.v4.set(str(res))

    def operation3(self):
        judge=3
        a=float(self.v1.get())
        b=float(self.v3.get())
        operate=self.v2.get()
        res=0.00
        if operate=='pow':
            res=math.pow(a,b)
        elif operate=='log':
            res=math.log(b,a)
        self.v4.set(str(res))



#Run the program

GUI()


不容易啊 不容易,我在做机械原理的实验的时候都没有认真的玩那个六自由度机器人反而是在琢磨着Debug 立地为猿真是一个艰难的决定啊

你可能感兴趣的:(python计算器实现 Yeah)