You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter如何从SQLite数据库填充子窗口Combobox并传递日期值到主窗口

问题1:子窗口Combobox无法填充选项的修复

你代码中已经完成了SQLite的分类数据查询,问题出在给Combobox赋值的代码被注释,且错误将data列表当做函数添加了调用括号,直接修改DateWindow类中对应代码即可:
找到如下注释行:

#self.c_select_cat['values']=data()

修改为:

self.c_select_cat['values'] = data
# 可选配置:默认选中第一个分类
self.c_select_cat.current(0)

问题2:子窗口参数传递回主窗口的修复

你需要将主窗口的引用传入子窗口,再通过子窗口的按钮点击事件,将选中的分类、起止日期传递给主窗口的报表处理逻辑,修改步骤如下:

  1. 调整DateWindow初始化方法,新增主窗口引用参数
  2. 把报表运行逻辑改为DateWindow内部方法,取值后传递给主窗口
  3. 调整子窗口打开逻辑,传入主窗口实例

关键代码修改示例

1. 调整DateWindow类

class DateWindow(tk.Toplevel):
    # 新增main_window参数接收主窗口引用
    def __init__(self, parent, main_window):
        super(DateWindow,self).__init__()
        self.main_window = main_window

        # 原有数据库查询逻辑不变
        conn= sqlite3.connect(r'/home/bushbug/Databases/NedCard')
        cur=conn.cursor()
        cur.execute("SELECT IncCat from Analyse where Indicator ='E' ")
        data = [row[0] for row in cur.fetchall()]
        cur.close()
        conn.close()       

        self.geometry("500x150")
        self.title("Get dates for report: " )

        self.l_select_cat=tk.Label(self,text='Category',fg='blue')
        self.l_select_cat.grid(row=2, column=0,sticky='w',pady=3,padx=5)
        self.c_select_cat=ttk.Combobox(self,width=20)
        # 修复Combobox赋值
        self.c_select_cat['values']=data
        self.c_select_cat.current(0)
        self.c_select_cat.grid(row=2,column=1,sticky='news',pady=3)

        self.l_Start_Date=tk.Label(self,text="Enter the start date for report", fg="blue")
        self.l_Start_Date.grid(row=4,column=0, sticky="w", padx=5, pady=10)
        self.d_Start_Date = DateEntry(self,width=10,fg="white",Locale='en_US',date_pattern='yyyy-mm-dd')
        self.d_Start_Date.grid(row = 4, column=1,sticky="w",pady=3)
        
        self.l_End_Date=tk.Label(self,text="Enter the end date for report", fg="blue")
        self.l_End_Date.grid(row=6,column=0, sticky="w", padx=5)
        self.d_End_Date = DateEntry(self,width=10,fg="white",Locale='en_US',date_pattern='yyyy-mm-dd')
        self.d_End_Date.grid(row = 6, column=1,sticky="w",pady=3)

        # 按钮绑定内部取值方法
        self.b_Run_Report=ttk.Button(self, text="Run Report", width=10,command=self.run_report)
        self.b_Run_Report.grid(row=7,column=2, sticky="e", pady=10) 

    def run_report(self):
        # 从子窗口组件取所有参数
        category = self.c_select_cat.get()
        start_date = self.d_Start_Date.get()
        end_date = self.d_End_Date.get()
        # 可选添加日期校验
        if start_date > end_date:
            msgbox.showerror("错误","结束日期不能早于开始日期")
            return
        # 传参给主窗口的报表方法
        self.main_window.run_category_date_report(category, start_date, end_date)
        # 运行完成关闭子窗口
        self.destroy()

2. 调整Menu_ex类的Open_DateWindow方法

def Open_DateWindow(self):
    # 传入主窗口实例给子窗口
    self.window=DateWindow(self, self.parent)
    self.window.grab_set()

3. 主窗口新增接收参数的报表方法

class MainWindow(tk.Tk):
    def __init__(self):    
        super().__init__()
        # 原有初始化逻辑不变
        global format_date
        date = dt.datetime.now()
        format_date=f"{date:%Y-%m-%d}"
        print(format_date)
        self.title('My Nedbank Credit Card')
        self.geometry('1150x450')
        self.grid_rowconfigure(0,weight=1)
        self.grid_columnconfigure(0,weight=1)
        self.resizable(width=False,height=False)
        self.menu = Menu_ex(self)

    # 新增方法接收子窗口参数,运行报表
    def run_category_date_report(self, category, start_date, end_date):
        # 把你原有Run_Report_By_Spend_Cat的逻辑移到此处,直接使用三个传入参数即可
        print(f"生成报表:分类={category},起始日期={start_date},结束日期={end_date}")

内容的提问来源于stack exchange,提问作者Frank Aldridge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 07:36:07