What I want to do this time is to store the value entered in the text box from the GUI screen in an external file (a file for writing to CSV).
There are two files, one is the GUI file (tkinter.py) and the other is the executable (merge.py).
GUI functionality is limited to
1 text box
2 Get text box value button
3 Close Button
·I believe that the value of the text box has been obtained in word1, but the following error has occurred.
①writer.writerow(word1)
_csv.Error: iterable expected, not function
② writer.writerow(word1)
_csv.Error: iterable expected, not NoneType
·There is no particular meaning to divide the script into two, but the request is to divide it into two.
·As for the details of the CSV file, there is no specific decision, and I just want to write the text box value to the CSV.
The code is as follows:
sample_tkinter.py
import tkinter ask
root=tk.Tk()
root.geometry ("300x300")
entry=tk.Entry()
entry.place(x=20,y=30)
button=tk.Button(text="OK")
button.place (x=150, y=29)
word1=""
def click():
global word1
word1 = entry.get()
# entry.delete(0,tk,tk.END)
label=tk.Label(text=word1)
label.place(x=20,y=50)
button ["command" ] = click
def close_window():
root.destroy()
botton1=tk.Button(text="Close", command=close_window)
botton1.pack()
root.mainloop()
merge.py
import csv
from sample_tkinter import word1
with open("sample.csv", "w", encoding="shift-jis") ascsvfile:writer=csv.writer(csvfile, lineterminator="\n")
print(word1)
writer.writerow (word1)
The above has been resolved, but the CSV that I wrote is displayed with one cell and one string broken down.
For example, if the value of the text box is "Ayeo", on CSV,
A|B|C|D|E
Ahhhhhhhhhhhhhhhhhhhhhhhhhhh
It appears as above.
Please tell me how to solve this problem.
In this case, word1
is not the text entered, but contains the object of the function defined in def click():
.
This corresponds to word1=click
.
Therefore, if you specify the writer.writerow()
parameter for a string or list, you will get an error.
If you want to make minimal changes, you can use the following parts of sample_tkinter.py
:
def click():
word=entry.get()
entry.delete(0,tk.END)
label=tk.Label(text=word)
label.place(x=20,y=50)
word1 = click
You will change it like this. word1=click
is to delete it.
word1='"
def click():
global word1
word1 = entry.get()
entry.delete(0,tk.END)
label=tk.Label(text=word1)
label.place(x=20,y=50)
addition:
Whatever word1
contains, if you want to treat it as a single piece of data, use the following line of merge.py
:
writer.writerow (word1)
You can change it to this one.
writer.writerow ([word1])
However, if a comma (,
) is included in the string you enter, it will automatically result in a double quotation ("
), so you should know the specifications of Python's csv
, csvwriter
module.
© 2024 OneMinuteCode. All rights reserved.