将命令行程序转换为GUI

时间:2012-11-10 05:51:01

标签: python user-interface tkinter

我为一个带有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

3 个答案:

答案 0 :(得分:1)

按照tkinter app的这个基本布局:

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()

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

答案 1 :(得分:0)

是的,在这种情况下,您需要重新编程整个计算器。你可以选择There are lots of GUI libraries。 TkInter,wxPython和PyQt是一些比较受欢迎的。

我建议在尝试构建像计算器这样复杂的东西之前在线查找基本教程并制作更小的GUI。

答案 2 :(得分:0)

只需要花一天的时间学习如何使用简单的GUI来执行上述代码中要完成的任务,并且值得而不是要求别人为您编写代码。我建议你自己做。一旦我花了我的时间,我就不知道构建GUI了,我现在可以通过Maya命令模块或我今天所知的wxPython来制作复杂的GUI,你可以去zetcode网站学习如何自己构建基于GUI的应用程序。 / p>

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