我使用名为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()
答案 0 :(得分:2)
设置&#39;结果&#39;作为&#39;值的价值&#39;第二个组合框的配置。
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