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
#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


Messages In This Thread
RE: How to perform math function in different page of Tkinter GUI - by deanhystad - Oct-23-2020, 02:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,147 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,974 May-03-2022, 02:33 PM
Last Post: deanhystad
  how to add two numbers and pass the result to the next page in tkinter? pymn 7 4,396 Feb-15-2022, 04:40 AM
Last Post: pymn
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,934 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,600 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,076 Oct-01-2021, 05:00 PM
Last Post: Yoriz
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,191 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  tkinter get function finndude 2 3,000 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,597 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,268 Nov-24-2020, 11:28 PM
Last Post: Dale22

Forum Jump:

User Panel Messages

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