給這段代碼添加 儅用戶點擊按鈕時,將excel表名是出入庫明細數據顯示到第一個文本框中,表名是庫存明細的顯示到第二個文本框中,顯示時數據需要設置每列的寬度到文本框中,數據是空的就是空,不要顯示字符出來,日期就顯示日期,不要將日期後面的時間顯示出來,因數據過大需要加水平滾動條和垂直滾動條在文本框的右側和底部,請給出添加后的代碼
时间: 2024-04-02 17:32:41 浏览: 81
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)
# 创建标签
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,
width=30,
height=10,
xscrollcommand=True,
yscrollcommand=True)
self.result_text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.result_text.tag_configure("header", font=("Arial", 12, "bold"))
# 创建标签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,
width=30,
height=10,
xscrollcommand=True,
yscrollcommand=True)
self.result_text2.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.result_text2.tag_configure("header", font=("Arial", 12, "bold"))
def show_excel(self):
# 清空文本框中的内容
self.result_text.delete("1.0", tk.END)
self.result_text2.delete("1.0", tk.END)
# 设置列宽
for col in range(1, self.record_sheet.max_column + 1):
self.result_text.columnconfigure(col, minsize=100)
for col in range(1, self.data_sheet.max_column + 1):
self.result_text2.columnconfigure(col, minsize=100)
# 显示出入庫明細
for row in self.record_sheet.iter_rows(min_row=1, max_row=1):
for cell in row:
self.result_text.insert(tk.END, cell.value.ljust(20), "header")
self.result_text.insert(tk.END, "\n")
for row in self.record_sheet.iter_rows(min_row=2):
for cell in row:
if isinstance(cell.value, datetime.datetime):
self.result_text.insert(
tk.END, cell.value.date().strftime("%Y-%m-%d").ljust(20))
else:
self.result_text.insert(tk.END, str(cell.value).ljust(20))
self.result_text.insert(tk.END, "\n")
# 显示庫存明細
for row in self.data_sheet.iter_rows(min_row=1, max_row=1):
for cell in row:
self.result_text2.insert(tk.END, cell.value.ljust(20), "header")
self.result_text2.insert(tk.END, "\n")
for row in self.data_sheet.iter_rows(min_row=2):
for cell in row:
if isinstance(cell.value, datetime.datetime):
self.result_text2.insert(
tk.END, cell.value.date().strftime("%Y-%m-%d").ljust(20))
else:
self.result_text2.insert(tk.END, str(cell.value).ljust(20))
self.result_text2.insert(tk.END, "\n")
# 添加水平和垂直滚动条
self.result_text.config(
xscrollcommand=self.result_text.xview,
yscrollcommand=self.result_text.yview)
self.result_text2.config(
xscrollcommand=self.result_text2.xview,
yscrollcommand=self.result_text2.yview)
阅读全文