Binding spinbox to textbox

63 Views Asked by At

How can we get the value of a Spinbox (from 1 to 10) then multiply it into a number (ex: 100)? And then display the result into a textbox in TKINTER???

I tried the .get() method to get the value, then I multiplied it by 100. Then I used the .insert() method to display the result in a textbox, but it didn't work that well

1

There are 1 best solutions below

3
toyota Supra On

How can we get the value of a Spinbox (from 1 to 10) then multiply it into a number (ex: 100)

I tried the .get() method to get the value, then I multiplied it by 100. Then I used the .insert() method to display the result in a textbox, but it didn't work that well

  • Add three widgets; Spinbox, Text and Button
  • Create function called value_changed

Edit: re-work in value_changed()

Snippet:

import tkinter as tk
from tkinter import ttk


# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Spinbox Demo')

def value_changed():
    cal = current_value.get()
    y = 100
    ca= int(cal) * y
    textbox.insert(tk.END, ca)

        
textbox = tk.Text(root, width=25, height=5)
btn = tk.Button(root, text=r'Click', command= value_changed)
 
# Spinbox
current_value = tk.StringVar(value=1)
spin_box = ttk.Spinbox(
    root,
    from_=1,
    to=10,
    textvariable=current_value,
    wrap=True)
 
spin_box.pack()
textbox.pack()

btn.pack(pady =10)

root.mainloop()

Screenshot:

enter image description here