Python tkinter中的SQL查询结果
我有一个tkinter界面,在这里我需要显示一些查询结果,我需要用户能够修改列并提交结果。目前,为了提取查询,我正在执行以下操作:Python tkinter中的SQL查询结果,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我有一个tkinter界面,在这里我需要显示一些查询结果,我需要用户能够修改列并提交结果。目前,为了提取查询,我正在执行以下操作: conn = connection_info_goes_here cur = conn.cursor() cur.execute(query_goes_here) 这是我的问题: SELECT id, reviewer, task, num_seconds, start_time, end_time FROM hours WHERE DATE(start_time
conn = connection_info_goes_here
cur = conn.cursor()
cur.execute(query_goes_here)
这是我的问题:
SELECT id, reviewer, task, num_seconds, start_time, end_time
FROM hours
WHERE DATE(start_time) = '2014-12-18'
AND reviewer = 'john'
用户需要修改的字段是num\u seconds
(仅数字)。我的问题是,如何使查询结果显示在网格中,以及如何使用按钮使其中一个字段可修改以提交更改
附加信息:我已经使用exec()
以非常混乱的方式完成了这项工作,并通过编程为每个字段创建了变量。它变得很长很混乱,我真的认为必须有一个更好更简单的方法来做到这一点
感谢您的帮助。谢谢
快速更新:由于此功能已暂停,我将添加一个类似于我正在寻找的内容的图像:
当我将条目标签中的值上载回数据库时,条目标签中的值必须替换右侧列中的值
当我说我做这件事的方式很混乱时,是因为我做了(我能想到的唯一方式):
其中,results
是包含查询结果的列表,upload\u cor
是将更改上载到数据库的函数。因为我使用了exec
,即使用户修改了输入框,我也不能使用.get()
来获取用户键入的内容。当我尝试使用.get()
时,即使在输入框中键入了某些内容,我也只能得到无
我只是需要一种不同的方法来实现这一点,同样,欢迎任何想法。您肯定不想使用exec,也不需要使用
textvariable
选项。这两种情况都加剧了混乱。只需将您的小部件存储为字典,直接从条目小部件获取数据,所有这些都变得非常容易管理
下面是一个工作示例:
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text="Done!", command=self.upload_cor)
b.pack()
table = tk.Frame(self)
table.pack(side="top", fill="both", expand=True)
data = (
(45417, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45418, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45419, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45420, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45421, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45422, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45423, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
)
self.widgets = {}
row = 0
for rowid, reviewer, task, num_seconds, start_time, end_time in (data):
row += 1
self.widgets[rowid] = {
"rowid": tk.Label(table, text=rowid),
"reviewer": tk.Label(table, text=reviewer),
"task": tk.Label(table, text=task),
"num_seconds_correction": tk.Entry(table),
"num_seconds": tk.Label(table, text=num_seconds),
"start_time": tk.Label(table, text=start_time),
"end_time": tk.Label(table, text=start_time)
}
self.widgets[rowid]["rowid"].grid(row=row, column=0, sticky="nsew")
self.widgets[rowid]["reviewer"].grid(row=row, column=1, sticky="nsew")
self.widgets[rowid]["task"].grid(row=row, column=2, sticky="nsew")
self.widgets[rowid]["num_seconds_correction"].grid(row=row, column=3, sticky="nsew")
self.widgets[rowid]["num_seconds"].grid(row=row, column=4, sticky="nsew")
self.widgets[rowid]["start_time"].grid(row=row, column=5, sticky="nsew")
self.widgets[rowid]["end_time"].grid(row=row, column=6, sticky="nsew")
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
# invisible row after last row gets all extra space
table.grid_rowconfigure(row+1, weight=1)
def upload_cor(self):
for rowid in sorted(self.widgets.keys()):
entry_widget = self.widgets[rowid]["num_seconds_correction"]
new_value = entry_widget.get()
print("%s: %s" % (rowid, new_value))
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
实际上,通过使用
add_row
方法创建一个Table
类,我可以稍微不同地实现这一点,但我不想变得太复杂。无论您是创建一个表
类,还是在一个类中完成所有工作,或者是按程序完成这些工作,基本思想都是一样的——创建一个字典来表示您的数据。你也可以使用嵌套列表,但我发现字典更容易使用。它们也是自我记录的,因为您通过符号名称引用事物,而不仅仅是知道第4列是开始时间 num_seconds将使用一个条目(或条目ID列表),其余将是标签。您还必须将行号链接到记录,并将每个条目与原始值进行比较,以查看哪些行发生了更改,但如果“id”是唯一的,则应该很简单。入门小部件信息这实际上非常棒,我想现在可以从不同的角度理解它了。还有一个问题,如果结果比窗口大,我如何放置滚动条?可能吗?谢谢如果你应该把这个问题留到另一个问题上来,请告诉我:)@rodrigocf:这是一个单独的问题,以前已经回答过了。看,当然,没问题,我会尽快奖励赏金,它告诉我我必须等待。。。
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text="Done!", command=self.upload_cor)
b.pack()
table = tk.Frame(self)
table.pack(side="top", fill="both", expand=True)
data = (
(45417, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45418, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45419, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45420, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45421, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45422, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
(45423, "rodringof", "CSP L2 Review", 0.000394, "2014-12-19 10:08:12", "2014-12-19 10:08:12"),
)
self.widgets = {}
row = 0
for rowid, reviewer, task, num_seconds, start_time, end_time in (data):
row += 1
self.widgets[rowid] = {
"rowid": tk.Label(table, text=rowid),
"reviewer": tk.Label(table, text=reviewer),
"task": tk.Label(table, text=task),
"num_seconds_correction": tk.Entry(table),
"num_seconds": tk.Label(table, text=num_seconds),
"start_time": tk.Label(table, text=start_time),
"end_time": tk.Label(table, text=start_time)
}
self.widgets[rowid]["rowid"].grid(row=row, column=0, sticky="nsew")
self.widgets[rowid]["reviewer"].grid(row=row, column=1, sticky="nsew")
self.widgets[rowid]["task"].grid(row=row, column=2, sticky="nsew")
self.widgets[rowid]["num_seconds_correction"].grid(row=row, column=3, sticky="nsew")
self.widgets[rowid]["num_seconds"].grid(row=row, column=4, sticky="nsew")
self.widgets[rowid]["start_time"].grid(row=row, column=5, sticky="nsew")
self.widgets[rowid]["end_time"].grid(row=row, column=6, sticky="nsew")
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
# invisible row after last row gets all extra space
table.grid_rowconfigure(row+1, weight=1)
def upload_cor(self):
for rowid in sorted(self.widgets.keys()):
entry_widget = self.widgets[rowid]["num_seconds_correction"]
new_value = entry_widget.get()
print("%s: %s" % (rowid, new_value))
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()