简体   繁体   English

从 MySql 输入中删除 {}

[英]Remove {} from MySql input

I am using a MySql stored procedure, get_list, to populate a ttk.Combobox我正在使用 MySql 存储过程 get_list 来填充 ttk.Combobox

my_number = tk.StringVar()
cb=ttk.Combobox(top_frame,width=25,textvariable=my_number)
cb['values']=db.get_list()
cb.grid(row=1,column=4)

It works fine, except the results are bracketed by {} .它工作正常,除了结果用{}括起来。

RESULTS: {Smith, John}结果: {Smith, John}

What I want: Smith, John我想要什么: Smith, John

How do I get rid of the beginning and ending {} ?如何摆脱开头和结尾的{}

The problem is that the database query is returning a list of rows, where each row is a list with a single column.问题是数据库查询返回了一个行列表,其中每一行都是一个包含单列的列表。 What you need for the values option is a list of strings. values选项需要的是一个字符串列表。

So, the simple solution is to use a list comprehension to convert each row to a string.因此,简单的解决方案是使用列表推导将每一行转换为字符串。 Something like this should work:像这样的东西应该工作:

values = [row[0] for row in db.get_list()]
cb['values'] = values

Since I can't run your code I can't say for sure.由于我无法运行您的代码,我不能肯定地说。 Regardless, the root of the problem is that you're passing a list of lists (or tuples) and tkinter requires a list of strings.无论如何,问题的根源在于您要传递一个列表(或元组)列表,而 tkinter 需要一个字符串列表。

Because internally tkinter uses curly braces for lists, it's adding the curly braces to preserve the nature of the data you're passing in. It's up to you to convert the data you get back from db.get_list() into a list of strings.因为在内部 tkinter 使用大括号作为列表,它添加大括号以保留您传入的数据的性质。您可以将您从db.get_list()返回的数据转换为字符串列表。

Bryan, I inserted the two line of code you suggested, but I am still getting value surrounded by {}.布莱恩,我插入了你建议的两行代码,但我仍然得到了被 {} 包围的价值。 Lots I don't understand, but I believe, never know for sure, that the MySql procedure is returning a string.很多我不明白,但我相信,永远不知道,MySql 过程正在返回一个字符串。 Here is the MySQL code:这是 MySQL 代码:

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_list`()
BEGIN
    SELECT concat(last_name,', ', first_name) as last_first
      FROM volunteers
    ORDER BY
      last_first;
END


my_number = tk.StringVar()
cb = ttk.Combobox(top_frame, width=25, textvariable=my_number)
values = [row[0] for row in db.get_list()]
cb['values'] = db.get_list()
cb.grid(row=1, column=4)

thanks for your persistence.谢谢你的坚持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM