Search code examples
pythonpython-3.xtkintertkinter-entry

Python3 Tkinter - Button to increase entry box value by float such as 1.0 then update entry box display


I am trying to create a few buttons that appends a float value to the current value in a tkinter entry box.

from tkinter import *
import tkinter.ttk

menu = Tk()
menu.geometry('800x480')
frame1 = Frame(menu) 
frame1.grid()

#entry box
e1 = Entry(frame1)
e1.insert(0, float("1.0")) 
e1.grid(row=2, pady=5, ipadx=5, ipady=5)

#buttons
bt1 = Button(frame1, text="Add 1.0")
bt1.grid() 
bt2 = Button(frame1, text="Add 0.1")
bt2.grid()
bt3 = Button(frame1, text="Add 0.01")
bt3.grid()

For example, suppose the current value in e1 is 1.0. I would like to create a button that adds 1.0 to the current value (turning it to 2.0 and display that instead of 1.0). I would also like to make buttons that add the value 0.1, and 0.01. What is the best approach to do this? Currently I am looking at using counter but I am not quite sure how to implement it.

To visualize what I mean in case I am not explaining it well basically I have the current setup before the button is clicked. Suppose I click add 1.0 I want the 1.0 to turn into 2.0.


Solution

  • You can use command= to assign function to button. This function have to get text from Entry, convert to float, add 0.1, (delete all text in Entry), insert new value into Entry,

    import tkinter as tk
    
    def add_1_0():
        value = float(e1.get())
        value += 1.0
        e1.delete(0, 'end')
        e1.insert(0, value)
    
    def add_0_1():
        value = float(e1.get())
        value += 0.1
        e1.delete(0, 'end')
        e1.insert(0, value)
    
    def add_0_01():
        value = float(e1.get())
        value += 0.01
        e1.delete(0, 'end')
        e1.insert(0, value)
    
    root = tk.Tk()
    
    #entry box
    e1 = tk.Entry(root)
    e1.insert(0, 1.0) 
    e1.pack()
    
    #buttons
    bt1 = tk.Button(root, text="Add 1.0", command=add_1_0)
    bt1.pack() 
    bt2 = tk.Button(root, text="Add 0.1", command=add_0_1)
    bt2.pack()
    bt3 = tk.Button(root, text="Add 0.01", command=add_0_01)
    bt3.pack()
    
    root.mainloop()
    

    You can put repeated code in one function

    def add_1_0():
        add(1.0)
    
    def add_0_1():
        add(0.1)
    
    def add_0_01():
        add(0.01)
    
    def add(val):
        value = float(e1.get())
        value += val
        e1.delete(0, 'end')
        e1.insert(0, value)
    

    If you have code in one function then you can use lambda to assign function with arguments.

    import tkinter as tk
    
    def add(val):
        value = float(e1.get())
        value += val
        e1.delete(0, 'end')
        e1.insert(0, value)
    
    root = tk.Tk()
    
    #entry box
    e1 = tk.Entry(root)
    e1.insert(0, 1.0) 
    e1.pack()
    
    #buttons
    bt1 = tk.Button(root, text="Add 1.0", command=lambda:add(1.0))
    bt1.pack() 
    bt2 = tk.Button(root, text="Add 0.1", command=lambda:add(0.1))
    bt2.pack()
    bt3 = tk.Button(root, text="Add 0.01", command=lambda:add(0.01))
    bt3.pack()
    
    root.mainloop()