Sum of Combobox & Text widget inputs

Adding Combobox and Text widget values

Adding combobox with selection options

sel1=tk.IntVar() # string variable 
sel1.set(0)
data=[5,3,15,22]
cb1 = ttk.Combobox(my_w, values=data,height=3, width=5,
    font=22, textvariable=sel1)
cb1.grid(row=0,column=0,padx=10,pady=30)

On click of the button

Here we are using click event of the button to trigger the function my_upd().
b1=tk.Button(my_w,text='Sum',command=lambda:my_upd(),font=22)

Reading and adding values

We collect the input values from Combobox and from the Text widget , then add these two values.
By using config() we will update the Label to display the sum of the values.
def my_upd(*args):
    total=sel1.get() + int(t1.get("1.0",END))
    l1.config(text=total)# update the Label text 
Tkinter adding combobox selection and text widget value and displaying the sum in a Label


Full code is here
import tkinter as tk
from tkinter import ttk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x250")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title
def my_upd(*args):
    total=sel1.get() + int(t1.get("1.0",END))
    l1.config(text=total) # update the Label text 

sel1=tk.IntVar() # string variable 
sel1.set(0)
data=[5,3,15,22]
cb1 = ttk.Combobox(my_w, values=data,height=3, 
	width=5,font=22, textvariable=sel1)
cb1.grid(row=0,column=0,padx=10,pady=10)
t1=tk.Text(my_w,width=10,height=1,bg='yellow',font=22)
t1.grid(row=0,column=1,padx=5)
t1.insert(tk.END, 0) # initial value of text widget 
l1=tk.Label(my_w,text='Total',font=22,fg='blue')
l1.grid(row=0,column=2,padx=5)
b1=tk.Button(my_w,text='Sum',command=lambda:my_upd(),font=22)
b1.grid(row=1,column=1)
#sel1.trace('w',my_upd) # to trigger the function on change 
my_w.mainloop()  # Keep the window open
Combobox
MySQL , SQLite,CSV and Json data is used to add options for Combobox
Two and Three interlinked Comboboxes
Listbox OptionMenu
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    29-07-2022

    Dear Sir,
    Thank you for your good instructions. I learn a lot from your videos.
    One question:
    how to delete the combobox option dynamicly?
    i was unable to use cb1['values'] -=(e1.get(),) to delete the option.
    please kindly teach me how to do it.

    Thank you very much.
    I subcribed your yourtube already.

    ST

    05-08-2022

    You can't delete by using cb1['values'] -=(e1.get(),) as it is a tuple. Create a new tuple with all the options except the one you want to delete. Then assign the new tuple to cb1.
    This part is added to this page. Here is the video again.

    https://youtu.be/PICzrYI6O9A

    22-11-2022

    In one of the programs which i am working , the combo box freezes when i click on dropdown and then the application stops and crashes. Please advise

    08-01-2023

    Are you using any onclick event and triggering any function? Just remove the function and see how it is working. Then check the code inside the function.




    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer