اذهب إلى المحتوى
  • 0

ماحل مشكلة tkinter.TclError: window ".!menu" isn't a top-level window

Ahmed Ahmed64

السؤال

ما مشكلة هذا الخطأ

_tkinter.TclError: window ".!menu" isn't a top-level window

الكود كامل هنا

import customtkinter
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
from pathlib import Path

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.title("App-v1")
        self.grid_columnconfigure(0, weight = 1)
        self.grid_rowconfigure(1, weight = 1)
        Menu(self)
        
    def File_dialog(self):
        
        self.filename = filedialog.askopenfilename(initialdir="C:\\Users\\Cakow\\PycharmProjects\\Main", title="Open file okay?", filetypes=(("text files", "*.xlsx"),("all files", "*.*")))
        self.label_file["text"] = self.filename
        return None

    def Load_excel_data(self):
        
        file_path = self.label_file["text"]
        try:
            excel_filename = r"{}".format(file_path)
            if excel_filename[-4:] == ".csv":
                df = pd.read_csv(excel_filename)
            else:
                df = pd.ExcelFile(excel_filename)
                self.optionmenu1.configure(values = df.sheet_names)
                self.label2["text"] = df.sheet_names
                  
                
                
              
        except ValueError:
            tk.messagebox.showerror("Information", "The file you have chosen is invalid")
            return None
        except FileNotFoundError:
            tk.messagebox.showerror("Information", f"No such file as {file_path}")
            return None

class Menu(customtkinter.CTkFrame,App):
    def __init__(self, master):
        super().__init__(master)
        
        self.grid(row=0, column=0, padx=(10, 10), pady=(10, 10), sticky="e")
        self.columnconfigure(0, weight = 1)
        self.rowconfigure(1, weight = 1)
        self.create_widgets()

    def create_widgets(self):
        self.button1 = customtkinter.CTkButton(self, text = 'Button 1',command=self.File_dialog)
        self.button1.grid(row = 0, column = 0, sticky = 'e')
        self.label_file = ttk.Label(self, text="")
        self.label_file.grid(row = 1, column = 0, sticky = 'e')

        self.button2 = customtkinter.CTkButton(self, text = 'Button 1',command=self.Load_excel_data)
        self.button2.grid(row = 2, column = 0, sticky = 'e')
        self.label2 = ttk.Label(self, text="")
        self.label2.grid(row = 3, column = 0, sticky = 'e')

        self.optionmenu1 = customtkinter.CTkOptionMenu(self,values=["إختر ورقة العمل"], font=customtkinter.CTkFont(family="Calibri", size=12, weight="bold"))
        self.optionmenu1.grid(row=4, column=0, padx=(10, 10), pady=(10, 10), sticky="e")
       
app = App()
app.mainloop()

 

تم التعديل في بواسطة Ahmed Ahmed64
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يظهر هذا الخطأ عندما يكون هناك تضارب في إستخدام Tk و Toplevel في تصميم الواجهة الرسومية، و الظاهر أن الخطأ قد يكون ناتجا عن توريث الفئة Menu من customtkinter.CTkFrame و App في الوقت نفسه، مما يؤدي إلى إنشاء نافذة رئيسية و Toplevel في نفس الوقت.

لتجنب هذا الخطأ، يمكنك إزالة الوراثة المتعددة واستخدام واجهة المستخدم الرئيسية App كنافذة رئيسية واستخدام Menu كـ Toplevel إذا كنت بحاجة إلى نافذة فرعية.

و هذا الكود المصحح:

import customtkinter
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
from pathlib import Path

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.title("App-v1")
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        Menu(self)

    def File_dialog(self):
        self.filename = filedialog.askopenfilename(initialdir="C:\\Users\\Cakow\\PycharmProjects\\Main",
                                                   title="Open file okay?", filetypes=(("text files", "*.xlsx"),
                                                                                        ("all files", "*.*")))
        self.label_file["text"] = self.filename
        return None

    def Load_excel_data(self):
        file_path = self.label_file["text"]
        try:
            excel_filename = r"{}".format(file_path)
            if excel_filename[-4:] == ".csv":
                df = pd.read_csv(excel_filename)
            else:
                df = pd.ExcelFile(excel_filename)
                self.optionmenu1.configure(values=df.sheet_names)
                self.label2["text"] = df.sheet_names

        except ValueError:
            tk.messagebox.showerror("Information", "The file you have chosen is invalid")
            return None
        except FileNotFoundError:
            tk.messagebox.showerror("Information", f"No such file as {file_path}")
            return None

class Menu(tk.Toplevel):
    def __init__(self, master):
        super().__init__(master)
        self.title("Menu")
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.create_widgets()

    def create_widgets(self):
        self.button1 = customtkinter.CTkButton(self, text='Button 1', command=self.master.File_dialog)
        self.button1.grid(row=0, column=0, sticky='e')
        self.label_file = ttk.Label(self, text="")
        self.label_file.grid(row=1, column=0, sticky='e')

        self.button2 = customtkinter.CTkButton(self, text='Button 2', command=self.master.Load_excel_data)
        self.button2.grid(row=2, column=0, sticky='e')
        self.label2 = ttk.Label(self, text="")
        self.label2.grid(row=3, column=0, sticky='e')

        self.optionmenu1 = customtkinter.CTkOptionMenu(self, values=["إختر ورقة العمل"],
                                                       font=customtkinter.CTkFont(family="Calibri", size=12,
                                                                                 weight="bold"))
        self.optionmenu1.grid(row=4, column=0, padx=(10, 10), pady=(10, 10), sticky="e")

app = App()
app.mainloop()

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...