从第一个组合框选择后如何在选择查询中为另一个组合框添加值

时间:2019-06-07 21:28:47

标签: python sqlite tkinter combobox

我正在尝试在combobox1上进行选择,使它用sqlite3数据库中的数据填充combobox2

我做了combobox1,但是我不知道为什么它不能与combobox2一起使用 我试图使event = none错误消失了,但是combobox2上没有值

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个表,具有一对多关系,一个表对应年级,每个级别对应一个班级,每个班级的学生信息

1 个答案:

答案 0 :(得分:0)

  

问题是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 1选择一个选项,Combobox 2现在应该更新