Search code examples
pythonwindowslistuser-inputthonny

How do I use tkinter to write into a txt document


we have a lot of code so far but we can't find anything to record info and send it to a txt document in a reciept style with all the categories. if there is a solution, we can't find it Here is a snippet of our code

#second window - rory
def new1():
 root = Tk( )
 root.config(bg=("#967117"))
 root.geometry( '1100x600+100+100') #where the box will be on the screen
 root.title("CheckOut")
 root.resizable (width=False, height=False)

 # address - rory
 Label (root, text = "Address:").place(relx = 0.3 , rely = 0.5)
 Label (root, text = "City:").place(relx = 0.3 , rely = 0.3)
 Label (root, text = "Province/Territory:").place(relx = 0.3 , rely = 0.1)
 Label (root, text = "Postal Code:").place(relx = 0.3 , rely = 0.7)
 Label (root, text = "Phone Number:").place(relx = 0.3 , rely = 0.9)
 depot7 = StringVar()
 depot7.set(None)
 OptionMenu (root, depot7, "British Columbia", "Alberta", "Saskatchewan", "Manitoba","Ontario","Yukon","Northwest Territories","Nunavut","Prince Edward Island","New Brunswick","Quebec","Nova Scotia","Newfoundland and Labrador").place(relx = 0.41 , rely = 0.09)
 address1 = Entry(root, width = 30).place(relx = 0.34 , rely = 0.3)
 address2 = Entry(root, width = 30).place(relx = 0.36 , rely = 0.5)
 address3 = Entry(root, width = 14).place(relx = 0.38 , rely = 0.7)
 pn = Entry(root, width = 18).place(relx = 0.39 , rely = 0.9)

 #payment - rory
 depot11 = StringVar()
 depot11.set(None)
 Label (root, text = "Type of Payment:").place(relx = 0.65 , rely = 0.1)
 OptionMenu (root, depot11, "VISA", "MASTERCARD", "AMERICAN EXPRESS", "DISCOVER").place(relx = 0.75 , rely = 0.09)
 Label (root, text = "Name On Card:").place(relx = 0.65 , rely = 0.3)
 Label (root, text = "Card Number:").place(relx = 0.65 , rely = 0.5)
 Label (root, text = "Expire Date (MM/YY):").place(relx = 0.65 , rely = 0.7)
 Label (root, text = "CVV:").place(relx = 0.65 , rely = 0.9)
 pay1 = Entry(root, width = 30).place(relx = 0.74 , rely = 0.3)
 pay2 = Entry(root, width = 30).place(relx = 0.73 , rely = 0.5)
 pay3 = Entry(root, width = 6).place(relx = 0.77 , rely = 0.7)
 pay4 = Entry(root, width = 6).place(relx = 0.69 , rely = 0.9)
 title = Label(root, text = "Cart", font = 40).place(relx = 0.14 , rely = 0.06)
 cart1 = tk.Text(root, width = 30, height = 28, state='disabled').place(relx = 0.05, rely = 0.1)
def dad():

 fileD = open("qqqq.txt", "a")
 fileD.write("Address:\n")
 fileD.write("%s\n" % address1.get())
 fileD.write("City:\n")
 fileD.write("%s\n" % address2.get())
 fileD.write("Province/Territory:\n")
 fileD.write("%s\n" % address3.get())
 fileD.write("Postal Code:\n")
 fileD.write("%s\n" % pn.get())

 Button(root, bg ="hot pink", text = "Add To Cart", font = 80, borderwidth = 10, relief= "ridge", command = dad).place(relx = 0.2 , rely = 0.2)

Solution

  • Learn python file operation. I will recommend you to visit this website for a clear understanding.

    https://www.google.com/amp/s/www.geeksforgeeks.org/file-handling-python/amp/