Search code examples
pythontkintercombobox

Values ​of combobox with space in the text


I have a combobox that feeds from a mysql request. The problem if the result has space seems wrong

If i have in mysql: test1 in my combobox i have test1 = OK test 2 in my combobox i have {test 2} = Wrong

#FRAME INFO
frame_info = LabelFrame (Product, text = 'Info Maker',height=240,width=1000)
frame_info.place (x=220, y=10)

combo_maker = ttk.Combobox(frame_info,state="readonly")
#combo_maker['value'] = combo_input()
combo_maker['value'] = combo_input()
combo_maker.current(0)
combo_maker.place(x=5, y=5, height = 25, width = 180)

#FRAME INFO

Solution

  • Your result from mysql is a list of tuples. You will need to flatten them first before inserting into a combobox.

    The below showcases the difference before and after flattening:

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    def combo_input():
        return [('test 1',), ('test 2',), ('test3',)]
    
    a_combo_maker = ttk.Combobox(root,state="readonly")
    a_combo_maker["value"] = combo_input()
    a_combo_maker.current(0)
    a_combo_maker.pack()
    
    b_combo_maker = ttk.Combobox(root,state="readonly")
    b_combo_maker["value"] = [item for result in combo_input() for item in result if item]
    b_combo_maker.current(0)
    b_combo_maker.pack()
    
    root.mainloop()