简体   繁体   English

从第一个组合框中选择后,如何从select查询中为其他组合框添加值

[英]how to add value to the other combobox from select query after selecting from the first combobox

i am trying to make a selection on a combobox1 thin it fill combobox2 with data from the sqlite3 db 我试图在一个combobox1上做出选择,它用来自sqlite3 db的数据填充combobox2

i did the combobox1 but i don't know why it is not working with the combobox2 i tried to make event = none the error gone but no value on the 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()

my db is 3 tables with one to many relationship one for grade level one for classes in each level and the student info for each class 我的数据库是3个表,一对多关系一个用于每个级别的班级的一年级和每个班级的学生信息

The problem is that the comboclass() function doesn't actually update the values of the Combobox , this is an easy fix. 问题是comboclass()函数实际上并没有更新Combobox的值,这是一个简单的修复。

You just need to create a function that updates the values. 您只需要创建一个更新值的函数。 Which is basically just this line of code : self.combobox2['value'] = self.comboclass() 这基本上就是这行代码: self.combobox2['value'] = self.comboclass()

So this would be the code you would need to change/add: 所以这将是您需要更改/添加的代码:

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()

The Combobox 2 should now update whenever you select an option for Combobox 1 Combobox ,只要你选择一个选项2现在应该更新Combobox 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM