When you write the value of the tkinter text box to csv, the label is also included.

Asked 2 years ago, Updated 2 years ago, 80 views

import tkinter ask
import d_list

tl=tk.Tk()
tl.geometry ('600x600')
tl.title ("Registration Monitor")
# Buttons Configuration
defbot1_1():
    import csv
    detail.append(txt1.get())
    detail.append(txt2.get())
    detail.append(txt3.get())
    detail.append(txt4.get())

    with open('deta.csv', 'a') ascsvFile:

        writer=csv.writer(csvFile)
        writer.writerow (detaset)

    csvFile.close()

detail = [ ] 

# label
lab1=tk.Label(text="Product registration", font=(",40))
lab1.place(x=20,y=30)
lab2=tk.Label(text="ID", font=(",30))
lab2.place(x=20,y=120)
lab3=tk.Label(text="item name", font=(",30))
lab3.place(x=20,y=190)
lab4=tk.Label(text="quantity", font=(",30))
lab4.place(x=20,y=260)
lab5=tk.Label(text="Register", font=(",30))
lab5.place(x=20,y=330)

# text box
txt1 = tk.Entry (font=(", 30), width=15)
txt1.place(x=200,y=120)
txt2=tk.Entry(font=(",30), width=15)
txt2.place (x=200, y=190)
txt3=tk.Entry(font=(",30), width=15)
txt3.place (x=200, y=260)
txt4 = tk.Entry (font=(", 30), width=15)
txt4.place (x=200, y=330)

# button
bot1=tk.Button(tl, text="Registration", font=(",40), width=5, command=bot1_1)
bot1.place(x=120,y=450)
bot2=tk.Button(tl, text="End", font=(",40), width=5)
bot2.place(x=320,y=450)

tl.mainloop()

python python3 tkinter

2022-09-30 15:56

1 Answers

The question is only the title and source code, and the title seems to have been cut off in the middle, so I guess the question is as follows.

@Mr. Fumu7 As you mentioned in the comment, the following content may have already been labeled data.csv before the program in question was activated.

Why is it that when I write the value of the tkinter text box to csv, the label is also included?

On the contrary, the following questions may be asked.

How do I put labels together when I write values in the tkinter text box to csv?

In this case, it is recommended that you do not write a label (header:item name) because the csv file is opened in add mode 'a' in the process of the "Register" button as shown below.

with open('data.csv', 'a')ascsvFile:

Instead of doing something with the program, you should have a single line of data.csv files with the following information written in advance.

ID, item name, quantity, subscriber

If necessary, enclose it with ".

 "ID", "Product Name", "Quantity", "Register"

For example, if you register one item, it looks like this.

ID, item name, quantity, subscriber
12345, 500ml pack of milk, 5, store manager

By the way, in addition to the questions, there are the following issues on the source code:

  • detaset=[] is defined as a global variable and cleared only at the beginning, so if you register multiple cases without exiting the program, data is added to the back of each line, resulting in an abnormal file.
  • It is useless to import csv every time the
  • Registration button process runs.
  • Due to the default mode when the file is opened, one blank line is written for each line (on Windows?).
  • with and open, so csvFile.close() is not required.
  • importThe d_list appears to be a proprietary module and does not appear to be using any of the questions, so it is not necessary for question articles.
  • Whether it is in the middle of creation or a transcription error, there is no program termination process with the "End" button.

So we can do the following.

  • import csv moves out of defbot1_1(): to the top
  • detaset=[] moves to the beginning of the defbot1_1(): process as a local variable rather than as a global variable
  • (on Windows?) The csv file opens as with open('data.csv', 'a', newline=')ascsvFile: so that line breaks do not double.
  • csvFile.close() should be deleted (put return instead)
  • Write a program exit action with the "Finish" button


2022-09-30 15:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.