文本中带有空格的组合框的值 [英] Values ​of combobox with space in the text

查看:52
本文介绍了文本中带有空格的组合框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从mysql请求提供的组合框.如果结果中有空格,则问题似乎不正确

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

如果我在mysql中:我的组合框中的test1我有test1 = OK我的组合框中的测试2我有{测试2} =错误

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

推荐答案

您从mysql得到的结果是一个元组列表.您需要先将它们展平,然后再插入组合框.

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

这篇关于文本中带有空格的组合框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆