使用tkinter拖入excel文件并显示
- 效果
- 代码
效果
代码
import tkinter as tk
from tkinter import ttk
from tkinterdnd2 import TkinterDnD, DND_FILES
import pandas as pd
class ExcelViewerApp(TkinterDnD.Tk):
def __init__(self):
super().__init__()
self.title("Excel Viewer")
self.geometry("800x600")
self.drop_label = ttk.Label(self, text="Drag and drop an Excel file here")
self.drop_label.pack(pady=20)
self.tree = ttk.Treeview(self)
self.tree.pack(expand=True, fill='both')
self.drop_target_register(DND_FILES)
self.dnd_bind('<<Drop>>', self.drop)
def drop(self, event):
file_path = event.data.strip('{}')
if file_path.endswith(('.xls', '.xlsx')):
self.show_excel(file_path)
else:
self.drop_label.config(text="Please drop a valid Excel file")
def show_excel(self, file_path):
df = pd.read_excel(file_path)
self.tree.delete(*self.tree.get_children())
self.tree["columns"] = list(df.columns)
self.tree["show"] = "headings"
for column in self.tree["columns"]:
self.tree.heading(column, text=column)
for index, row in df.iterrows():
self.tree.insert("", "end", values=list(row))
self.drop_label.config(text="Drag and drop an Excel file here")
if __name__ == "__main__":
app = ExcelViewerApp()
app.mainloop()
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 使用tkinter拖入excel文件并显示
发表评论 取消回复