Search code examples
tkintercombobox

How get values from Combobox to write into textbox


Using "Add" button, I want takes values from combobox into writen into entry. I also keep track of the existing values from on each click rather than allowing the user to select multiple names. For example:

When user select g from combobox and click add button, in textbox wroten "Selected letter: g"

After user select Q and click add button, in textbox wroten "Selected letter g,Q"

After user select p and click add button, in textbox wroten "Selected letter g,Q,p"

I think I confused on defining function "Add" button. Thanks.

    import tkinter as tk
from tkinter.ttk import Combobox

form=tk.Tk()
form.title("Pi Theorem")
form.geometry("300x300")

x=tk.StringVar()

def Add():
    textbox.insert(x)

#buton
buton=tk.Button(form,text='EKLE',fg='black',command=Add)

buton.place(x=200,y=50,height=20)

#entry - textbox
textbox=tk.Entry(fg='black',bg='white')
textbox.pack(ipady=10)
textbox.place(width=150,height=20,x=20,y=100)

#combobox 
degiskenler=['g','Q','p','H','M']
combobox=Combobox(form, values=degiskenler,textvariable=x,height=3)
combobox.pack()
combobox.place(width=150,height=20,x=20,y=50)

form.mainloop()

Solution

  • To get the value of the combobox in tkinter just use this command:

    combobox.get()
    

    Here is the final code:

    import tkinter as tk
    from tkinter.ttk import Combobox
    
    form=tk.Tk()
    form.title("Pi Theorem")
    form.geometry("300x300")
    
    
    def Add():
        textbox.insert(END, combobox.get())
    
    #buton
    buton=tk.Button(form,text='EKLE',fg='black',command=Add)
    
    buton.place(x=200,y=50,height=20)
    
    #entry - textbox
    textbox=tk.Entry(fg='black',bg='white')
    textbox.pack(ipady=10)
    textbox.place(width=150,height=20,x=20,y=100)
    
    #combobox 
    degiskenler=['g','Q','p','H','M']
    combobox=Combobox(form, values=degiskenler,height=3)
    combobox.pack()
    combobox.place(width=150,height=20,x=20,y=50)
    
    form.mainloop()
    

    There is no text variable in this. Simply take the value and put it in the end of the entrybox.

    Thank You!