給這段代碼添加 儅用戶點擊按鈕時,將excel表名是出入庫明細數據顯示到第一個文本框中,表名是庫存明細的顯示到第二個文本框中,顯示是數據需要跟表格一樣有表格框並將數據置中,數據是空的就是空,不要顯示字符出來,日期就顯示日期,不要將日期後面的時間顯示出來並需要設置顯示數據的列框,因數據過大需要加水平滾動條和垂直滾動條在文本框的右側和底部,請給出添加后的代碼
时间: 2024-04-02 20:32:58 浏览: 19
class InventoryApp:
def __init__(self, master):
self.master = master
master.title("物料进出库统计")
master.state('zoomed') # 窗口最大化
# 创建右侧的工具栏
toolbar_frame = ttk.Frame(master, width=20)
toolbar_frame.pack(side='right', fill='y')
# 创建底部的工具栏
bottom_frame = ttk.Frame(master, height=20)
bottom_frame.pack(side='bottom', fill='x')
# 创建左侧面板
self.container = tk.Frame(master, width=100, bg='lightcyan')
self.container.pack(side=tk.LEFT, fill=tk.BOTH)
# 创建工具栏
toolbar = tk.Frame(master, height=0.2)
self.selected_label = tk.Label(toolbar, text="", fg="red", font=("Arial", 12))
self.selected_label.pack(side=tk.LEFT, padx=0.02, pady=0.02)
# 创建左上方面板
self.container_top = tk.Frame(self.container, width=100, bg='lightcyan')
self.container_top.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# 创建左下方面板
self.container_bottom = tk.Frame(self.container, width=100, bg='lightcyan')
self.container_bottom.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
# 创建右侧面板
self.container1 = tk.Frame(master)
self.container1.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 打开Excel文件
self.wb = openpyxl.load_workbook(庫存)
self.record_sheet = self.wb["出入庫明細"]
self.data_sheet = self.wb["庫存明細"]
# 添加显示excel内容的按钮
self.show_button = tk.Button(self.container_bottom, text="显示Excel内容", command=self.show_excel)
self.show_button.grid(row=12, column=0, columnspan=2, padx=5, pady=5)
# 创建底部工具栏
bottom_toolbar = tk.Frame(master, bg='white')
bottom_toolbar.pack(side=tk.BOTTOM, fill=tk.X)
# 创建标签1
label = tk.Label(self.container1, text="出入庫明細")
label.pack(side=tk.TOP)
# 创建文本框1
text_frame = tk.Frame(self.container1)
text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.result_text = tk.Text(text_frame, wrap="none")
self.result_text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# 添加水平滚动条和垂直滚动条
hscrollbar = tk.Scrollbar(text_frame, orient=tk.HORIZONTAL, command=self.result_text.xview)
hscrollbar.pack(side=tk.BOTTOM, fill=tk.X)
vscrollbar = tk.Scrollbar(text_frame, orient=tk.VERTICAL, command=self.result_text.yview)
vscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.result_text.configure(xscrollcommand=hscrollbar.set, yscrollcommand=vscrollbar.set)
# 创建标签2
label2 = tk.Label(self.container1, text="庫存明細")
label2.pack(side=tk.TOP)
# 创建文本框2
text_frame2 = tk.Frame(self.container1)
text_frame2.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
self.result_text2 = tk.Text(text_frame2, wrap="none")
self.result_text2.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# 添加水平滚动条和垂直滚动条
hscrollbar2 = tk.Scrollbar(text_frame2, orient=tk.HORIZONTAL, command=self.result_text2.xview)
hscrollbar2.pack(side=tk.BOTTOM, fill=tk.X)
vscrollbar2 = tk.Scrollbar(text_frame2, orient=tk.VERTICAL, command=self.result_text2.yview)
vscrollbar2.pack(side=tk.RIGHT, fill=tk.Y)
self.result_text2.configure(xscrollcommand=hscrollbar2.set, yscrollcommand=vscrollbar2.set)
def show_excel(self):
# 显示出入库明细
headers = [cell.value for cell in self.record_sheet[1]]
data = []
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
data.append([str(cell) if isinstance(cell, datetime.datetime) else cell for cell in row])
self.show_table(self.result_text, headers, data)
# 显示库存明细
headers = [cell.value for cell in self.data_sheet[1]]
data = []
for row in self.data_sheet.iter_rows(min_row=2, values_only=True):
data.append([str(cell) if isinstance(cell, datetime.datetime) else cell for cell in row])
self.show_table(self.result_text2, headers, data)
def show_table(self, text_widget, headers, data):
# 清空文本框
text_widget.delete('1.0', 'end')
# 添加表头
header_row = ' | '.join(str(header).center(20) for header in headers)
text_widget.insert('end', header_row + '\n')
text_widget.insert('end', '-' * len(header_row) + '\n')
# 添加表格数据
for row in data:
row_str = ' | '.join(str(cell).center(20) if cell != None else ' '.center(20) for cell in row)
text_widget.insert('end', row_str + '\n')
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)