带有 MySQLdb 的 Python Tkinter 列表框

标签 python mysql listbox tkinter mysql-python

这是我在 python 中的第一个程序,我正在尝试创建一个程序,它将 MySql 地址簿与星号服务器连接起来并进行调用。 “星号代码部分”没问题,我稍后会添加,但问题是我在 Tkinter 中有一个列表框,我想用从 mysqldb 查询中检索到的值填充它。 我只能向列表框添加一个值,但如果我进行打印,结果是正确的。 我该如何解决这个问题?我想我将不得不做一个 for 循环。 稍后我将不得不知道如何从列表中选择一个值并将其存储在变量中。

from Tkinter import *
import MySQLdb
root = Tk()
root.title("PyCall")
myContainer = Frame(root)
myContainer.pack(side=TOP, expand=YES, fill=BOTH)

db = MySQLdb.connect("localhost",port=3306, user="root", passwd="mypass", db="test" )
cursor = db.cursor()

cursor.execute("SELECT * FROM utenti")
db.commit()

numrows = int(cursor.rowcount)
for x in range(0,numrows):
    row = cursor.fetchone()
    print row[1]

listbox = Listbox(root)
listbox.pack()
listbox.insert(END, row[1])

root.mainloop()
db.close()

最佳答案

如果这是您使用 Python 编写的第一个程序,那么您就有了一个良好的开端!您的问题确实与 MySQL 没有什么关系,而且您似乎对此有很好的处理,所以我将不理会它,只关注您遇到的 Tkinter 问题。反正我没有 MySQL 数据库来测试 :)

在 GUI 应用程序中,至少在 Tkinter 中,使用 Tk 根框架作为应用程序的主容器来设置您自己的应用程序类通常是一个好主意。这样做的原因是 GUI 总是有点手巧,它们只是真实应用程序数据的外观,而真实数据需要存在的地方。通过创建您自己的应用程序类,您可以通过 self 为该数据提供一个位置。

好吧,所有无聊的东西都说完了,让我们来构建一些东西:

import Tkinter


class Application(Tkinter.Frame):
    def __init__(self, master):
        Tkinter.Frame.__init__(self, master)
        self.master.minsize(width=256, height=256)
        self.master.config()
        self.pack()

        self.main_frame = Tkinter.Frame()

        self.some_list = [
            'One',
            'Two',
            'Three',
            'Four'
        ]

        self.some_listbox = Tkinter.Listbox(self.main_frame)

        # bind the selection event to a custom function
        # Note the absence of parentheses because it's a callback function
        self.some_listbox.bind('<<ListboxSelect>>', self.listbox_changed)
        self.some_listbox.pack(fill='both', expand=True)
        self.main_frame.pack(fill='both', expand=True)

        # insert our items into the list box
        for i, item in enumerate(self.some_list):
            self.some_listbox.insert(i, item)

        # make a label to show the selected item
        self.some_label = Tkinter.Label(self.main_frame, text="Welcome to SO!")
        self.some_label.pack(side='top')

        # not really necessary, just make things look nice and centered
        self.main_frame.place(in_=self.master, anchor='c', relx=.5, rely=.5)

    def listbox_changed(self, *args, **kwargs):
        selection_index = self.some_listbox.curselection()
        selection_text = self.some_listbox.get(selection_index, selection_index)
        self.some_label.config(text=selection_text)

root = Tkinter.Tk()
app = Application(root)
app.mainloop()

希望这对您有所帮助,祝您构建 GUI 愉快!

关于带有 MySQLdb 的 Python Tkinter 列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19638940/

相关文章:

MySql 在插入时触发(错误)

mysql - 合并Mysql中的两个查询

c# - 获取 RadListBox 中的 Checked 复选框 (telerik)

python - Django 网站中的 Bokeh 渲染问题

python - 难以在 Flask 的模板文件夹中将外部 CSS 和 bootstrap 文件包含在 HTML 中

python - 使用 Python/OpenCV 对非浅色/彩色数字进行数字识别

mysql - Rails - 如何加入基于单个 View 的两个关系?

sql - 关于dsn字符串的查询

c# - 在列表框中显示Oxyplot图表的动态列表

python - 在 Python 中为 findContours 使用层次结构