2

我使用名为 PAGE 的 GUI 编辑器制作了一个简单的 GUI。它包含两个 Tkinter.ttk 组合框。我的第一个组合框从连接的 sqlite 数据库表中获取其值作为列名。当我从第一个组合框中选择列名时,第二个组合框应自动更新与第一个值相关的值。

顺便说一句,我下面的代码在当前条件下可以正常工作。如果我从第一个组合框中选择一个值,它会将这些值打印到交互式 shell。但是这些值应该插入到第二个组合框中。有谁知道我怎么能弄清楚?

任何帮助是极大的赞赏。提前致谢...

这是我的 GUI 代码:

from pysqlite2 import dbapi2 as db
from Tkinter import *
import ttk

def getdata():
    global colnames
    conn = db.connect("blabla.sqlite")
    cur = conn.execute("select * from states")
    col = cur.description
    colnames = [abu[0] for abu in col]
initcombo = getdata()

def vp_start_gui():

    global val, w, root
    root = Tk()
    root.title('Linked Comboboxes')
    root.geometry('301x230+556+208')
    set_Tk_var()
    w = New_Toplevel_1 (root)
    init()
    root.mainloop()

w = None
def create_New_Toplevel_1 (root):

    global w, w_win
    if w: 
        return
    w = Toplevel (root)
    w.title('New_Toplevel_1')
    w.geometry('301x230+556+208')
    set_Tk_var()
    w_win = New_Toplevel_1 (w)
    init()
    return w_win

def destroy_New_Toplevel_1 ():
    global w
    w.destroy()
    w = None


def set_Tk_var():

    global combobox
    combobox = StringVar()


def init():
    pass


class New_Toplevel_1:
    def __init__(self, master=None):

        style = ttk.Style()
        theme = style.theme_use()
        default = style.lookup(theme, 'background')
        master.configure(background=default)


        def choose1(event=None):
            conn2 = db.connect("blabla.sqlite")
            cur2 = conn2.execute("select %s from states" % self.TCombobox1.get())
            results = cur2.fetchall()
            for row in results:
                print row


        self.TCombobox1 = ttk.Combobox (master, state='readonly')
        self.TCombobox1.place(relx=0.03,rely=0.13,relheight=0.09,relwidth=0.48)
        self.TCombobox1["values"] = colnames
        self.TCombobox1.set("Choose one...")
        self.TCombobox1.bind("<<ComboboxSelected>>", choose1)

        self.TCombobox2 = ttk.Combobox (master, state='readonly')
        self.TCombobox2.place(relx=0.03,rely=0.33,relheight=0.09,relwidth=0.48)


        self.TLabel1 = ttk.Label (master)
        self.TLabel1.place(relx=0.03,rely=0.83,height=19,width=28)
        self.TLabel1.configure(relief="flat")
        self.TLabel1.configure(text='''Info:''')

        self.TButton1 = ttk.Button (master)
        self.TButton1.place(relx=0.63,rely=0.22,height=25,width=76)
        self.TButton1.configure(takefocus="")
        self.TButton1.configure(text='''Run''')


if __name__ == '__main__':
    vp_start_gui()
4

1 回答 1

2

将“结果”设置为第二个组合框的“值”配置的值。

def choose1(event=None):
        conn2 = db.connect("blabla.sqlite")
        cur2 = conn2.execute("select %s from states" % self.TCombobox1.get())
        results = cur2.fetchall()
        self.TCombobox2['values'] = results
        for row in results:
            print row
于 2012-11-30T23:07:47.013 回答