Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk, filedialog
- import pandas as pd
- def open_file():
- file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv"), ("Excel files", "*.xlsx")])
- if file_path:
- if file_path.endswith('.csv'):
- df = pd.read_csv(file_path)
- elif file_path.endswith('.xlsx'):
- df = pd.read_excel(file_path)
- display_data(df)
- def display_data(df):
- # Creating a new Frame inside the root window
- frame = ttk.Frame(root)
- frame.pack(fill=tk.BOTH, expand=True)
- # Adding vertical scrollbar
- scrollbar_y = ttk.Scrollbar(frame, orient=tk.VERTICAL)
- scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
- # Adding horizontal scrollbar
- scrollbar_x = ttk.Scrollbar(frame, orient=tk.HORIZONTAL)
- scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X)
- # Adding Text widget
- text = tk.Text(frame, yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
- text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- # Configuring Scrollbars to work with Text widget
- scrollbar_y.config(command=text.yview)
- scrollbar_x.config(command=text.xview)
- # Inserting data into Text widget
- text.insert(tk.END, df.to_string(index=False))
- # Configuring text color
- text.config(fg="black", bg="white")
- # Main GUI window
- root = tk.Tk()
- root.title("File Reader")
- # Changing the background color of root window
- root.config(bg="lightgrey")
- # Adding Open File button
- open_button = ttk.Button(root, text="Open File", command=open_file)
- open_button.pack(pady=10)
- # Apply a new theme
- style = ttk.Style()
- style.theme_use("clam")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement