Python Forum

Full Version: How to perform math function in different page of Tkinter GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from tkinter import *
from functools import partial
  
def raise_frame(frame):
    frame.tkraise()
  
win = Tk()
win.geometry("400x300+10+10")
win.title("Calculator")
 
third_frame = Frame(win)
third_frame.place(x=0, y=0, width=400, height=300) 

second_frame = Frame(win)
second_frame.place(x=0, y=0, width=400, height=300)
 
first_frame = Frame(win)
first_frame.place(x=0, y=0, width=400, height=300)
 
label_0 = Label(first_frame, text="CHOOSE NUMBER OF INPUT",width=30,font=("bold", 12))
label_0.place(x=60,y=50)
    
Button(first_frame, text='2 inputs',width=10,font=("bold", 10),bg='yellow',fg='black', command=lambda:raise_frame(second_frame)).place(x=155,y=150)


Button(second_frame, text="Back to Front Page",width=18,bg='brown',fg='white', command=lambda:raise_frame(first_frame)).place(x=20,y=260)           
              

label_1 = Label(second_frame, text="Digit  1",width=10,font=("bold", 10))
label_1.place(x=100, y=50)

entry_1 = Entry(second_frame)
entry_1.place(x=200,y=50)

label_2 = Label(second_frame, text="Digit 2",width=10,font=("bold", 10))
label_2.place(x=100,y=100)
#RT = get()(second_frame, entry1, entry2.get())
  
entry_2 = Entry(second_frame)
entry_2.place(x=200,y=100)  
  
label_3 = Label(second_frame, text="Result",font=("bold", 10))
label_3.place(x=100,y=200)

entry_3 = Entry(second_frame)
entry_3.place(x=200,y=200)
#RT = (entry_1,get() + entry_2,get())
#entry_3.second_frame, insert(END, str(RT))  
  


Button(second_frame, text='ADD',width=10,bg='blue',fg='black').place(x=100,y=150)  

#second_frame.entry1, R1 = int(get())

    #entry_2.delete(0,END)
    #R1=int(entry_1.get())
    #R2=int(entry_2.get())
    #RT= R1 + R2
    #entry_3.insert(END, str(RT))  
    
    
Button(second_frame, text='SUB',width=10,bg='blue',fg='black').place(x=200,y=150)              



win.mainloop()
I don't understand your question or how it relates to pages.

I think maybe your question is how you make your program add or subtract when you press the "ADD" or "SUB" button. To do that you need to bind a function to the button.

In this example I bound the "ADD" button to the function add_numbers(). add_numbers() gets the text from the two input entries, converts the text to float numbers, and sets the text of the output entry to the sum of the inputs.
from tkinter import *
   
def add_numbers():
    a = float(a_var.get())
    b = float(b_var.get())
    result_var.set(str(a+b))
    
root = Tk()

# Use tkinter variables to talk to entry widgets
a_var = StringVar()
b_var = StringVar()   
result_var = StringVar()

thing = Label(root, text='A')
thing.grid(row=0, column=0)

thing = Entry(root, textvar=a_var, justify=RIGHT)
thing.grid(row=0, column=1)
 
thing = Label(root, text='B')
thing.grid(row=1, column=0)

thing = Entry(root, textvar=b_var, justify=RIGHT)
thing.grid(row=1, column=1)
   
thing = Button(root, text='ADD', command=add_numbers)
thing.grid(row=2, column=1)

thing = Label(root, text='Sum')
thing.grid(row=3, column=0)

thing = Entry(root, textvar=result_var, justify=RIGHT, state="readonly")
thing.grid(row=3, column=1)

root.mainloop()
I changed your program not as an indictment of your programming, but to make a shorter example and demonstrate a couple of ideas.

Notice I reuse "thing" as the handle for widgets. I don't keep handles to widgets unless I plan on using the handle. In this case I will not be using the handle again after I place the widget in the window.

Instead of working with the widget directly I prefer using tkinter variables. The syntax for getting and setting the text in an entry is ugly and I prefer not having to deal with it. By setting "textvar=a_var" I can get and set the textvar value of the entry widget using a_var.get() and a_var.set(value).

Other changes you may find useful. I set the entries to be right justified. People are used to seeing numbers right justified, so your application should have right justified numbers too. I also made the result read only so you cannot type in the entry.
Another way to make a much more powerful calculator with much less code is using "eval". The code below has evaluates the equation and displays the result when the user types "=".
from tkinter import *
from math import *

root = Tk()

def evaluate(*args):
    try:
        equ = equation.get()
        if equ[-1] == '=':
            result.set(str(eval(equ[:-1])))
        else:
            result.set('')
    except:
        result.set('')
    
equation = StringVar()
equation.trace('w', evaluate)
result = StringVar()   
 
x = Label(root, text='Equation ')
x.grid(row=0, column=0)
 
x = Entry(root, textvar=equation, width=30)
x.grid(row=0, column=1)
  
x = Entry(root, textvar=result, width=8, justify=RIGHT, state="readonly")
x.grid(row=0, column=2)
 
root.mainloop()
To calculate the hypotenuse of a right triangle enter "(3**2 + 4**2)**0.5="