【问题标题】:Is there good way to save text input from tkinter Text widget有没有从 tkinter Text 小部件保存文本输入的好方法
【发布时间】:2023-03-06 08:00:01
【问题描述】:

我正在构建一个在文本小部件中使用 Tkinter 的收据,它在 GUI 中格式正确

但是当我保存它时,它变成了这样

文本文件乱七八糟

如何在文本文件中获得类似于 GUI 中的文本格式的文本??? 这是我的代码。

from tkinter import *
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import askyesno

# ------------
app = Tk()
# =================== frames ========================
frame1 = Frame(app)
frame1.pack(side=TOP)
# ----
title_label = Label(frame1, text="Facture", font=("courier", 40, 'bold'))
title_label.pack()
# ----
frame2 = Frame(app, bd=5)
frame2.pack(side=TOP)
# ----
frame3 = Frame(app)
frame3.pack(side=TOP)
# ============================================

# ======--- text field -----======
text_input = Text(frame2, width=47, height=25, bg="aliceblue", fg='black', font=("arial", 14, 'bold'))
text_input.pack()
# ============ in text field ===============
text_input.insert(END, f'--------------------------------- Facture ---------------------------------\n')
text_input.insert(END, f"\nQty  " + f"Product" + f"                                 PU" + f"                 PV/TVAC" + f'\n')
text_input.insert(END, f"\n1      " + f"FANTA CITRON" + f"                      2500" + f"              2500")
text_input.insert(END, f"\n1      " + f"BUFFET PER HEAD" + f"              10000" + f"            10000")
text_input.insert(END, f"\n1      " + f"MUKEKE GRILLE OIGNONS" + f"16000" + f"            16000" + f'\n')
text_input.insert(END, f"\nTOTAL" + f"                                                                   28500")

# --------- functions ---------
def save():
    filepath = asksaveasfilename(
        defaultextension = "csv",
        filetypes = [("Text Files", "*.csv"), ("All Files", "*.*")])

    if not filepath:
        return

    with open(filepath, 'w') as output_file:
        text = text_input.get('1.0', END)
        output_file.write(text)

def printR():
    pass

def delete():
    text_input.delete('1.0', END)

def exit():
    iExit = askyesno("Attention", "You are on your way to quit\nAre you sure you want quit")
    if iExit > 0:
        app.destroy()
# ----------------------------------

# ============---------------===============
# =====--------- Buttons -----------========
save_button = Button(frame3, text="Save", height=3, width=10, command=save)
save_button.grid(row=0, column=0, padx=2)
# ----------------
print_button = Button(frame3, text="Print", height=3, width=10, command=printR)
print_button.grid(row=0, column=1, padx=2)
# ----------------
delete_button = Button(frame3, text="Delete", height=3, width=10, command=delete)
delete_button.grid(row=0, column=2, padx=2)
# ----------------
quit_button = Button(frame3, text="Exit", height=3, width=10, command=exit)
quit_button.grid(row=0, column=3, padx=2)
# ============================================

app.mainloop()   

…………………………………………………………………………………… ………………………………………………………………………………………………………… ………………………………………………………………………………………………………… .........................

【问题讨论】:

  • 为什么不在Text 小部件中使用等宽字体?
  • 或者尝试使用\t,它会留下一个空格。
  • 我已经尝试过 \t 但格式并不好,我的问题是我想知道如何在文本文件中获得与 GUI 中相同的输入

标签: python-3.x tkinter tkinter-text


【解决方案1】:

我认为问题在于“文本”的格式仅使用 Tkfixedfont 字体。

我做了一些更改,替换了格式类型,删除了粗体和大小,并使用了 format() 函数来对齐文本。

from tkinter import *
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import askyesno

# ------------
app = Tk()
# =================== frames ========================
frame1 = Frame(app)
frame1.pack(side=TOP)
# ----
title_label = Label(frame1, text="Facture", font=("courier", 40, 'bold'))
title_label.pack()
# ----
frame2 = Frame(app, bd=5)
frame2.pack(side=TOP)
# ----
frame3 = Frame(app)
frame3.pack(side=TOP)
# ============================================

# ======--- text field -----======
text_input = Text(frame2,bg="aliceblue", fg='black', font=('TkFixedFont'))
text_input.pack(expand = True)
# ============ in text field ===============
header = "{0} {1} {2}\n".format("-"*35,"Facture", "-"*35)
text_input.insert(END, header)
fields = "{0:10}{1:40}{2:20}{3:8}\n".format("Qty", "Product", "PU", "PV/TVAC")

text_input.insert(END, fields)
rs = (("1", "FANTA CITRON", "2500", "2500"),
        ("1", "BUFFET PER HEAD", "10000", "10000"),
        ("1", "MUKEKE GRILLE OIGNONS", "16000", "16000"),)

for i in rs:
    fields = "{0:10}{1:40}{2:20}{3:10}\n".format(i[0], i[1], i[2], i[3])
    text_input.insert(END, fields)
    
footer = "{0:70}{1}".format("TOTAL","28500")
text_input.insert(END, footer)


# --------- functions ---------
def save():
    filepath = asksaveasfilename(
        defaultextension = "csv",
        filetypes = [("Text Files", "*.csv"), ("All Files", "*.*")])

    if not filepath:
        return

    with open(filepath, 'w') as output_file:
        text = text_input.get('1.0', END)
        output_file.write(text)

def printR():
    pass

def delete():
    text_input.delete('1.0', END)

def exit():
    iExit = askyesno("Attention", "You are on your way to quit\nAre you sure you want quit")
    if iExit > 0:
        app.destroy()
# ----------------------------------

# ============---------------===============
# =====--------- Buttons -----------========
save_button = Button(frame3, text="Save", height=3, width=10, command=save)
save_button.grid(row=0, column=0, padx=2)
# ----------------
print_button = Button(frame3, text="Print", height=3, width=10, command=printR)
print_button.grid(row=0, column=1, padx=2)
# ----------------
delete_button = Button(frame3, text="Delete", height=3, width=10, command=delete)
delete_button.grid(row=0, column=2, padx=2)
# ----------------
quit_button = Button(frame3, text="Exit", height=3, width=10, command=exit)
quit_button.grid(row=0, column=3, padx=2)
# ============================================

app.mainloop()  

【讨论】:

  • 是的,伙计,它有效,谢谢,但我不明白这是什么意思“{0:10}{1:40}{2:20}{3:8}\n”跨度>
  • @red_ant 看看PyFormat
猜你喜欢
  • 2018-05-25
  • 2011-04-20
  • 2011-05-22
  • 2017-09-07
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多