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


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,045 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,844 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,338 Feb-15-2022, 04:40 AM
Last Post: pymn
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,851 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,531 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,011 Oct-01-2021, 05:00 PM
Last Post: Yoriz
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,114 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  tkinter get function finndude 2 2,950 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,414 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,116 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