如何在 tkinter GUI 中的单独行上输出 sqlite3 查询的结果 [英] How to output results from a sqlite3 query on seperate rows in a tkinter GUI

查看:26
本文介绍了如何在 tkinter GUI 中的单独行上输出 sqlite3 查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写我的计算机科学课程,这是一个日记和周计划程序,它将用户的输入输入到 sqlite3 数据库中,然后在搜索名称时,它将与查询匹配的每条记录输出到 GUI(特金特).目前,它在一行上打印所有行,但是我希望它们输出一行,然后转移到下一行.

I am currently coding my Computer Science coursework, which is a diary and week planner that takes input from the user into a sqlite3 database, then when the name is searched, it outputs every record that matches with the query to the GUI (Tkinter). Currently, it prints all the rows on one line, however I want them to output one row, then transfer to the next line.

def searched():
    Main_Screen.delete('1.0', END)
    searched_for = search.get()
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Name = ?''',(searched_for,))
    list1 = list(cursor.fetchall())
    list1 = str(list1)
    cursor.execute('''SELECT COUNT(*) FROM dates WHERE Name = ?''',(searched_for,))
    result = cursor.fetchone()
    conn.commit()
    print(result)
    str.replace(')','\n', END )

    Main_Screen.insert(tkinter.END, list1)

我使用关键字test"进行了测试,并通过我的 GUI 得到了这个:

I tested using the keyword 'test', and got this through my GUI:

[('2017-06-06', 'test', '11:00:00', '12:00:00'), ('2017-06-    06', 'test', '10:00:00', '12:00:00'), ('2017-05-04', 'test', '11:00:00', '16:00:00'), ('2017-04-03', 'test', '11:00:00', '14:00:00')]

我哪里出错了?

推荐答案

当您执行 list1 = list(cursor.fetchall()) 时,您会从数据库中返回一个值列表,如您所见在您的输出中:

When you do list1 = list(cursor.fetchall()) you return a list of values from your database, as you see in your output:

[('2017-06-06', 'test', '11:00:00', '12:00:00'), ('2017-06-    06', 'test', '10:00:00', '12:00:00'), ('2017-05-04', 'test', '11:00:00', '16:00:00'), ('2017-04-03', 'test', '11:00:00', '14:00:00')]

不想将其包装为字符串 (list1 = str(list1)),因为这会导致您看到的输出 - 您想要以简洁的方式组合结果进行显示.

You don't want to wrap this as a string (list1 = str(list1)), because that results in the output you're seeing - you want to combine the results in a neat way for displaying.

str.join() 方法通过在它们之间放置您指定的字符串来组合可迭代的元素.所以,如果你要这样做,比如,'-'.join(['Dan Simons', 'asongtoruin']),你的输出将是 Dan Simons-asongtoruin.

由于您的 fetchall 调用返回一个元组列表,我们需要两次使用此方法:

As your fetchall call returns a list of tuples, we need to us this method twice:

  1. 在每行的列之间放置空格
  2. 将每一行放在一个新行

然后,对于每一行,步骤 1 将类似于 ' '.join(row) .例如,

Step 1, then, would be something like ' '.join(row) for each of your rows. For example,

' '.join(('2017-06-06', 'test', '11:00:00', '12:00:00'))

会给我们 2017-06-06 test 11:00:00 12:00:00 作为一个字符串.

would give us 2017-06-06 test 11:00:00 12:00:00 as a string.

如果我们使用列表理解,我们可以一次性完成所有这些操作,如下所示:

If we use a list comprehension, we can do all of these in one go, like so:

joined = [' '.join(row) for row in list1]`

这会给我们:

['2017-06-06 test 11:00:00 12:00:00', '2017-06-    06 test 10:00:00 12:00:00', '2017-05-04 test 11:00:00 16:00:00', '2017-04-03 test 11:00:00 14:00:00']

然后我们可以使用 '\n'.join(joined) 将每一个都放到新的一行,我们甚至可以在一行中完成两个步骤.您的代码可能会变成:

We can then put each one onto a new line by using '\n'.join(joined), and we can even do both steps in one line. Your code could then become:

def searched():
    Main_Screen.delete('1.0', END)
    searched_for = search.get()
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Name = ?''',(searched_for,))
    list1 = cursor.fetchall()
    cursor.execute('''SELECT COUNT(*) FROM dates WHERE Name = ?''',(searched_for,))
    result = cursor.fetchone()
    # conn.commit() - probably not needed
    print(result)
    output_text = '\n'.join([' '.join(row) for row in list1])

    Main_Screen.insert(tkinter.END, output_text)

这应该将从您的数据库返回的文本块插入小部件(尽管没有看到更广泛的代码我无法确认)

This should insert the block of text returned form your database into the widget (though without seeing the wider code I can't confirm it)

这篇关于如何在 tkinter GUI 中的单独行上输出 sqlite3 查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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