top of page

How To Insert The Result Of Two Numbers Added In Tkinter Into An Entry Widget With Tkinter

  • Writer: Brian Clark
    Brian Clark
  • Apr 14, 2022
  • 1 min read

I'm practicing with The Python Mega Course @Udemy.com, and the Exercise that I'm working with has me adding two numbers and displaying the result. I decided to enhance it to show the result in the label box of my choice.



''' 
Author: Brian Clark 
Website: https://brianclark88.wixsite.com/mysite
'''
from tkinter import *


def addNumbers():
    res = int(e1.get())+int(e2.get())
    myText.set(res)
    e3.insert(3, res) #simply add the insert() funciton in the primary         #function


master = Tk()
myText = StringVar()
Label(master, text="First").grid(row=0, sticky=W)
Label(master, text="Second").grid(row=1, sticky=W)
Label(master, text="Result:").grid(row=3, sticky=W)
result = Label(master, text="", textvariable=myText).grid(
    row=3, column=1, sticky=W)

e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=3, column=1)

b = Button(master, text="Calculate", command=addNumbers)
b.grid(row=0, column=2, columnspan=2, rowspan=2,
       sticky=W+E+N+S, padx=5, pady=5)


mainloop()

I wasn't going to stop until I figured this out because I knew it couldn't be too difficult. Your program should look like this when you run it on Visual Studio code or your favorite Integrated Development Environment(IDE).





References:


2. Arash H. (December 9th, 2017 )Stack Overflow. How to display an output using entry widget in Python? https://stackoverflow.com/questions/34793321/how-to-display-an-output-using-entry-widget-in-python




Comments


bottom of page