Advertisement
Najeebsk

CSV-XLSX-READER.py

Mar 17th, 2024
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, filedialog
  3. import pandas as pd
  4.  
  5. def open_file():
  6.     file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv"), ("Excel files", "*.xlsx")])
  7.     if file_path:
  8.         if file_path.endswith('.csv'):
  9.             df = pd.read_csv(file_path)
  10.         elif file_path.endswith('.xlsx'):
  11.             df = pd.read_excel(file_path)
  12.         display_data(df)
  13.  
  14. def display_data(df):
  15.     # Creating a new Frame inside the root window
  16.     frame = ttk.Frame(root)
  17.     frame.pack(fill=tk.BOTH, expand=True)
  18.  
  19.     # Adding vertical scrollbar
  20.     scrollbar_y = ttk.Scrollbar(frame, orient=tk.VERTICAL)
  21.     scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
  22.  
  23.     # Adding horizontal scrollbar
  24.     scrollbar_x = ttk.Scrollbar(frame, orient=tk.HORIZONTAL)
  25.     scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X)
  26.  
  27.     # Adding Text widget
  28.     text = tk.Text(frame, yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
  29.     text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  30.  
  31.     # Configuring Scrollbars to work with Text widget
  32.     scrollbar_y.config(command=text.yview)
  33.     scrollbar_x.config(command=text.xview)
  34.  
  35.     # Inserting data into Text widget
  36.     text.insert(tk.END, df.to_string(index=False))
  37.  
  38.     # Configuring text color
  39.     text.config(fg="black", bg="white")
  40.  
  41. # Main GUI window
  42. root = tk.Tk()
  43. root.title("File Reader")
  44.  
  45. # Changing the background color of root window
  46. root.config(bg="lightgrey")
  47.  
  48. # Adding Open File button
  49. open_button = ttk.Button(root, text="Open File", command=open_file)
  50. open_button.pack(pady=10)
  51.  
  52. # Apply a new theme
  53. style = ttk.Style()
  54. style.theme_use("clam")
  55.  
  56. root.mainloop()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement