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

الرجاء مراجعة الكود والتعديل عليه

Ahmed Ahmed64

السؤال

السلام عليكم

في السابق قمت بعرض هذا الكود على حضراتكم ولاكن لم أجد الرد على تساؤلي بالطريقة التي أود أن تكون وهل يمكن كتابة الكود بهذه الكيفية

أولا أطرح عليكم الكود

import customtkinter
from tkinter import filedialog
from pathlib import Path
import pandas as pd

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)


        def openFile():
            filepath = filedialog.askopenfilename(initialdir="C:\\Users\\Cakow\\PycharmProjects\\Main",title="Open file okay?",filetypes= (("text files","*.xlsx"),("all files","*.*")))
            filepathA = Path(filepath)
            print(filepathA)
            df = pd.read_excel(filepathA,header=None) 

        

        self.button = customtkinter.CTkButton(self, text="فتح ملف",font=customtkinter.CTkFont(family="Calibri" ,size=12, weight="bold"),command=openFile)
        self.button.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="we")
        self.optionmenu2 = customtkinter.CTkOptionMenu(self, values=["Light", "Dark"])
        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)
          
        def button_event():
            print("button_event")

        self.button2 = customtkinter.CTkButton(self, text="تشغيل",font=customtkinter.CTkFont(family="Calibri" ,size=12, weight="bold"),command=button_event)
        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.customtkinter.set_appearance_mode("dark")


        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="nswe")
        self.Frame_C = Frame3(self)
        self.Frame_C.grid(row=1, column=0, padx=1, pady=(1, 10), sticky="nswe")
        
    

app = App()
app.mainloop()

هل يمكن عمل تعديل على الكود بحيث يتم إدراج هذه الدالة 

def openFile():
            filepath = filedialog.askopenfilename(initialdir="C:\\Users\\Cakow\\PycharmProjects\\Main",title="Open file okay?",filetypes= (("text files","*.xlsx"),("all files","*.*")))
            filepathA = Path(filepath)
            df = pd.read_excel(filepathA,header=None)

بحيث تكون الدالة السابقة داخل كلاس class App الموجود في الكود الذي بالأعلى 

علما أن كود button موجود في كلاس class Frame2

self.button = customtkinter.CTkButton(self, text="فتح ملف",font=customtkinter.CTkFont(family="Calibri" ,size=12, weight="bold"),command=openFile)
        self.button.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="we")

 

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

Recommended Posts

  • 0

الكود الذي قدمته يحتوي على بعض الأخطاء والمشاكل الصغيرة. إليك الكود بعد إجراء بعض التعديلات:
 

import tkinter as tk
from tkinter import filedialog
from pathlib import Path
import pandas as pd

class App(tk.Tk):
    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="nswe")
        self.Frame_C = Frame3(self)
        self.Frame_C.grid(row=1, column=0, padx=1, pady=(1, 10), sticky="nswe")

        self.button = tk.Button(self.Frame_B, text="فتح ملف", font=("Calibri", 12, "bold"), command=self.openFile)
        self.button.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="we")

    def openFile(self):
        filepath = filedialog.askopenfilename(initialdir="C:\\Users\\Cakow\\PycharmProjects\\Main", title="Open file okay?", filetypes=(("text files", "*.xlsx"), ("all files", "*.*")))
        filepathA = Path(filepath)
        df = pd.read_excel(filepathA, header=None)

class Frame1(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.checkbox_1 = tk.Checkbutton(self, text="checkbox 1")
        self.checkbox_1.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="w")
        self.checkbox_2 = tk.Checkbutton(self, text="checkbox 2")
        self.checkbox_2.grid(row=1, column=0, padx=3, pady=(3, 0), sticky="w")
        self.optionmenu = tk.OptionMenu(self, tk.StringVar(), "option 1", "option 2")
        self.optionmenu.grid(row=2, column=0, padx=3, pady=(3, 0), sticky="we")

class Frame2(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.optionmenu2 = tk.OptionMenu(self, tk.StringVar(), "Light", "Dark")
        self.optionmenu2.grid(row=1, column=0, padx=3, pady=(3, 0), sticky="we")
        self.entry1 = tk.Entry(self, font=("Calibri", 12, "bold"), width=20)
        self.entry1.grid(row=2, column=0, padx=3, pady=(3, 0), sticky="we")

class Frame3(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.button2 = tk.Button(self, text="تشغيل", font=("Calibri", 12, "bold"), command=self.button_event)
        self.button2.grid(row=0, column=0, padx=3, pady=(3, 0), sticky="w")

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

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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...