在列表框中显示 sql 查询的结果时去掉括号(Tkinter 和 Python) [英] Get rid of brackets when displaying results from sql query in a list box (Tkinter and Python)

查看:73
本文介绍了在列表框中显示 sql 查询的结果时去掉括号(Tkinter 和 Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,简单地说,人们将数据输入数据库,然后他们可以通过搜索来检索数据.我在 python 中使用 sqlite 3 和 Tkinter 来构建这个程序.当我搜索数据并显示在 Tkinter 列表框中时,它显示为:{Bob Jones} name@email.com {John Jones}

I am building an application where, simply, people enter data into a database and they can retrieve their data by searching for it. I am using sqlite 3 and Tkinter in python to build this program. When I'm searching for data and it displays in the Tkinter list box it shows as: {Bob Jones} name@email.com {John Jones}

如何在没有大括号的情况下显示查询?

How do I display the query without those curly brackets?

后端代码:

def search(parent_email=""):
    conn=sqlite3.connect("parentsevening.db")
    cur=conn.cursor()
    cur.execute("SELECT * FROM bookings WHERE parent_email=? ORDER BY app_time", (parent_email,))
    rows=cur.fetchall()
    conn.close()
    return rows

前端代码:

def search_command():
    list1.delete(0,END)
    for row in parents_backend.search(parent_email.get()):
        list1.insert(END,row)

推荐答案

您将 Python 列表插入到列表框中,但 insert 方法需要一个字符串.您负责在插入数据之前将数据转换为字符串.如果您没有明确进行转换,tkinter 将为您进行转换.由于底层 tcl 解释器将使用自己的语法来保留原始数据的列表结构,这可能会在数据中产生括号.

You're inserting a python list into a listbox, but the insert method expects a string. You are responsible for converting the data to a string before inserting the data. If you don't explicitly do the conversion, tkinter will do the conversion for you. That may yield brackets in the data due to the fact that the underlying tcl interpreter will use its own syntax to preserve the list structure of the original data.

例如,如果您希望列表中的元素之间出现空格,您可以使用join:

For example, if you want the elements in the list to appear with spaces between them you can use join:

list1.insert(END, " ".join(row))

这篇关于在列表框中显示 sql 查询的结果时去掉括号(Tkinter 和 Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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