給這段代碼添加 儅用戶點擊按鈕時,將excel表名是出入庫明細數據顯示到第一個文本框中,表名是庫存明細的顯示到第二個文本框中,顯示是數據需要跟表格一樣有表格框並將數據置中,數據是空的就是空,不要顯示字符出來,日期就顯示日期,不要將日期後面的時間顯示出來並需要設置顯示數據的列框,跟表格是一樣的,文字居中,因數據過大需要加水平滾動條和垂直滾動條在文本框的右側和底部,請給出添加后的代碼
时间: 2024-04-02 07:33:05 浏览: 50
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)
# 创建水平滚动条和垂直滚动条
xscrollbar = tk.Scrollbar(text_frame, orient=tk.HORIZONTAL)
xscrollbar.pack(side=tk.BOTTOM, fill=tk.X)
yscrollbar = tk.Scrollbar(text_frame)
yscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 创建表格1
self.table1 = tk.Text(text_frame, wrap=tk.NONE, xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
self.table1.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
xscrollbar.config(command=self.table1.xview)
yscrollbar.config(command=self.table1.yview)
# 创建标签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)
# 创建水平滚动条和垂直滚动条
xscrollbar2 = tk.Scrollbar(text_frame2, orient=tk.HORIZONTAL)
xscrollbar2.pack(side=tk.BOTTOM, fill=tk.X)
yscrollbar2 = tk.Scrollbar(text_frame2)
yscrollbar2.pack(side=tk.RIGHT, fill=tk.Y)
# 创建表格2
self.table2 = tk.Text(text_frame2, wrap=tk.NONE, xscrollcommand=xscrollbar2.set, yscrollcommand=yscrollbar2.set)
self.table2.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
xscrollbar2.config(command=self.table2.xview)
yscrollbar2.config(command=self.table2.yview)
def show_excel(self):
# 清空文本框
self.table1.delete('1.0', tk.END)
self.table2.delete('1.0', tk.END)
# 获取表格1的列数和行数
rows = self.record_sheet.max_row
cols = self.record_sheet.max_column
# 设置表格1的列宽度
for i in range(1, cols + 1):
self.table1.columnconfigure(i, weight=1, minsize=50)
# 在表格1中显示数据
for row in range(1, rows + 1):
for col in range(1, cols + 1):
cell = self.record_sheet.cell(row=row, column=col)
value = cell.value
if value is None:
value = ""
elif type(value) == datetime.datetime:
value = value.date()
self.table1.insert(tk.END, str(value))
if col != cols:
self.table1.insert(tk.END, "\t")
self.table1.insert(tk.END, "\n")
# 获取表格2的列数和行数
rows = self.data_sheet.max_row
cols = self.data_sheet.max_column
# 设置表格2的列宽度
for i in range(1, cols + 1):
self.table2.columnconfigure(i, weight=1, minsize=50)
# 在表格2中显示数据
for row in range(1, rows + 1):
for col in range(1, cols + 1):
cell = self.data_sheet.cell(row=row, column=col)
value = cell.value
if value is None:
value = ""
elif type(value) == datetime.datetime:
value = value.date()
self.table2.insert(tk.END, str(value))
if col != cols:
self.table2.insert(tk.END, "\t")
self.table2.insert(tk.END, "\n")
# 设置表格1和表格2的对齐方式
self.table1.tag_configure("center", justify='center')
self.table2.tag_configure("center", justify='center')
# 设置表格1和表格2的边框
for i in range(1, cols + 1):
self.table1.tag_add("center", "1.%d" % ((i - 1) * 10), "end")
self.table2.tag_add("center", "1.%d" % ((i - 1) * 10), "end")
for i in range(1, rows + 1):
self.table1.tag_add("center", "%d.0" % (i), "%d.end" % (i))
self.table2.tag_add("center", "%d.0" % (i), "%d.end" % (i))
阅读全文