0

我为一个包含 19 个函数的简单调用创建了一个 python 脚本。我遇到了问题,因为它无法在 GUI 模式下工作。我必须重新编码整个事情吗?我对GUI不太了解。有人可以在这里帮助我吗?我试过阅读关于 GUI 我只是迷失了这些步骤。

import math
q=1
while q == True:
    print("Please select one option from the menu below: ")
    print("\t 0: Expression Input")
    print("\t 1: Numbers Input")
    print("\t 2: Exit \n")
    op = int(input("Please enter (0 or 1 or 2): "))
    def expression_input(str1):
        num3 = eval(str1)
        return num3

    if op == 0:
        expression = input("Please input the expression for calculation: ")
        num3 = expression_input(expression)
        print(num3)
        q = int(input("Press 1 to continue or 0 to exit:"))
        exit

    elif op == 1:
        s = 1
        while s == True:
            opr = float(input("Please select a method for calculation from 1 to 19 for the required function:\n\n\t1:+\n\t2:-\n\t3:*\n\t4:/\n\t5:power\n\t6:root\n\t7:sin\n\t8:cos\n\t9:tan\n\t10:arccos\n\t11:arcsin\n\t12:arctan\n\t13:log \n\t14:ln \n\t15:factorial\n\t16:hex\n\t17:octal\n\t18:decimal\n\t19:binary\n\t"))
            if opr > 19:
                print("Wrong choice, please reenter the option")
                exit
            elif opr <=4:
                p=1
                numb1=input("Enter the first value for calculation")
                numb2=input("Enter the second value for calculation")
                while p==True:

                    if numb1.isdigit():

                        if numb2.isdigit():
                            numb1=float(numb1)
                            numb2=float(numb2)
                            if opr == 1:
                                result=numb1+numb2
                                print (result)
                            if opr == 2:
                                result=numb1-numb2
                                print (result)
                            if opr == 3:
                                result=numb1*numb2
                                print (result)
                            if opr == 4:
                                result=numb1/numb2
                                print (result)
                            p=0
                            s=0
                            q = int(input("Press 1 to continue or 0 to exit:"))
                            exit
                        else:
                            numb2=input("Sorry, the second value is not a number,please re enter the second value")
                    else:
                        numb1=input("Sorry,the first value is not a number. Please Re enter the first value")

            else :
                t=1
                numb1=input("Enter the first value for calculation")
                while t== True:

                    if numb1.isdigit():
                        numb1=float(numb1)                   
                        if opr == 5:
                            numb2=int(input("Enter the power value for calculation"))
                            result = math.pow(numb1,numb2)
                            print (result)
                        if opr == 6:      
                            result = math.sqrt(numb1)
                            print (result)
                        if opr == 7:      
                            result = math.sin(numb1)
                            print (result)
                        if opr == 8:      
                            result = math.cos(numb1)
                            print (result)
                        if opr == 9:
                            result = math.tan(numb1)
                            print (result)
                        if opr == 10:      
                            result = math.acos(numb1)
                            print (result)
                        if opr == 11:      
                            result = math.asin(numb1)
                            print (result)
                        if opr == 12:      
                            result = math.atan(numb1)
                            print (result)
                        if opr == 13:
                            numb2=float(input("Enter the base value for calculation"))
                            result = math.log(numb1,[numb2])
                            print (result)
                        if opr == 14:      
                            result = math.log1p(numb1)
                            print (result)
                        if opr == 15:      
                            result = math.factorial(numb1)
                            print (result)
                        if opr == 16:
                            numb1=int(numb1)
                            result = hex(numb1)
                            print (result)
                        if opr == 17:
                            numb1=int(numb1)
                            result = oct(numb1)
                            print (result)
                        if opr == 18:
                            numb1=int(numb1)
                            result = float(numb1)
                            print (result)
                        if opr == 19:
                            numb1=int(numb1)
                            result = bin(numb1)
                            print (result)

                        q = int(input("Press 1 to continue or 0 to exit:"))
                        t=0
                        p=0
                        s=0
                        exit

                    else:
                        numb1=input("Sorry, it is not a number. Please re enter")


    elif op==2 :
        q=0
        print ("Thank you for using the program. Bye!")
        exit
    else:


        print("Wrong option, Please re enter again")
        s=0
        exit
4

3 回答 3

2

遵循 tkinter 应用程序的基本布局:

from Tkinter import *

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi there, everyone!"

root = Tk()

app = App(root)

root.mainloop()

您可以使用条目小部件获取数字条目

v = StringVar()
e = Entry(master, textvariable=v)
e.pack()

这里最重要的是为init函数中的每个函数创建一个按钮,例如......

b1=Button(self,text='+')

然后将鼠标按钮的单击绑定到说“添加”功能的功能添加是:

add(self,event):

#your code to add    

绑定 b1 使用以下语法:

b1.bind("<Button-1>",add)
b1.pack()

将所有按钮绑定到各自的功能,您可以使用消息小部件显示结果。

于 2012-11-10T15:08:40.530 回答
0

Yes, in this case you will need to reprogram the entire calculator. There are lots of GUI libraries you can pick from. TkInter, wxPython and PyQt are some of the more popular ones.

I suggest finding basic tutorials online and making smaller GUIs before trying to build something more complex like a calculator.

于 2012-11-10T05:56:03.067 回答
0

你只需要一天的时间来学习制作简单的 GUI 来执行你想要在上面的代码中完成的事情,它值得而不是让别人为你编码。我建议自己做。一旦我花时间,我就没有构建 GUI 的知识,现在我可以通过 Maya 命令模块或我今天知道的 wxPython 来制作复杂的 GUI,你可以去 zetcode 网站学习如何自己构建基于 GUI 的应用程序。

另外,花一些时间使用类,然后通过创建两个类(上面的一个你已经在工作)将 GUI 的代码与逻辑分开......

于 2012-11-10T05:58:27.693 回答