1

这是我在 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()
4

1 回答 1

2

If that's your first program in Python, you're off to a good start! Your question really has little to do with MySQL, and you seem to have a good handle on that, so I'll leave that alone and just focus on the Tkinter issues you are having. I don't have a MySQL database to test with anyway :)

It's generally a good idea in GUI applications, at least in Tkinter, to setup your own application class with a Tk root frame as the application's master container. The reason for this is that GUIs are a always a bit of slight of hand, they are only a facade for real application data, and that real data needs someplace to live. By creating your own application class, you have a home for that data via self.

Okay, enough with all the boring stuff, let's just build something:

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()

Hope this helps, and have fun building GUIs!

于 2013-10-28T23:50:28.317 回答