我試圖將一個視窗的內容轉移到一個外部檔案上,但當我這樣做時,它顯示的專案數量不完整。我怎樣才能使rec_content顯示所有的內容呢?
代碼:
get_content = listbox2.get(0, END) # Get the items selected by user
bb = Label(receipt_window, text="Bert's Burgers")
line1 = Label(receipt_window, text='----------------------------------------------------------------')
bb.pack()
line1.pack()
for con_item in get_content: # 將專案分成兩個變數,以格式化文本。
con1, con2 = con_item.split('PHP'/span>)
rec_content = f'{con1:<30}costs PHP{con2:<8}'
receipt = Label(receipt_window, text=rec_content)
receipt.pack()
item_list = [str(rec_content), "Cost" str(cost), "Cash Payed" payment. get(), "Change" str(change_formula)]
with open(the receipt.txt", "w") as f:
for line in item_list:
f.write(line)
f.write("
")
uj5u.com熱心網友回復:
一個非常簡單和方便的格式化文本的方法是使用一個三段式引號字串。
這使你能夠快速地組織或修改你的收據。
下面是一個示范。
這里有一個演示。
con1 = 1
con2=130
con3 = 2
con4 =100
con5=3
con6 = 75310
變化 = 付款 - 成本
收據 = f""
伯特的漢堡包
{con1} pc(s) Vegan Burger費用為PHP {con2}。
{con3} pc(s) Big Fries costs PHP {con4}.
{con5} pc(s) French Fries costs PHP {con6}。
----------------------------------------------------------------
總成本。PHP{cost}
投入的現金。PHP{payment}
變化。PHP{change}
"""
with open(the receipt.txt", "wt") as f:
f.write(receive)
with open(the receipt.txt", "rt") as f:
收據 = f.read()
print(receive)
uj5u.com熱心網友回復:
你的問題與上一個問題類似。你在for回圈中使用了相同的變數rec_content,然后在for回圈外將這個變數添加到item_list。 所以item_list將擁有for回圈中的最后一個專案。
你需要在for回圈中把rec_content追加到item_list中:
item_list = [] 。
for con_item in get_content:
con1, con2 = con_item.split('PHP'/span>)
rec_content = f'{con1:<30}costs PHP{con2:<8}'
receipt = Label(receipt_window, text=rec_content)
receipt.pack()
item_list.append(rec_content)
item_list = [f'Cost {cost}'。
f'Cash Paid{payment.get()}'。
f'Change{change_formula}']
with open(' receipt.txt', 'w') as f:
f.write('
'.join(item_list))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/319450.html
標籤:



