Python Forum
[Tkinter] How to perform math function in different page of Tkinter GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to perform math function in different page of Tkinter GUI
#1
Star 
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()
Larz60+ write Oct-23-2020, 03:09 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time.
Reply
#2
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.
Reply
#3
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="
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 302 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 506 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 528 Oct-01-2021, 05:00 PM
Last Post: Yoriz
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 1,157 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  tkinter get function finndude 2 944 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 1,787 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  Using Tkinter inside function not working Ensaimadeta 3 1,265 Dec-23-2020, 02:57 PM
Last Post: deanhystad
  function in new window (tkinter) Dale22 7 1,352 Nov-24-2020, 11:28 PM
Last Post: Dale22
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 1,711 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  tkinter get method is not accepting value when called by function jagasrik 1 975 Sep-16-2020, 05:28 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020