【问题标题】:Values ​of combobox with space in the text文本中带有空格的组合框的值
【发布时间】:2019-05-02 07:56:46
【问题描述】:

我有一个来自 mysql 请求的组合框。结果有空格的问题似乎是错误的

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

#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

【问题讨论】:

  • 请显示combo_input()的结果。如果你传递一个正确的列表或元组,它应该可以正常工作。
  • 显示图片。 imgur.com/nvM4wFk。是用mysql测试的。
  • 不,我的意思是你的函数combo_input的返回结果,而不是在你将它传递给组合框之后。简单的print (combo_input()) 并展示它是什么。
  • 我有[('test 1',), ('test 2',), ('test3',)]

标签: python tkinter combobox


【解决方案1】:

你从 mysql 得到的结果是一个元组列表。在插入组合框之前,您需要先将它们展平。

下面展示了展平前后的差异:

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

【讨论】:

  • 有效。查看我已阅读的教程时会发生什么。没有任何参考。我不知道。我会每天使用它。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 2017-03-01
  • 1970-01-01
相关资源
最近更新 更多