import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
classApp(tk.Tk):def __init__(self):
super().__init__()
self.title("App-v1")
self.columnconfigure(0, weight =1)
self.rowconfigure(1, weight =1)Menu(self)Load_excel_data()defFile_dialog(self):"""This Function will open the file explorer and assign the chosen file path to label_file"""
filename = filedialog.askopenfilename(initialdir="/",
title="Select A File",
filetype=(("xlsx files","*.xlsx"),("All Files","*.*")))
label_file["text"]= filename
returnNonedefLoad_excel_data():"""If the file selected is valid this will load the file into the Treeview"""
file_path = label_file["text"]try:
excel_filename = r"{}".format(file_path)if excel_filename[-4:]==".csv":
df = pd.read_csv(excel_filename)else:
df = pd.read_excel(excel_filename)exceptValueError:
tk.messagebox.showerror("Information","The file you have chosen is invalid")returnNoneexceptFileNotFoundError:
tk.messagebox.showerror("Information", f"No such file as {file_path}")returnNoneclassMenu(ttk.Frame):def __init__(self, parent):
super().__init__(parent)
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.menu_button1 = ttk.Button(self, text ='Button 1',command=File_dialog)
self.menu_button1.grid(row =0, column =0, sticky ='e')
app =App()
app.mainloop()
السؤال
Ahmed Ahmed64
السلام عليكم
1 جواب على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.