为什么 tkinter 小部件出现在错误的窗口上?

问题描述 投票:0回答:0

我已经为这个错误苦苦挣扎了几个星期。菜单预计会根据单击的按钮打开一个新窗口,它确实如此。当 ttk.Treview 对象(或表格)预计会出现在新窗口中但它一直出现在菜单一上时,就会出现问题。 我试过检查亲子关系,但一切似乎都很好。有人能告诉我为什么会这样吗?

from tkinter import * 
import tkinter as tk
from tkinter import ttk

import sqlite3
from basicCRUD_methods import *


class ScheduleWindow:
    #All the design of the GUI window takes place here. The GUI adapts to the database table that will be manipulated
    def __init__(self, window, column_names,table_name):
        
        #Window properties
        self.window = window
        self.window.title(f"{table_name} MANIPULATION")
        self.window.geometry("1000x1050")
        self.window.configure(bg="blue")
        self.column_names = column_names
        self.table_name = table_name
        
        # Frame container(s)
        frame = LabelFrame(self.window, text= f"Insert a {table_name}")
        frame.grid(row=0, column=0, columnspan=3, pady=20)

        # Inputs creation
        self.entries = {}
        for index, column_name in enumerate(self.column_names):
            Label(frame, text=f"{column_name}: ").grid(row=index + 1, column=0)
            entry = Entry(frame)
            entry.grid(row=index + 1, column=1)
            self.entries[column_name] = entry

        # Button creation
        ttk.Button(frame, text=f'Save new {table_name}', command= self.insert_row).grid(row=len(self.column_names) + 1, column = 0, sticky=W + E)
        ttk.Button(frame, text=f'Delete {table_name}', command= self.delete_row).grid(row=len(self.column_names) + 1, column = 1, sticky=W + E)
        ttk.Button(frame, text=f'Update {table_name}', command = self.update_row).grid(row=len(self.column_names) + 1, column=2, sticky=W + E)

        # Control messages
        self.message = Label(text='', fg="red")
        self.message.grid(row=len(self.column_names) + 2, column=0, columnspan=2, sticky=W + E)

        # Display table parameters
        columns = [f"c{i}" for i in range(len(self.column_names))]
        self.tree = ttk.Treeview(column=tuple(columns), show='headings', height=8)
        self.tree.grid(row=len(self.column_names) + 3, column=0, columnspan=2)
        
        for index, column_name in enumerate(self.column_names):
            self.tree.column(f"# {index + 1}", anchor=CENTER)
            self.tree.heading(f"# {index + 1}", text=column_name)
        
        #Display information on created table
        self.get_rows()

ScheduleWindow.run_query = run_query
ScheduleWindow.validating_inputs = validating_inputs
ScheduleWindow.get_rows = get_rows
ScheduleWindow.insert_row = insert_row
ScheduleWindow.delete_row = delete_row
ScheduleWindow.update_row = update_row
ScheduleWindow.edit_row = edit_row   

        

class MenuWindow:
    def __init__(self, window):
        self.window = window
        self.window.configure(bg="green")
        self.window.title("Menu")
        self.window.geometry("300x300")

        self.create_menu_buttons()

    def create_menu_buttons(self):
        for idx, table in enumerate(get_tables()):
            table_button = ttk.Button(self.window, text=table,
                                      command=lambda t=table: self.open_table(t))
            table_button.grid(row=idx, column=0, padx=5, pady=5, sticky=W+E)

    def open_table(self, table_name):
        table_window = Toplevel(self.window)
        table_window.config(bg="black")
        column_names = get_column_names(table_name)
        ScheduleWindow( table_window, column_names, table_name)


    
if __name__ == "__main__":

    def get_column_names(table_name):
        with sqlite3.connect("schedule_manager.db") as connection:
            cursor = connection.cursor()
            cursor.execute(f"PRAGMA table_info({table_name})")
            columns_info = cursor.fetchall()
            column_names = [column_info[1] for column_info in columns_info]
        return column_names

    def get_tables():
        with sqlite3.connect("schedule_manager.db") as connection:
            cursor = connection.cursor()
            cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
            tables = cursor.fetchall()
        return [table[0] for table in tables]
 
#MAIN TO RUN THROUGH MENU (STILL BUGGING because table doesnt show up on new window)
    
    window = Tk()
    app = MenuWindow(window)
    window.mainloop()
    

#MAIN TO RUN THROUGH EACH TABLE (works better)
'''
    root = Tk()
    TABLE = "Classroom"
    column_names = get_column_names(TABLE)
    app = ScheduleWindow(root, column_names,TABLE)
    root.mainloop()
    '''

第二个主要用于独立运行每个窗口并且它有效。但是当通过菜单打开时,它一直在窃听。执行更改时,其他方法会更新表,但我认为这可能无关。

#updating the visual table (R in CRUD)
def get_rows(self):
    #Clear the zone before displaying
    records = self.tree.get_children()
    for element in records:
        self.tree.delete(element)

    query = f"Select * from {self.table_name}"
    db_rows = self.run_query(query)

    for row in db_rows:
        self.tree.insert('', 0, text=row[1], values=row)
python sqlite tkinter treeview ttk
© www.soinside.com 2019 - 2024. All rights reserved.