def show_data(self, sheet_name): try: # 读取工作表中A列所有数据 df = pd.read_excel(self.file_name, sheet_name=sheet_name, usecols=['A']) # 清空日志框 self.log_text.delete(1.0, tk.END) # 创建一个Frame来包含每个数据内容和相应的下拉框/滑动条 frame_list = [] for column_name in df.iloc[:, 0]: frame = tk.Frame(self.log_text) frame.pack(side=tk.TOP, fill=tk.X, padx=10, pady=(5, 0)) label = tk.Label(frame, text=f"{df.columns[0]}: ") label.pack(side=tk.LEFT) # 遍历每一行,获取该列的数据并更新下拉框/滑动条的取值范围 column_data = df[column_name] if isinstance(column_data.iloc[0], (int, float)): scale = ttk.Scale(frame, from_=0, to=column_data.max(), orient=tk.HORIZONTAL) scale.pack(side=tk.RIGHT, fill=tk.X, expand=True) elif isinstance(column_data.iloc[0], str): combobox = ttk.Combobox(frame, values=column_data.unique()) combobox.pack(side=tk.RIGHT, fill=tk.X, expand=True) frame_list.append(frame) except Exception as e: self.log_text.delete(1.0, tk.END) self.log_text.insert(tk.END, str(e) + "\n")修改為讀取第一列數據
时间: 2023-12-02 16:06:02 浏览: 95
修改后的代码如下:
```
def show_data(self, sheet_name):
try:
# 读取工作表中第一列所有数据
df = pd.read_excel(self.file_name, sheet_name=sheet_name, usecols=[0])
# 清空日志框
self.log_text.delete(1.0, tk.END)
# 创建一个Frame来包含每个数据内容和相应的下拉框/滑动条
frame_list = []
for column_name in df.iloc[:, 0]:
frame = tk.Frame(self.log_text)
frame.pack(side=tk.TOP, fill=tk.X, padx=10, pady=(5, 0))
label = tk.Label(frame, text=f"{df.columns[0]}: ")
label.pack(side=tk.LEFT)
# 遍历每一行,获取该列的数据并更新下拉框/滑动条的取值范围
column_data = df[column_name]
if isinstance(column_data.iloc[0], (int, float)):
scale = ttk.Scale(frame, from_=0, to=column_data.max(), orient=tk.HORIZONTAL)
scale.pack(side=tk.RIGHT, fill=tk.X, expand=True)
elif isinstance(column_data.iloc[0], str):
combobox = ttk.Combobox(frame, values=column_data.unique())
combobox.pack(side=tk.RIGHT, fill=tk.X, expand=True)
frame_list.append(frame)
except Exception as e:
self.log_text.delete(1.0, tk.END)
self.log_text.insert(tk.END, str(e) + "\n")
```
阅读全文