含While/If语句时Python写入HTML文件空白问题求助
解决Python写入HTML时While/If语句导致文件空白的问题
我帮你找出问题所在啦!你的代码里有几个关键问题导致加入While/If后HTML文件变空白,咱们一步步来修复:
问题根源分析
函数提前退出,写入代码未执行
你在while循环里写了if counter >= len(list_of_items) - 1: return,这个return会直接结束整个invoice_creator_window函数,导致后面打开文件、写入HTML内容的代码完全没机会运行,自然HTML文件就是空白的。内容未累加,变量被覆盖
每次循环你都用html_formatting_mid = (...)重新赋值,而不是累加内容,最后只会保留最后一次循环的结果;如果列表为空,这个变量甚至会未定义,导致写入时报错。窗口对象被意外覆盖
你把按钮赋值给了invoice_created变量,这直接覆盖了之前创建的Toplevel窗口对象,虽然不是HTML空白的直接原因,但会导致后续GUI功能异常。Global声明位置错误
global html_formatting_mid放在while循环内部是无效的,而且这个变量根本不需要全局声明,函数内定义即可。
修复后的代码
下面是修正后的完整代码,我标注了关键修改点:
def invoice_creator_window(): global total_cost, total_cost_list, list_of_items, invoice_created invoice_created = Toplevel() invoice_created.focus_set() invoice_created.resizable(width=True, height=False) invoice_created.title("Invoice Created") invoice_created.geometry("300x300+400+400") invoice_created.configure(bg="limegreen") currentDisplay = 10 print(total_cost, total_cost_list, list_of_items) # 修改:按钮变量名改为done_btn,避免覆盖窗口对象 done = Label(invoice_created, text="Items have been purchased. Invoice has been created. Please check this program's file location.") done.grid(row=1, column=1, padx=7.5, pady=space_between) # 恢复Label的布局 done_btn = Button(invoice_created, text="Done", bg="white", command=close_window) done_btn.grid(row=2, column=1, padx=7.5, pady=space_between) # HTML生成部分 html_formatting_start = """<!DOCTYPE html> <html> <head> <title>Games R Us - Invoice</title> </head> <body> <style> body {background-color: #F7D358;} h1 {color: #775A03;} p {color: #000; border: 1px solid #775A03; padding: 15px; width: 650px} </style> <center> <h1>Games R Us - Invoice Document</h1>""" # 修改:初始化html_formatting_mid为空字符串,用于累加内容 html_formatting_mid = "" # 修改:用for循环遍历列表,更简洁且避免counter出错 for item, cost in zip(list_of_items, total_cost_list): html_formatting_mid += f""" <h3> <p> <img src="https://steamcdn-a.akamaihd.net/steam/apps/211420/header.jpg?t=1483694369"></img> <br>{item}<br> <i>${cost} AUD</i> </p> </h3>""" html_formatting_end = f""" <h2>In Total: ${total_cost} AUD</h2> <br> <b>Information Grabbed from These Links: </b> <a href="https://store.steampowered.com/explore/new/">Steam's New Releases</a> [LIVE] - <a href="https://store.steampowered.com/search/?filter=topsellers">Steam's Top Sellers</a> [LIVE] - <a href="https://www.umart.com.au/Headphones_147C.html">Umart's Headphones</a> [PRE-DOWNLOADED] - <a href="https://www.umart.com.au/Microphones_496C.html">Umart's Microphones</a> [PRE-DOWNLOADED] </center> </body> </html>""" # 修改:确保这段写入代码在循环执行完后运行,不会被提前return打断 with open(invoice_file, "w") as invoice_creation: invoice_creation.write(html_formatting_start) invoice_creation.write(html_formatting_mid) invoice_creation.write(html_formatting_end) # 原按钮代码保留 button_buy = Button(welcome_window, text="Buy", fg="white", bg="goldenrod", font=gui_font_10, command=invoice_creator_window)
关键修改说明
- 移除了导致函数提前退出的
return语句,改用for循环遍历列表,逻辑更清晰且不易出错 - 初始化
html_formatting_mid为空字符串,用+=累加每一项的HTML内容 - 重命名按钮变量,避免覆盖Toplevel窗口对象
- 使用
with语句打开文件,自动处理文件关闭,更安全 - 修复了CSS里
p {color: ;}的语法错误(添加了默认颜色#000) - 用f-string替代字符串拼接,代码更简洁易读
这样修改后,即使保留循环逻辑(这里用更健壮的for循环替代了原while),HTML文件也能正常写入所有预期内容啦!
内容的提问来源于stack exchange,提问作者Simone Rizzuto
相关产品推荐
相关产品推荐

