python 我想打开窗口显示html界面的内容,但是我把HTML和样式js,css都放在相同的路径,还是不能显示完整的HTML界面,这是我的代码,帮我修改一下,修改好完整的发给我
import tkinter as tk
import logging
import os
import sys
from tkinter import messagebox
# 配置日志记录
logging.basicConfig(level=logging.DEBUG, filename='app.log', format='%(asctime)s - %(levelname)s - %(message)s')
# 检查tkinterweb是否安装
try:
from tkinterweb import HtmlFrame
logging.debug("tkinterweb库导入成功")
except ImportError as e:
logging.error("tkinterweb库导入失败,请确保已安装: pip install tkinterweb", exc_info=True)
messagebox.showerror("导入错误", "tkinterweb库未安装,请运行 'pip install tkinterweb' 安装")
sys.exit(1)
except Exception as e:
logging.error(f"导入库时发生意外错误: {str(e)}", exc_info=True)
messagebox.showerror("导入错误", f"导入库时发生错误: {str(e)}")
sys.exit(1)
class DynamicFaceApp:
def __init__(self, root):
try:
logging.debug("开始初始化DynamicFaceApp")
self.root = root
self.root.title("软件卸载")
self.root.geometry("720x460")
self.root.configure(bg="#ccc")
self.root.resizable(False, False)
# 创建顶部标题栏
logging.debug("创建标题栏")
header_frame = tk.Frame(self.root, bg="#ccc", height=40)
header_frame.pack(fill=tk.X, padx=10)
header_frame.pack_propagate(False)
# 创建标题栏按钮
close_btn = tk.Button(header_frame, bg="#a43", width=2, height=1, bd=0, relief=tk.FLAT, command=self.root.quit)
close_btn.pack(side=tk.LEFT, padx=5)
minimize_btn = tk.Button(header_frame, bg="#cb3", width=2, height=1, bd=0, relief=tk.FLAT, command=lambda: self.root.iconify())
minimize_btn.pack(side=tk.LEFT, padx=5)
maximize_btn = tk.Button(header_frame, bg="#6a4", width=2, height=1, bd=0, relief=tk.FLAT)
maximize_btn.pack(side=tk.LEFT, padx=5)
# 创建标题
title = tk.Label(header_frame, text="软件卸载", bg="#ccc", font=('SimHei', 12))
title.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
# 创建主体内容
logging.debug("创建主体内容框架")
body_frame = tk.Frame(self.root, bg="#ccc")
body_frame.pack(expand=True, fill=tk.BOTH, padx=40, pady=20)
# 创建HTML显示组件
logging.debug("创建HTML渲染组件")
self.html_frame = HtmlFrame(body_frame, horizontal_scrollbar="auto", vertical_scrollbar="auto")
self.html_frame.pack(expand=True, fill=tk.BOTH)
# 尝试加载HTML文件的可能路径
possible_paths = [
os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "笑脸动态代码", "index.html")
]
html_path = None
for path in possible_paths:
if os.path.exists(path) and os.path.isfile(path):
html_path = path
break
if html_path:
logging.debug(f"找到HTML文件: {html_path}")
try:
logging.debug("尝试加载HTML文件")
html_content = open(html_path, 'r', encoding='utf-8').read()
base_dir = os.path.dirname(html_path)
# 替换相对路径为绝对路径
html_content = html_content.replace('href="styles.css"', f'href="file:///{base_dir.replace(os.sep, "/")}/styles.css"')
html_content = html_content.replace('src="script.js"', f'src="file:///{base_dir.replace(os.sep, "/")}/script.js"')
self.html_frame.load_html(html_content)
except Exception as e:
error_msg = f"加载HTML文件失败: {str(e)}"
logging.error(error_msg, exc_info=True)
self.show_error(body_frame, error_msg)
else:
error_msg = f"未找到HTML文件,已尝试路径: {possible_paths}"
logging.error(error_msg)
self.show_error(body_frame, error_msg)
except Exception as e:
error_msg = f"初始化界面失败: {str(e)}"
logging.error(error_msg, exc_info=True)
messagebox.showerror("初始化错误", error_msg)
self.root.after(5000, self.root.quit) # 显示错误后5秒自动退出
def show_error(self, parent, message):
"""显示错误信息"""
error_label = tk.Label(parent, text=message, bg="#ccc", fg="red", wraplength=400, font=('SimHei', 10))
error_label.pack(expand=True, padx=20, pady=20)
# 同时显示消息框确保用户看到错误
messagebox.showerror("加载错误", message)
if __name__ == "__main__":
try:
logging.debug("程序启动")
root = tk.Tk()
app = DynamicFaceApp(root)
root.mainloop()
except Exception as e:
error_msg = f"程序运行失败: {str(e)}"
logging.error(error_msg, exc_info=True)
messagebox.showerror("程序错误", error_msg)
print(error_msg)
# 让控制台保持打开状态以便查看错误
input("按Enter键退出...")
raise