2012-10-24 82 views
2

我用一個稱爲PAGE的GUI編輯器製作了一個簡單的GUI。它包含兩個Tkinter.ttk組合框。我的第一個組合框從連接的sqlite數據庫表中獲取它的值作爲列名。當我從第一個組合框中選擇一個列名時,第二個組合框應該自動更新與第一個值相關的值。Python 2.7:Tkinter/ttk鏈接組合框

順便說一下,我的代碼下面在這種情況下正常工作。如果我從第一個組合框中選擇一個值,它會將值打印到交互式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() 

回答

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