import tkinter as tk from tkinter import ttk import pandas as pd def readfile():…
- User
import tkinter as tk from tkinter import ttk import pandas as pd
def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx")
# Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', values=(index + 1, *row))def sort_table(col, order): data = [tree.item(child)['values'] for child in tree.get_children()] data.sort(key=lambda x: x[col], reverse=not order)
# Update the table with the sorted data for i, item in enumerate(data): tree.item(item[0], values=(i + 1, *item[1:])) tree.heading(col, command=lambda: sort_table(col, not order))def on_double_click(event): item_id = tree.focus() cell_value = tree.item(item_id)['values'][1] tk.messagebox.showinfo('Cell value', cell_value)
root = tk.Tk()
Radio buttons
radio_var = tk.StringVar(value='1-day') for text in ['day', '1-hour', '4-hour', '30-minute']: tk.Radiobutton(root, text=text, variable=radio_var, value=text).pack(side='left')
Read button
tk.Button(root, text='Read', command=read_file).pack(side='left')
Table
columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f', 'g') column_widths = [50, 100, 100, 100, 100, 100, 100, 100]
tree = ttk.Treeview(root, columns=columns, show='headings') tree.bind('', on_double_click)
Add headings and set their sort function
for i, (col, width) in enumerate(zip(columns, column_widths)): tree.column(col, width=width) tree.heading(col, text=col, command=lambda col=i: sort_table(col, True))
tree.pack()
root.mainloop()此代码在排序和弹窗上出错
- Assistant
我已经修复了弹窗问题,并修改了排序功能,现在应该可以正常使用了。以下是修复后的完整代码:
import tkinter as tk from tkinter import ttk import pandas as pd def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx") # Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', iid=index+1, values=(index + 1, *row)) def sort_table(col, order): data = [(tree.item(child)['iid'], tree.item(child)['values']) for child in tree.get_children()] data.sort(key=lambda x: x[1][col], reverse=not order) # Update the table with the sorted data for index, (iid, item) in enumerate(data): tree.item(iid, values=(index + 1, *item[1:])) tree.heading(col, command=lambda: sort_table(col, not order)) def on_double_click(event): item_id = tree.focus() if item_id: cell_value = tree.item(item_id)['values'][1] tk.messagebox.showinfo('Cell value', cell_value) root = tk.Tk() # Radio buttons radio_var = tk.StringVar(value='1-day') for text in ['day', '1-hour', '4-hour', '30-minute']: tk.Radiobutton(root, text=text, variable=radio_var, value=text).pack(side='left') # Read button tk.Button(root, text='Read', command=read_file).pack(side='left') # Table columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f', 'g') column_widths = [50, 100, 100, 100, 100, 100, 100, 100] tree = ttk.Treeview(root, columns=columns, show='headings') tree.bind('<Double-1>', on_double_click) # Add headings and set their sort function for i, (col, width) in enumerate(zip(columns, column_widths)): tree.column(col, width=width) tree.heading(col, text=col, command=lambda col=i: sort_table(col, True)) tree.pack() root.mainloop() - User
排序出错data = [(tree.item(child)['iid'], tree.item(child)['values']) for child in tree.get_children()];弹窗功能加了个引入库就正常了:import tkinter.messagebox
- Assistant
非常感谢您的反馈。非常抱歉关于排序的问题没有完全解决。经过进一步检查,我提供了一个修复方案。我已经修改了排序功能以及加入了
import tkinter.messagebox。现在,请尝试以下修复后的代码:import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx") # Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', iid=index+1, values=(index + 1, *row)) def sort_table(col, order): data = [tree.item(child) for child in tree.get_children()] data.sort(key=lambda x: x['values'][col], reverse=not order) # Update the table with the sorted data for index, item in enumerate(data): tree.item(item['iid'], values=(index + 1, *item['values'][1:])) tree.heading(col, command=lambda: sort_table(col, not order)) def on_double_click(event): item_id = tree.focus() if item_id: cell_value = tree.item(item_id)['values'][1] tkinter.messagebox.showinfo('Cell value', cell_value) root = tk.Tk() # Radio buttons radio_var = tk.StringVar(value='1-day') for text in ['day', '1-hour', '4-hour', '30-minute']: tk.Radiobutton(root, text=text, variable=radio_var, value=text).pack(side='left') # Read button tk.Button(root, text='Read', command=read_file).pack(side='left') # Table columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f', 'g') column_widths = [50, 100, 100, 100, 100, 100, 100, 100] tree = ttk.Treeview(root, columns=columns, show='headings') tree.bind('<Double-1>', on_double_click) # Add headings and set their sort function for i, (col, width) in enumerate(zip(columns, column_widths)): tree.column(col, width=width) tree.heading(col, text=col, command=lambda col=i: sort_table(col, True)) tree.pack() root.mainloop() - User
还是无法排序
- Assistant
抱歉给您带来困扰。这里是一个新的修复版本,其中针对排序和更新表格的功能进行了优化。请试一下这个版本:
import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx") # Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', iid=index+1, values=(index + 1, *row)) def sort_treeview(treeview, col, reverse): data = [(treeview.set(child, col), child) for child in treeview.get_children()] data.sort(reverse=reverse) for index, (_val, child) in enumerate(data): treeview.move(child, '', index) treeview.heading(col, command=lambda: sort_treeview(treeview, col, not reverse)) def on_double_click(event): item_id = tree.focus() if item_id: cell_value = tree.item(item_id)['values'][1] tkinter.messagebox.showinfo('Cell value', cell_value) root = tk.Tk() # Radio buttons radio_var = tk.StringVar(value='1-day') for text in ['day', '1-hour', '4-hour', '30-minute']: tk.Radiobutton(root, text=text, variable=radio_var, value=text).pack(side='left') # Read button tk.Button(root, text='Read', command=read_file).pack(side='left') # Table columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f', 'g') column_widths = [50, 100, 100, 100, 100, 100, 100, 100] tree = ttk.Treeview(root, columns=columns, show='headings') tree.bind('<Double-1>', on_double_click) # Add headings and set their sort function for i, (col, width) in enumerate(zip(columns, column_widths)): tree.column(col, width=width) tree.heading(col, text=col, command=lambda col=i: sort_treeview(tree, col, False)) tree.pack() root.mainloop() - User
排序正常了,但index这列我希望只是起到序号的作用,不参与排序
- Assistant
非常抱歉刚才的实现没有充分理解您的需求。为了让“index”列仅作为序号使用,请尝试以下修复后的代码:
import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx") # Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', iid=index+1, values=(index + 1, *row)) def sort_treeview(treeview, col, reverse): if col == 0: # ignore the index column return data = [(treeview.set(child, col), child) for child in treeview.get_children()] data.sort(reverse=reverse) for index, (_val, child) in enumerate(data): treeview.move(child, '', index) tree.set(child, '#1', index + 1) # update the index column treeview.heading(col, command=lambda: sort_treeview(treeview, col, not reverse)) def on_double_click(event): item_id = tree.focus() if item_id: cell_value = tree.item(item_id)['values'][1] tkinter.messagebox.showinfo('Cell value', cell_value) root = tk.Tk() # Radio buttons radio_var = tk.StringVar(value='1-day') for text in ['day', '1-hour', '4-hour', '30-minute']: tk.Radiobutton(root, text=text, variable=radio_var, value=text).pack(side='left') # Read button tk.Button(root, text='Read', command=read_file).pack(side='left') # Table columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f', 'g') column_widths = [50, 100, 100, 100, 100, 100, 100, 100] tree = ttk.Treeview(root, columns=columns, show='headings') tree.bind('<Double-1>', on_double_click) # Add headings and set their sort function for i, (col, width) in enumerate(zip(columns, column_widths)): tree.column(col, width=width) tree.heading(col, text=col, command=lambda col=i: sort_treeview(tree, col, False)) tree.pack() root.mainloop() - User
排序有问题,表格数据为“50%”这种字符时,排序出错
- Assistant
非常抱歉您遇到了排序问题。下面的代码已经修复了这个问题。在进行排序之前,现在会从字符串中剔除百分号,并将百分比数值转换为浮点数以便正确排序。
这是修复后的完整代码:
import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx") # Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', iid=index+1, values=(index + 1, *row)) def sort_treeview(treeview, col, reverse): if col == 0: # ignore the index column return data = [(treeview.set(child, col), child) for child in treeview.get_children()] # Remove "%" and convert to float data = [(float(value.rstrip("%")), child) for value, child in data] data.sort(reverse=reverse) for index, (_val, child) in enumerate(data): treeview.move(child, '', index) tree.set(child, '#1', index + 1) # update the index column treeview.heading(col, command=lambda: sort_treeview(treeview, col, not reverse)) def on_double_click(event): item_id = tree.focus() if item_id: cell_value = tree.item(item_id)['values'][1] tkinter.messagebox.showinfo('Cell value', cell_value) root = tk.Tk() # Radio buttons radio_var = tk.StringVar(value='1-day') for text in ['day', '1-hour', '4-hour', '30-minute']: tk.Radiobutton(root, text=text, variable=radio_var, value=text).pack(side='left') # Read button tk.Button(root, text='Read', command=read_file).pack(side='left') # Table columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f', 'g') column_widths = [50, 100, 100, 100, 100, 100, 100, 100] tree = ttk.Treeview(root, columns=columns, show='headings') tree.bind('<Double-1>', on_double_click) # Add headings and set their sort function for i, (col, width) in enumerate(zip(columns, column_widths)): tree.column(col, width=width) tree.heading(col, text=col, command=lambda col=i: sort_treeview(tree, col, False)) tree.pack() root.mainloop() - User
少显示一列
- Assistant
为了从表格中移除一列(例如最后一列 "g"),您需要在创建表格时移除该列的名称和宽度,同时确保读取的数据中也不包含该列。
这里是一个将最后一列 "g" 从表格中移除的示例:
import tkinter as tk from tkinter import ttk import pandas as pd import tkinter.messagebox def read_file(): filename = radio_var.get() data = pd.read_excel(f"results-{filename}.xlsx") data = data.iloc[:, :-1] # Remove the last column from the data # Clear the existing table for row in tree.get_children(): tree.delete(row) # Update the table with the new data for index, row in data.iterrows(): tree.insert('', 'end', iid=index+1, values=(index + 1, *row)) # ... (The rest of the code is unchanged) ... # Table columns = ('index', 'a', 'b', 'c', 'd', 'e', 'f') # Remove 'g' from the columns column_widths = [50, 100, 100, 100, 100, 100, 100] # Remove the width for column 'g' # ... (The rest of the code is unchanged) ...这段代码从原始数据(
data)中移除了最后一列,并从表格中移除了列名称 "g" 和对应的列宽。现在表格将仅显示前七列,并在删除一列的同时仍然正常排序。请尝试运行并确保它满足您的需求。如果您有任何问题,请随时告诉我。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.