diff --git a/Simple_Enterprise_Warehouse_Management.py b/Simple_Enterprise_Warehouse_Management.py index 7a12586..74f86b0 100644 --- a/Simple_Enterprise_Warehouse_Management.py +++ b/Simple_Enterprise_Warehouse_Management.py @@ -265,7 +265,7 @@ class InventoryUI: page_size_combo.bind("<>", self.on_page_size_change) # 统计信息 - self.query_stats_label = tk.Label(bottom_frame, text="总重量: 360.0kg 总价: 11680.00元") + self.query_stats_label = tk.Label(bottom_frame, text="查询结果: 0条记录 净重量: 0.00kg 总价值: 0.00元") self.query_stats_label.pack(side=tk.RIGHT) # 返回按钮 @@ -299,8 +299,8 @@ class InventoryUI: self.product_tree.insert("", tk.END, values=( product_name, - f"{weight:.8f}", - f"{price:.8f}", + f"{weight:.2f}", + f"{price:.2f}", "详情" )) @@ -827,16 +827,17 @@ class InventoryUI: # 计算全部记录的统计信息 all_total_weight = 0 all_total_value = 0 - for transaction in self.filtered_transactions: - weight = float(transaction["weight"]) - price = float(transaction["price"]) - total_price = weight * price - - if transaction["type"] == "入库": - all_total_weight += weight - else: - all_total_weight -= weight - all_total_value += total_price + if self.filtered_transactions: # 只有当有筛选结果时才计算 + for transaction in self.filtered_transactions: + weight = float(transaction["weight"]) + price = float(transaction["price"]) + total_price = weight * price + + if transaction["type"] == "入库": + all_total_weight += weight + else: + all_total_weight -= weight + all_total_value += total_price # 更新统计信息 self.query_stats_label.config(text=f"查询结果: {self.total_records}条记录 净重量: {all_total_weight:.2f}kg 总价值: {all_total_value:.2f}元") @@ -859,7 +860,7 @@ class InventoryUI: try: self.page_size = int(self.page_size_var.get()) self.current_page = 1 # 重置到第一页 - if hasattr(self, 'filtered_transactions') and self.filtered_transactions: + if hasattr(self, 'filtered_transactions'): self._display_current_page() except ValueError: pass @@ -972,7 +973,7 @@ class InventoryUI: return True def print_query_results(self): - """打印查询结果""" + """生成HTML文件并用浏览器打开打印""" try: # 获取当前查询结果 results = [] @@ -984,68 +985,184 @@ class InventoryUI: messagebox.showwarning("提示", "没有查询结果可以打印") return - # 创建打印预览窗口 - print_window = tk.Toplevel(self.root) - print_window.title("打印预览") - print_window.geometry("800x600") - print_window.resizable(True, True) - - # 创建文本框显示打印内容 - text_frame = tk.Frame(print_window) - text_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) - - text_widget = tk.Text(text_frame, wrap=tk.NONE, font=("Courier", 10)) - scrollbar_y = tk.Scrollbar(text_frame, orient=tk.VERTICAL, command=text_widget.yview) - scrollbar_x = tk.Scrollbar(text_frame, orient=tk.HORIZONTAL, command=text_widget.xview) - text_widget.config(yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set) - - scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y) - scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X) - text_widget.pack(fill=tk.BOTH, expand=True) - - # 生成打印内容 + # 生成HTML内容 from datetime import datetime - current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + import os + import webbrowser + import tempfile - print_content = f"""库存管理系统 - 查询结果报表 -生成时间: {current_time} -{'='*80} - + current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + stats_text = self.query_stats_label.cget("text") + + html_content = f""" + + + + + 库存管理系统 - 查询结果报表 + + + + +
+
+

库存管理系统 - 查询结果报表

+
生成时间: {current_time}
+
+ + + + + + + + + + + + + + """ + + # 添加数据行 + for values in results: + row_class = "inbound" if values[2] == "入库" else "outbound" + html_content += f""" + + + + + + + + """ - # 表头 - header = f"{'时间':<20} {'产品名':<15} {'类型':<8} {'重量(kg)':<10} {'价格(元/kg)':<12} {'总价(元)':<12} {'备注':<20}" - print_content += header + "\n" - print_content += "-" * 80 + "\n" + html_content += f""" +
时间产品名类型重量(kg)价格(元/kg)总价(元)备注
{values[0]}{values[1]}{values[2]}{values[3]}{values[4]}{values[5]}{values[6]}
+ +
+

统计信息

+

{stats_text}

+

记录总数: {len(results)}条

+
+
+ +""" - # 数据行 - for values in results: - line = f"{values[0]:<20} {values[1]:<15} {values[2]:<8} {values[3]:<10} {values[4]:<12} {values[5]:<12} {values[6]:<20}" - print_content += line + "\n" + # 创建HTML文件到软件根目录 + current_dir = os.path.dirname(os.path.abspath(__file__)) + html_file = os.path.join(current_dir, f"{datetime.now().strftime('%Y%m%d')}_{len(results)}条.html") - # 统计信息 - stats_text = self.query_stats_label.cget("text") - print_content += "\n" + "="*80 + "\n" - print_content += f"统计信息: {stats_text}\n" - print_content += f"记录总数: {len(results)}条\n" + with open(html_file, 'w', encoding='utf-8') as f: + f.write(html_content) - text_widget.insert(tk.END, print_content) - text_widget.config(state=tk.DISABLED) + # 用浏览器打开HTML文件 + webbrowser.open(f'file:///{html_file.replace(os.sep, "/")}') - # 按钮框架 - button_frame = tk.Frame(print_window) - button_frame.pack(pady=10) - - def copy_to_clipboard(): - print_window.clipboard_clear() - print_window.clipboard_append(print_content) - messagebox.showinfo("成功", "内容已复制到剪贴板") - - tk.Button(button_frame, text="复制到剪贴板", command=copy_to_clipboard, bg="lightblue").pack(side=tk.LEFT, padx=5) - tk.Button(button_frame, text="关闭", command=print_window.destroy).pack(side=tk.LEFT, padx=5) + messagebox.showinfo("成功", f"报表已生成并在浏览器中打开\n文件位置: {html_file}") except Exception as e: - messagebox.showerror("错误", f"生成打印预览失败: {str(e)}") + messagebox.showerror("错误", f"生成HTML报表失败: {str(e)}") def run(self): """运行应用"""