![](/img/trans.png)
[英]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.