如何在 Tkinter 中显示数据库查询响应

发布于 2024-10-21 15:13:58 字数 265 浏览 8 评论 0原文

我真的是 Tkinter 的新手,所以请原谅我缺乏远见。我想要完成的任务如下:为用户提供 3 个可查询字段的文本字段、一个用于执行查询的提交按钮以及一种显示结果的方法。就数据库查询而言,我对 SQL 和 Python 完全满意。不过,最困难的部分是我希望用户能够单击结果并为该结果触发补充查询以检索额外信息。问题是我想不出一种干净的方法来识别用户在可滚动文本框中单击的记录。

我是否应该使用不同的小部件而不是可滚动文本框来显示单个记录,以便当用户单击时我知道他们单击了哪一个?你是如何解决这个问题的?

I'm a really rookie to Tkinter so excuse my lack of vision here. What I'm trying to accomplish is the following: give the user 3 text fields that are queryable fields, a submit button to execute the query, and a way to display the results. As far as the DB query goes, I'm totally fine with SQL and Python. The hard part about this though, is that I want the user to be able to click a result and have a supplementary query get fired for that result to retrieve extra information. The problem is that I can't think of a clean way to identify which record the user is clicking in a scrollable text box.

Is there a different widget I should be using instead of a scrollableText box to display individual records so that when the user clicks I know which one they clicked on? How have you solved this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听你说爱我 2024-10-28 15:13:58

假设每条记录都是一行,为每一行创建一个标记(从 line_num.0 到 line_num.end)。对于每个标记,使用 text.tag_bind 并将标记绑定到 '' 以检测鼠标单击。在回调中使用 lambda 将行号返回给事件处理程序。

这是一个简单的例子,它就是这样做的:

from Tkinter import *

rows = ["A few lines", "of text", "for our example"]
def callback(row):
    print "you picked row # %s which has this data: %s" % (row, rows[row])

rows = ["A few lines", "of text", "for our example"]
root = Tk()
t = Text(root)
t.pack()

t.insert(END, '\n'.join(rows))
for i in range(len(rows)):
    line_num = i + 1 # Tkinter text counts from 1, not zero
    tag_name = "tag_%s" % line_num
    t.tag_add(tag_name, "%s.0" % line_num, "%s.end" % line_num)
    t.tag_bind(tag_name, "<Button-1>", lambda e, row=i: callback(row))

root.mainloop()

Assuming each record is one line, create a tag for each line (from line_num.0 to line_num.end). For each tag, use text.tag_bind and bind your tag to '<Button-1>' to detect mouse click. Use a lambda in the callback to return your row number to the event handler.

Here's a toy example that does just that:

from Tkinter import *

rows = ["A few lines", "of text", "for our example"]
def callback(row):
    print "you picked row # %s which has this data: %s" % (row, rows[row])

rows = ["A few lines", "of text", "for our example"]
root = Tk()
t = Text(root)
t.pack()

t.insert(END, '\n'.join(rows))
for i in range(len(rows)):
    line_num = i + 1 # Tkinter text counts from 1, not zero
    tag_name = "tag_%s" % line_num
    t.tag_add(tag_name, "%s.0" % line_num, "%s.end" % line_num)
    t.tag_bind(tag_name, "<Button-1>", lambda e, row=i: callback(row))

root.mainloop()
池木 2024-10-28 15:13:58

以下是使用 Listbox 实现此操作的方法:

import Tkinter as tk

rows = ["A few lines", "of text", "for our example"]
def callback(event):
    lb=event.widget
    # http://www.pythonware.com/library/tkinter/introduction/x5453-patterns.htm
    # http://www.pythonware.com/library/tkinter/introduction/x5513-methods.htm
    items = lb.curselection()
    try: items = map(int, items)
    except ValueError: pass
    idx=items[0]
    print(idx,rows[idx])       
root = tk.Tk()
scrollbar = tk.Scrollbar(root, orient="vertical")
lb = tk.Listbox(root, width=50, height=20,
                yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)
scrollbar.pack(side="right", fill="y")
lb.pack(side="left",fill="both", expand=True)
for row in rows:
    lb.insert("end", row)
    # http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
    lb.bind('<ButtonRelease-1>',callback)
root.mainloop()

Here's how you could do it with a Listbox:

import Tkinter as tk

rows = ["A few lines", "of text", "for our example"]
def callback(event):
    lb=event.widget
    # http://www.pythonware.com/library/tkinter/introduction/x5453-patterns.htm
    # http://www.pythonware.com/library/tkinter/introduction/x5513-methods.htm
    items = lb.curselection()
    try: items = map(int, items)
    except ValueError: pass
    idx=items[0]
    print(idx,rows[idx])       
root = tk.Tk()
scrollbar = tk.Scrollbar(root, orient="vertical")
lb = tk.Listbox(root, width=50, height=20,
                yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)
scrollbar.pack(side="right", fill="y")
lb.pack(side="left",fill="both", expand=True)
for row in rows:
    lb.insert("end", row)
    # http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
    lb.bind('<ButtonRelease-1>',callback)
root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文