[英]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.