Values ​of combobox with space in the text

非 Y 不嫁゛ 提交于 2021-01-29 10:21:29

问题


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

回答1:


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()


来源:https://stackoverflow.com/questions/55947957/values-of-combobox-with-space-in-the-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!