如何在 Tkinter 中显示数据库查询响应
我真的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设每条记录都是一行,为每一行创建一个标记(从 line_num.0 到 line_num.end)。对于每个标记,使用
text.tag_bind
并将标记绑定到''
以检测鼠标单击。在回调中使用 lambda 将行号返回给事件处理程序。这是一个简单的例子,它就是这样做的:
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:
以下是使用
Listbox
实现此操作的方法:Here's how you could do it with a
Listbox
: