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

ما سبب خطأ NameError: name 'button_event' is not defined عند استخدام مكتبة customtkinter

Ahmed Ahmed64

السؤال

السلام عليكم ورحمة الله وبركاته 

أين الخطأ في في كتابة هذا الكود

وماهي أسهل طريقة لتتبع الكود ومعرفة الخطأ

import customtkinter
class Frame1(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        self.checkbox_1 = customtkinter.CTkCheckBox(self, text="checkbox 1")
        self.checkbox_1.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="w")
        self.checkbox_2 = customtkinter.CTkCheckBox(self, text="checkbox 2")
        self.checkbox_2.grid(row=1, column=0, padx=10, pady=(10, 0), sticky="w")
        self.optionmenu = customtkinter.CTkOptionMenu(self, values=["option 1", "option 2"])
        self.optionmenu.grid(row=2, column=0, padx=10, pady=(10, 0), sticky="w")


class Frame2(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        self.button = customtkinter.CTkButton(self, text="فتح ملف", command=button_event)
        self.button.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="w")


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.title("my app")
        self.geometry("400x180")
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)

        self.Frame_A = Frame1(self)
        self.Frame_A.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="nsw")
        self.Frame_B = Frame2(self)
        self.Frame_B.grid(row=0, column=1, padx=10, pady=(10, 0), sticky="nsw")


    def button_event(Frame2):
        print("button_event")    

app = App()
app.mainloop()

 

تم التعديل في بواسطة Mustafa Suleiman
تعديل عنوان السؤال
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

رسالة الخطأ التي تظهر عند تشغيل الكود هي NameError: name 'button_event' is not defined وبالتالي هي تخبرك أن لديك متغير غير معرف.

وبالرجوع للمستند الرسمي الخاص بـ customtkinter وبالتحديد CTkButton نجد مثال يخبرك بالطريقة الصحيحة:

def button_event():
    print("button pressed")

button = customtkinter.CTkButton(app, text="CTkButton", command=button_event)

لاحظ أنه قام بتعريف دالة بنفس الاسم الذي يتم تمريره في command وبالتالي يتم تنفيذ ما بداخل الدالة عند الضغط على الزر.

عليك كتابة تلك الدالة

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

  • 0

الخطأ : NameError: name 'button_event' is not defined

 

import customtkinter


class Frame1(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        self.checkbox_1 = customtkinter.CTkCheckBox(self, text="checkbox 1")
        self.checkbox_1.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="w")
        self.checkbox_2 = customtkinter.CTkCheckBox(self, text="checkbox 2")
        self.checkbox_2.grid(row=1, column=0, padx=3, pady=(3, 0), sticky="w")
        self.optionmenu = customtkinter.CTkOptionMenu(self, values=["option 1", "option 2"])
        self.optionmenu.grid(row=2, column=0, padx=3, pady=(3, 0), sticky="we")


class Frame2(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        self.button = customtkinter.CTkButton(self, text="فتح ملف",font=customtkinter.CTkFont(family="Calibri" ,size=12, weight="bold"), command=button_event)
        self.button.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="we")
        self.optionmenu2 = customtkinter.CTkOptionMenu(self, values=["option 1", "option 2"])
        self.optionmenu2.grid(row=1, column=0, padx=3, pady=(3, 0), sticky="we")
        self.entry1 = customtkinter.CTkEntry(self, placeholder_text="إسم ورقة العمل",font=customtkinter.CTkFont(family="Calibri" ,size=12, weight="bold"),width=200)
        self.entry1.grid(row=2, column=0, padx=3, pady=(3, 0), sticky="we")

class Frame3(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        self.button2 = customtkinter.CTkButton(self, text="تشغيل",font=customtkinter.CTkFont(family="Calibri" ,size=12, weight="bold"))
        self.button2.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="w")
                


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.title("my app")
        self.geometry("400x180")
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)



        self.Frame_A = Frame1(self)
        self.Frame_A.grid(row=0, column=0, padx=1, pady=(1, 0), sticky="nswe")
        self.Frame_B = Frame2(self)
        self.Frame_B.grid(row=0, column=1, padx=1, pady=(1, 0), sticky="nsw")
        self.Frame_C = Frame3(self)
        self.Frame_C.grid(row=1, column=0, padx=1, pady=(1, 10), sticky="nsw")

    def button_event(self):
        print("button_event")    

app = App()
app.mainloop()

 

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

  • 0

معذرة أخي كل ما ذكر في تعليقك  تم تطبيقه إذا إين الخطأ في ذلك

هل الدالة صحيحة هكذا

def button_event(self): print("button_event")

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

  • 0

قم بكتابة دالة button_event كالتالي:

class Frame2(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        def button_event():
            print("button pressed")

        self.button = customtkinter.CTkButton(
            self, text="فتح ملف", command=button_event)
        self.button.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="w")

 

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

  • 0
بتاريخ 1 دقيقة مضت قال Mustafa Suleiman:

قم بكتابة دالة button_event كالتالي:

class Frame2(customtkinter.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        def button_event():
            print("button pressed")

        self.button = customtkinter.CTkButton(
            self, text="فتح ملف", command=button_event)
        self.button.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="w")

 

أخي أحتاج أن تكون الدالة داخل class App

def button_event():
            print("button pressed")

وليس كما فعلت حضرتك

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

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...