Converting a command line program to a GUI
Question: I created a python script for a simple call with 19 functions. I'm having
problems as it won't work in GUI mode. Do I have to recode the whole thing? I
don't know much about GUI. Could someone help me here please. I've tried
reading about GUI I'm just lost with the steps.

    
    
    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
    


Answer: Follow this basic layout for 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()
    

You can use entry widget to get number entries

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

The most important thing here will be to create a button for each function in
the **init** function for example....

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

then bind the clicking of the mouse button to a function of say 'add' function
add is:

    
    
    add(self,event):
    
    #your code to add    
    

to bind b1 use this syntax:

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

Bind all buttons to respective functions and you can display the result using
message widget.

