[英]Get Value from first Combobox and change value of second Combobox
[英]how to add value to the other combobox from select query after selecting from the first combobox
我試圖在一個combobox1上做出選擇,它用來自sqlite3 db的數據填充combobox2
我做了combobox1,但我不知道為什么它不使用combobox2我試圖使事件=沒有錯誤消失但沒有值的組合框2
import tkinter as tk
from tkinter import ttk
import sqlite3
class SchoolProjict(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage,):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def get_page(self, classname):
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
class StartPage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self, parent)
lablel = tk.Label(self, text = "Stuident Info")
lablel.grid(row = 1, columnspan = 3, pady=5, padx=5)
lable2 = tk.Label(self, text = "gread")
lable2.grid(row = 2, column = 2, pady=5, padx=5)
lable3 = tk.Label(self, text = "class")
lable3.grid(row = 3, column = 2, pady=5, padx=5)
lable4 = tk.Label(self, text = "Stuident Name")
lable4.grid(row = 4, column = 2, pady=5, padx=5)
self.number = tk.StringVar()
self.combobox1 = ttk.Combobox(self, width = 15)
self.combobox1.bind("<<ComboboxSelected>>", self.comboclass)
self.combobox1['value'] = self.combogread()
self.combobox1.grid(row = 2, column = 1, pady=5, padx=5)
self.combobox2 = ttk.Combobox(self, width = 15)
self.combobox2['value'] = self.comboclass()
self.combobox2.grid(row = 3, column = 1, pady=5, padx=5)
def combogread(self):
self.conn = sqlite3.connect("exeldata.db")
self.cur = self.conn.cursor()
self.cur = self.conn.execute('SELECT rowid, GradNumber FROM gradelevel')
result = []
for row in self.cur.fetchall():
result.append(row[1])
return result
def comboclass(self, event = None):
greadid = self.combobox1.get()
self.conn = sqlite3.connect("exeldata.db")
self.cur = self.conn.cursor()
self.cur = self.conn.execute('SELECT rowid, GradNumber FROM gradelevel WHERE GradNumber = (?)', (greadid,))
result = []
for row in self.cur.fetchall():
result.append(row[0])
self.cur = self.conn.execute('SELECT rowid , ClassNumb FROM classnumber WHERE GradID = (?)', (str(result),))
result = []
for row in self.cur.fetchall():
result.append(row[0])
return result
app = SchoolProjict()
app.mainloop()
我的數據庫是3個表,一對多關系一個用於每個級別的班級的一年級和每個班級的學生信息
問題是
comboclass()
函數實際上並沒有更新Combobox
的值,這是一個簡單的修復。
您只需要創建一個更新值的函數。 這基本上就是這行代碼: self.combobox2['value'] = self.comboclass()
所以這將是您需要更改/添加的代碼:
class StartPage(tk.Frame):
def __init__(self, parent, controller):
"""All Init Code Here"""
self.combobox1 = ttk.Combobox(self, width = 15)
self.combobox1.bind("<<ComboboxSelected>>", self.update_combo) # Changed binds command to the update the combobox
self.combobox1['value'] = self.combogread()
self.combobox1.grid(row = 2, column = 1, pady=5, padx=5)
self.combobox2 = ttk.Combobox(self, width = 15)
self.combobox2['value'] = self.comboclass()
self.combobox2.grid(row = 3, column = 1, pady=5, padx=5)
def update_combo(self, event=None): # New function to update the combobox
self.combobox2['value'] = self.comboclass()
該Combobox
,只要你選擇一個選項2現在應該更新Combobox
1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.