Multiple Entries in an Entry Box from a Button in Tkinter

932 views Asked by At

I have provided some code that uses callbacks to allow an Entry box to take a value from a button. The question is, how can I callback two concatenated values into the entry box. If the user clicks 1 and then 7, the value of 17 is shown.

from tkinter import *
win = Tk()
win.geometry("210x125")

def set_text(text):
    e.insert(0,text)
    return

e = Entry(win,width=35)
e.grid(columnspan=4)

seven_button = Button(win,width=6,text="7",command=lambda:set_text("7")).grid(row=1, column=0)
eight_button = Button(win,width=6,text="8",command=lambda:set_text("8")).grid(row=1, column=1)
nine_button = Button(win,width=6,text="9",command=lambda:set_text("9")).grid(row=1, column=2)
div_button = Button(win,width=6,text="÷",command=lambda:set_text("÷")).grid(row=1, column=3)

four_button = Button(win,width=6,text="4",command=lambda:set_text("4")).grid(row=2, column=0)
five_button = Button(win,width=6,text="5",command=lambda:set_text("5")).grid(row=2, column=1)
six_button = Button(win,width=6,text="6",command=lambda:set_text("6")).grid(row=2, column=2)
multiply_button = Button(win,width=6,text="x",command=lambda:set_text("x")).grid(row=2, column=3)

one_button = Button(win,width=6,text="1",command=lambda:set_text("1")).grid(row=3, column=0)
two_button = Button(win,width=6,text="2",command=lambda:set_text("2")).grid(row=3, column=1)
three_button = Button(win,width=6,text="3",command=lambda:set_text("3")).grid(row=3, column=2)
minus_button = Button(win,width=6,text="-",command=lambda:set_text("-")).grid(row=3, column=3)

zero_button = Button(win,width=14,text="0",command=lambda:set_text("0")).grid(columnspan=2)
point_button = Button(win,width=6,text=".",command=lambda:set_text(".")).grid(row=4, column=2)
plus_button = Button(win,width=6,text="+",command=lambda:set_text("+")).grid(row=4, column=3)

win.mainloop()
2

There are 2 answers

1
jwillis0720 On BEST ANSWER

if you want full functionality, might i suggest adding an equals button that will compute everything in the entry widget?

import Tkinter as tk
win = tk.Tk()
win.geometry("300x300")


def compute(entry):
 string_to_compute = tk.StringVar()
 string_to_compute = entry.get()
 entry.delete(0, tk.END)
 if '+' in string_to_compute:
    l = string_to_compute.split('+')[0]
    r = string_to_compute.split('+')[1]
    entry.insert(0, float(l) + float(r))
 elif '-' in string_to_compute:
    l = string_to_compute.split('-')[0]
    r = string_to_compute.split('-')[1]
    entry.insert(0, float(l) - float(r))
 elif 'x' in string_to_compute:
    l = string_to_compute.split('x')[0]
    r = string_to_compute.split('x')[1]
    entry.insert(0, float(l) * float(r))
 elif '/' in string_to_compute:
    l = string_to_compute.split('/')[0]
    r = string_to_compute.split('/')[1]
    entry.insert(0, float(l) / float(r))


def set_text(text):
 e.insert(tk.END, text)
 return

e = tk.Entry(win, width=35)
e.grid(columnspan=4)

seven_button = tk.Button(win, width=6, text="7", command=lambda: set_text("7")).grid(row=1, column=0)
eight_button = tk.Button(win, width=6, text="8", command=lambda: set_text("8")).grid(row=1, column=1)
nine_button = tk.Button(win, width=6, text="9", command=lambda: set_text("9")).grid(row=1, column=2)
div_button = tk.Button(win, width=6, text="/", command=lambda: set_text("/")).grid(row=1, column=3)

four_button = tk.Button(win, width=6, text="4", command=lambda: set_text("4")).grid(row=2, column=0)
five_button = tk.Button(win, width=6, text="5", command=lambda: set_text("5")).grid(row=2, column=1)
six_button = tk.Button(win, width=6, text="6", command=lambda: set_text("6")).grid(row=2, column=2)
multiply_button = tk.Button(win, width=6, text="x", command=lambda: set_text("x")).grid(row=2, column=3)

one_button = tk.Button(win, width=6, text="1", command=lambda: set_text("1")).grid(row=3, column=0)
two_button = tk.Button(win, width=6, text="2", command=lambda: set_text("2")).grid(row=3, column=1)
three_button = tk.Button(win, width=6, text="3", command=lambda: set_text("3")).grid(row=3, column=2)
minus_button = tk.Button(win, width=6, text="-", command=lambda: set_text("-")).grid(row=3, column=3)

zero_button = tk.Button(win, width=14, text="0", command=lambda: set_text("0")).grid(columnspan=2)
point_button = tk.Button(win, width=6, text=".", command=lambda: set_text(".")).grid(row=4, column=2)
plus_button = tk.Button(win, width=6, text="+", command=lambda: set_text("+")).grid(row=4, column=3)
equal_button = tk.Button(win, width=6, text="=", command=lambda: compute(e)).grid(row=5, column=1)


win.mainloop()

Note i changed the division sign and some of the logic of the entry so it would enter the left to right.

0
Alok On

Just insert the text at the end.

def set_text(text):
    e.insert(END,text)
    return