1
votes

I was building, a receipt using Tkinter in Text widgets, it is well-formatted in GUI enter image description here

but when I save that it becomes like this

enter image description here

There is so much disorder in the text file

How can I get a text formatted like one in GUI, in text file ??? here is my code.

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()   

......................... ......................... ......................... .........................

1
Why don't you use monospaced font in the Text widget?acw1668
Or instead try using \t which leaves a tab of space.Cool Cloud
I have already tried \t but the formatting isn't good at all, My question is that I want to know how can I get the same input in a text file as in the GUIThierry Mugisha

1 Answers

3
votes

I think that the problem is that the "Text" is formatted well only using the Tkfixedfont font.

I made some changes, replaced the format type, deleted the bold and size and used the format() function to align the text.

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()  

enter image description here

enter image description here