简体   繁体   English

如何使用从我的SQLite数据库中检索的数据填充tkinter下拉框?

[英]How do I populate tkinter drop-down box with data retrieved from my SQLite database?

Basically I have a cinema booking system with a database which stores what films are on and at what time date etc.. I have managed to print the contents in the format I wish to use however, I am unsure how to use this data in terms of using a tkinter drop down menu which individually shows the lines shown underneath. 基本上我有一个电影院预订系统,里面有一个数据库,可以存储电影的内容和时间等等。我已经设法以我希望使用的格式打印内容但是,我不确定如何使用这些数据的术语使用tkinter下拉菜单,它单独显示下面显示的行。

import sqlite3 as lite
conn = lite.connect('Logindetails.db')

with conn:

    conn.row_factory = lite.Row
    cursor = conn.cursor()

    title = raw_input("Name of tile: ")

    query = "SELECT * FROM Viewings WHERE Title=?"
    cursor.execute(query, (title,))

    rows = cursor.fetchall()

    for row in rows:
        data = "%s %s %s %s %s" % (row["Id"], row["Title"], row["Date"], row["Time"], row["Duration"])
        print data

That is my code for retrieving the data that its required. 这是我检索其所需数据的代码。 Here is its output. 这是它的输出。

Name of tile: Frozen
1 Frozen 06/15 11:35 95
3 Frozen 06/18 11:35 95
4 Frozen 06/30 11:25 95
5 Frozen 07/02 11:45 95
6 Frozen 07/05 12:30 95

I have written the foundations of my tkinter project just unsure how to manipulate this data and import it into a drop down box of which each data set can be selected singularly. 我已经编写了我的tkinter项目的基础,只是不确定如何操作这些数据并将其导入到一个下拉框中,每个数据集可以单独选择。

You could define movieList array somewhere in your code and then append the strings created in for loop to the movieList array: 您可以在代码中的某处定义movieList数组,然后将for循环中创建的字符串追加到movieList数组:

movieList = []
...
for row in rows:
        data = "%s %s %s %s %s" % (row["Id"], row["Title"], row["Date"], row["Time"], row["Duration"])
        movieList.append(data)
        print data

(As a side note, you could leave out row["Id"] and row["Duration"] fields, as the former is not really important to your users and the latter is redundant) (作为旁注,您可以省略row["Id"]row["Duration"]字段,因为前者对您的用户并不重要,后者是多余的)

Building the drop-down menu is easy then, using OptionMenu widget; 使用OptionMenu小部件构建下拉菜单很容易; here is a minimal example with hard-coded values, the movieList of yours will be constructed dynamically in the aforementioned for loop: 这里是一个带有硬编码值的最小例子,你的movieList将在前面提到的for循环中动态构造:

from Tkinter import *

movieList = ["1 Frozen 06/15 11:35 95", "3 Frozen 06/18 11:35 95",
        "4 Frozen 06/30 11:25 95", "5 Frozen 07/02 11:45 95",
        "6 Frozen 07/05 12:30 95"]

master = Tk()

option = StringVar(master)
option.set(movieList[0]) # Set the first value to be the default option

w = apply(OptionMenu, (master, option) + tuple(movieList))
w.pack()

mainloop()

You can get the the chosen option then by calling get() method: 您可以通过调用get()方法get()所选的选项:

option.get()

暂无
暂无

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

相关问题 每次按下“刷新”时,如何用新数据填充 tkinter 下拉框? - How do i populate tkinter drop-down box with new data everytime i pressed “refresh”? 如何在我的tkinter组合框的下拉列表中添加值? - How do I add values in the drop down of my tkinter combo box? 将 OptionMenu() 下拉菜单中的数据插入 Python 中的 SQLite 数据库 - Inserting data from an OptionMenu() drop-down menu into an SQLite database in Python Flask 从下拉列表中获取数据到 sqlite - Flask get data from drop-down into sqlite 如何使我的页面在从下拉菜单或文本字段获取数据之间进行选择? - How would I make my page choose between getting data from a drop-down menu or a text-field? 如何根据下拉菜单中的选项动态填充tkinter中的选项小部件? - How can I dynamic populate an option widget in tkinter depending on a choice from a drop down menu? 如何从 .kv 文件中添加在 .py 文件中制作的 Kivy 下拉菜单? - How do I add a Kivy drop-down menu made in the .py file from the .kv file? 当我使用 Tkinter 下拉框时,使用 WHERE 子句的 SQlite 查询没有给我正确的输出 - SQlite Query using WHERE clause is not giving me the right output when I use a Tkinter drop down box 在 SQLite 中,如何使用输入框中的文本搜索我的数据库,并将结果显示为文本? - In SQLite, how do I search my database with text from an entry box, and display the result as text? 如何从 Tkinter 的输入框中检索数据? - How can I retrieve the data from my entry box in Tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM