import tkinter as tk from tkinter import ttk import pandas as pd import…
- User
import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox as MessageBox
def on_double_click(event): item = tree.selection()[0] value = tree.item(item, 'values')[2] # 获取原表格中的第二列(现在是第一列)的值 MessageBox.showinfo('提醒', f"单元格内容:{value}")
def sort_column(treeview, col, reverse): items = [treeview.item(item)['values'] for item in treeview.get_children()] items.sort(key=lambda x: x[col], reverse=reverse) for index, item in enumerate(items): treeview.item(treeview.get_children()[index], values=item) treeview.set(treeview.get_children()[index], column=0, value=index+1) if not reverse: treeview.heading(col, command=lambda _col=col: sort_column(treeview, _col, True)) else: treeview.heading(col, command=lambda _col=col: sort_column(treeview, _col, False))
file_path = 'results-1-day_sorted.xlsx' # 请替换为实际文件路径 df = pd.read_excel(file_path)
root = tk.Tk() root.title('Excel内容')
创建滚动条
scrollbar = tk.Scrollbar(root, orient='vertical')
创建TreeView并绑定滚动条
tree = ttk.Treeview(root, columns=['#0']+list(df.columns), show='headings', yscrollcommand=scrollbar.set) scrollbar.config(command=tree.yview)
添加列
tree.column('#0', width=10) # 设置序号列宽度 tree.heading('#0', text='序号')
for index, column in enumerate(df.columns): tree.column(index+1, width=100) # 设置每一列的宽度 tree.heading(index+1, text=column, command=lambda _col=index+1: sort_column(tree, _col, False))
添加数据行
for index, row in df.iterrows(): tree.insert('', 'end', values=(index+1, *row)) # 添加序号和数据行
tree.bind('', on_double_click) # 双击事件绑定
使用place定位Treeview
tree.place(x=20, y=200)
使用place定位滚动条
scrollbar.place(x=20 + tree.winfo_reqwidth(), y=20, height=tree.winfo_reqheight())
root.mainloop() 读取的文件中,只有8列,请修改代码能定义每一列的宽度
- Assistant
可以尝试将列宽度设置为列表,以便更直观地指定每一列的宽度。以下是修改后的代码: ```python import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox as MessageBox def on_double_click(event): item = tree.selection()[0] value = tree.item(item, 'values')[1] # 获取原表格中的第二列(现在是第一列)的值 MessageBox.showinfo('提醒', f"单元格内容:{value}") def sort_column(treeview, col, reverse): items = [treeview.item(item)['values'] for item in treeview.get_children()] items.sort(key=lambda x: x[col], reverse=reverse) for index, item in enumerate(items): treeview.item(treeview.get_children()[index], values=item) treeview.set(treeview.get_children()[index], column=0, value=index+1) if not reverse: treeview.heading(col, command=lambda _col=col: sort_column(treeview, _col, True)) else: treeview.heading(col, command=lambda _col=col: sort_column(treeview, _col, False)) file_path = 'results-1-day_sorted.xlsx' # 请替换为实际文件路径 df = pd.read_excel(file_path) root = tk.Tk() root.title('Excel内容') # 创建滚动条 scrollbar = tk.Scrollbar(root, orient='vertical') # 创建TreeView并绑定滚动条 tree = ttk.Treeview(root, columns=['#0']+list(df.columns), show='headings', yscrollcommand=scrollbar.set) scrollbar.config(command=tree.yview) # 添加列 tree.column('#0', width=10) # 设置序号列宽度 tree.heading('#0', text='序号') # 设置列宽度的列表 column_widths = [100, 200, 300, 100, 150, 80, 120, 90] for index, column in enumerate(df.columns): tree.column(index+1, width=column_widths[index]) # 设置每一列的宽度 tree.heading(index+1, text=column, command=lambda _col=index+1: sort_column(tree, _col, False)) # 添加数据行 for index, row in df.iterrows(): tree.insert('', 'end', values=(index+1, *row)) # 添加序号和数据行 tree.bind('<Double-1>', on_double_click) # 双击事件绑定 # 使用place定位Treeview tree.place(x=20, y=200) # 使用place定位滚动条 scrollbar.place(x=20 + tree.winfo_reqwidth(), y=20, height=tree.winfo_reqheight()) root.mainloop() - User
现在我不想在运行程序生成TK窗口时就显示表格的内容,而是想通过在TK窗口,点击一个按钮后,才显示
- Assistant
您可以创建一个按钮,将表格显示功能封装为一个函数,在点击按钮时调用该函数。以下是修改后的代码:
import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox as MessageBox def on_double_click(event): item = tree.selection()[0] value = tree.item(item, 'values')[1] # 获取原表格中的第二列(现在是第一列)的值 MessageBox.showinfo('提醒', f"单元格内容:{value}") def sort_column(treeview, col, reverse): items = [treeview.item(item)['values'] for item in treeview.get_children()] items.sort(key=lambda x: x[col], reverse=reverse) for index, item in enumerate(items): treeview.item(treeview.get_children()[index], values=item) treeview.set(treeview.get_children()[index], column=0, value=index+1) if not reverse: treeview.heading(col, command=lambda _col=col: sort_column(treeview, _col, True)) else: treeview.heading(col, command=lambda _col=col: sort_column(treeview, _col, False)) def display_table(): df = pd.read_excel(file_path) # 清空table for row in tree.get_children(): tree.delete(row) # 添加数据行 for index, row in df.iterrows(): tree.insert('', 'end', values=(index+1, *row)) # 添加序号和数据行 file_path = 'results-1-day_sorted.xlsx' # 请替换为实际文件路径 root = tk.Tk() root.title('Excel内容') # 创建滚动条 scrollbar = tk.Scrollbar(root, orient='vertical') # 创建TreeView并绑定滚动条 tree = ttk.Treeview(root, columns=['#0']+list(df.columns), show='headings', yscrollcommand=scrollbar.set) scrollbar.config(command=tree.yview) # 添加列 tree.column('#0', width=10) # 设置序号列宽度 tree.heading('#0', text='序号') # 设置列宽度的列表 column_widths = [100, 200, 300, 100, 150, 80, 120, 90] for index, column in enumerate(df.columns): tree.column(index+1, width=column_widths[index]) # 设置每一列的宽度 tree.heading(index+1, text=column, command=lambda _col=index+1: sort_column(tree, _col, False)) tree.bind('<Double-1>', on_double_click) # 双击事件绑定 # 使用place定位Treeview tree.place(x=20, y=200) # 使用place定位滚动条 scrollbar.place(x=20 + tree.winfo_reqwidth(), y=20, height=tree.winfo_reqheight()) # 添加按钮 button = tk.Button(root, text="显示表格", command=display_table) button.place(x=20, y=20) root.mainloop()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.