How to display the result of a formula in a tkinter Entry textbox

Question:

I`’m building a GUI for an engineering software that I’ve created and, being a beginner on Python, I’m having trouble with the code. I’ll try to simplify my problem and describe what I want to do:

Using Python and Tkinter, I want to create a simple window with 3 Entry widgets. The first two will be my inputs. The third one, the output, should display the sum of whatever numbers are inserted in the first two. The output value should refresh as soon as I insert any values in the inputs and should not be changeable in any other way (disabled or read-only, I guess). I intend to use the output value in other calculation forward in the project.

Thanks in advance for helping!

This is the initial look of the window. Tried a lot of different methods, but none of then worked so far.

import tkinter as tk

window = tk.Tk()
window.title("Simple window")

window_frame = tk.LabelFrame(window)
window_frame.pack()

input1_label= tk.Label(window_frame, text="input 1")
input1_label.pack()
input1_entry= tk.Entry(window_frame)
input1_entry.pack()

input2_label= tk.Label(window_frame, text="input 2")
input2_label.pack()
input2_entry= tk.Entry(window_frame)
input2_entry.pack()

output_label= tk.Label(window_frame, text="output")
output_label.pack()
output_entry= tk.Entry(window_frame)
output_entry.pack()


window.mainloop()
Asked By: JohnEngineer

||

Answers:

Here’s an example of how to do it with a button. Note that I changed the output to a Label to make it read-only:

import tkinter as tk

window = tk.Tk()
window.title("Simple window")

window_frame = tk.LabelFrame(window)
window_frame.pack()

input1_label= tk.Label(window_frame, text="input 1")
input1_label.pack()
input1_entry= tk.Entry(window_frame)
input1_entry.pack()

input2_label= tk.Label(window_frame, text="input 2")
input2_label.pack()
input2_entry= tk.Entry(window_frame)
input2_entry.pack()

output_label= tk.Label(window_frame, text="output")
output_label.pack()
output_entry= tk.Label(window_frame)
output_entry.pack()

def compute():
    x1 = input1_entry.get()
    x2 = input2_entry.get()
    sumx = int(x1)+int(x2)
    output_entry.configure(text=str(sumx))

go = tk.Button(window_frame, text="Go!", command=compute)
go.pack()

window.mainloop()
Answered By: Tim Roberts

Here’s an example of how to do it without a button (with apologies to @TimRoberts).

Pro Tip: don’t do this.

import tkinter as tk


def compute():
    x1 = input1_entry.get()
    x2 = input2_entry.get()
    sumx = int(x1)+int(x2)
    output_entry.configure(text=str(sumx))


window = tk.Tk()
window.title("Simple window")

window_frame = tk.LabelFrame(window)
window_frame.pack()

input1_label= tk.Label(window_frame, text="input 1")
input1_label.pack()
input1_entry= tk.Entry(window_frame)
input1_entry.pack()

input2_label= tk.Label(window_frame, text="input 2")
input2_label.pack()
input2_entry= tk.Entry(window_frame)
input2_entry.pack()

# bind this event to the Entry so 'compute' is called when the user exits the field
# (I used a lambda here so the unused 'event' value is simply discarded since
# 'compute' doesn't need it)
input1_entry.bind('<FocusOut>', lambda _event: compute())
input2_entry.bind('<FocusOut>', lambda _event: compute())

output_label= tk.Label(window_frame, text="output")
output_label.pack()
output_entry= tk.Label(window_frame)
output_entry.pack()


window.mainloop()
Answered By: JRiggles
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.