給這段代碼添加 儅用戶點擊按鈕時,將excel表名是出入庫明細數據顯示到第一個文本框中,表名是庫存明細的顯示到第二個文本框中,顯示是數據需要跟表格一樣顯示到文本框中,數據是空的就是空,不要顯示字符出來,日期就顯示日期,不要將日期後面的時間顯示出來,因數據過大需要加水平滾動條和垂直滾動條在文本框的右側和底部,請給出添加后的代碼
时间: 2024-03-23 09:36:56 浏览: 57
Excel模板库存出入库管理表.zip
好的,这里是添加了你要求的功能的代码:
```python
import tkinter as tk
from tkinter import ttk
import openpyxl
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('库存.xlsx')
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=tk.NONE)
self.result_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
x_scroll = tk.Scrollbar(text_frame, orient=tk.HORIZONTAL, command=self.result_text.xview)
x_scroll.pack(side=tk.BOTTOM, fill=tk.X)
y_scroll = tk.Scrollbar(self.container1, orient=tk.VERTICAL, command=self.result_text.yview)
y_scroll.pack(side=tk.RIGHT, fill=tk.Y)
self.result_text.config(xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.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=tk.NONE)
self.result_text2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
x_scroll2 = tk.Scrollbar(text_frame2, orient=tk.HORIZONTAL, command=self.result_text2.xview)
x_scroll2.pack(side=tk.BOTTOM, fill=tk.X)
y_scroll2 = tk.Scrollbar(self.container1, orient=tk.VERTICAL, command=self.result_text2.yview)
y_scroll2.pack(side=tk.RIGHT, fill=tk.Y)
self.result_text2.config(xscrollcommand=x_scroll2.set, yscrollcommand=y_scroll2.set)
def show_excel(self):
# 遍历出入库明细表格,获取所有的单元格内容,并添加到文本框中
for row in self.record_sheet.iter_rows():
line = ""
for cell in row:
if cell.value is None:
line += " "*15
elif type(cell.value) is str:
line += cell.value.ljust(15)
elif type(cell.value) is int:
line += str(cell.value).ljust(15)
elif type(cell.value) is float:
line += str(cell.value).ljust(15)
elif type(cell.value) is bool:
line += str(cell.value).ljust(15)
elif type(cell.value) is datetime.datetime:
line += cell.value.strftime('%Y-%m-%d').ljust(15)
else:
line += str(cell.value).ljust(15)
self.result_text.insert(tk.END, line + "\n")
# 遍历库存明细表格,获取所有的单元格内容,并添加到文本框中
for row in self.data_sheet.iter_rows():
line = ""
for cell in row:
if cell.value is None:
line += " "*15
elif type(cell.value) is str:
line += cell.value.ljust(15)
elif type(cell.value) is int:
line += str(cell.value).ljust(15)
elif type(cell.value) is float:
line += str(cell.value).ljust(15)
elif type(cell.value) is bool:
line += str(cell.value).ljust(15)
elif type(cell.value) is datetime.datetime:
line += cell.value.strftime('%Y-%m-%d').ljust(15)
else:
line += str(cell.value).ljust(15)
self.result_text2.insert(tk.END, line + "\n")
```
我们在`show_excel()`方法中遍历了出入库明细和库存明细两个表格,并将每个单元格的值添加到相应的文本框中,使用了滚动条来解决表格过大的问题。同时,我们对不同类型的数据进行了处理,以便正确地在文本框中显示。
阅读全文