I am not able to write into a file in python Tkinter project, the snippet of the code is given below | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I am not able to write into a file in python Tkinter project, the snippet of the code is given below

def submit(): with open(f"{m_f}", "a") as f: f.write(f"{us_d.get()}") all the above mentioned variables are defined but still I am not able to see the entry in my file.

11th Jul 2021, 3:25 PM
😇Abhinaya😇
😇Abhinaya😇 - avatar
8 Answers
+ 4
I wrote an answer for the actual issue, but it didn't go through. Short answer = You need to pass the container 'food' to the StringVar() constructor when creating the variable, otherwise it will be associated with the root container by default. Label(food, text=f"What did {a} eat?").pack() us_d = StringVar(food) Entry(food, textvariable=us_d).pack() def submit(): with open(m_f, "a") as f: f.write(f"{us_d.get()}\n") Also, added \n so each new entry would go on a new line.
12th Jul 2021, 1:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 9
Abhinaya , maybe you have some doubts in sharing your code with us. but for the community it is not easy to give you helpful hints by not having all informations we need.. it would be nice if you could share your code with us, this will avoid gathering all the small bits and pieces we need to get an idea what the issue is. thanks for understanding! BTW: you have not done a python tutorial upto now here in Sololearn. the way the file will be opened in your code is a bit unusual. where did you learn that ?
11th Jul 2021, 4:24 PM
Lothar
Lothar - avatar
+ 4
AFAIK Python string doesn't have a get() method. I don't have any idea what a `StringVar` class or function is. Maybe you can explain that part Did you get any error message running that snippet? it may help others in answering your question.
11th Jul 2021, 3:49 PM
Ipang
+ 4
Change this portion of your code in your if statement to get it working; ... # Label line is unchanged but added here for code location reference Label(food, text=f"What did {a} eat?").pack() food_en = Entry(food) food_en.pack() def submit(): val = food_en.get() with open(m_f, 'a') as f: f.write(val) ... I removed the f-strings in the file context manager due to string redundancy (they already are strings!!). I think the problem has to do with reference accesses. .get() was always returning an empty string. it works for the global references (us_n) because, well, they're global. Where the food entry object belonged inside of the food window. I'm not certain this is the issue as of yet, need to review the documentation for tk.Entry and its text argument, but changing the code to maintain a reference to the Entry object and calling the .get() method on it directly works as desired. Even using the text argument to set to a variable here didn't work.
12th Jul 2021, 12:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
What is that <us_d> variable? a dictionary? You need to pass something (item key) to the get() method to get something in return, where it's possible to be a `None` object when such key isn't present in the dictionary.
11th Jul 2021, 3:30 PM
Ipang
+ 2
ChaoticDawg, your answer has solved my problem. Thank you very much.😄😄 Ipang , Lothar thanks, I got my solution 😃!
12th Jul 2021, 5:08 AM
😇Abhinaya😇
😇Abhinaya😇 - avatar
+ 1
Hi Ipang The variable <us_d> is not a dictionary but a StringVar().
11th Jul 2021, 3:38 PM
😇Abhinaya😇
😇Abhinaya😇 - avatar
+ 1
Hi Lothar, My Code goes like this from tkinter import * import tkinter.messagebox as tmsg root = Tk() root.geometry("1000x500") root.title("Health Management") def check(): a = us_n.get() # Name Value f_w = us_f_w.get() # Diet or Workout value names = ["Harry", "Hermione"] if a in names: tmsg.askyesno("Confirmation", f"You want to enter the {f_w} details of the user {a} ?") w_f = f"{a}_workout.txt" m_f = f"{a}_diet.txt" if f_w == "diet": food = Tk() food.geometry("300x300") food.title("Diet Entry") Label(food, text=f"What did {a} eat?").pack() us_d = StringVar() Entry(food, text=us_d).pack() val = us_d.get() def submit(): with open(f"{m_f}", "a") as f: f.write(f"{us_d.get()}") Button(food, text="submit", command=submit).pack() Button(food, text="Done", command=food.destroy).pack() food.mainloop() else: print("workout") else: tmsg.showwarning("Alert", "User not registered") def register(): pass Label(root, text="Welcome to Health Mangement System", font="consolas 30 bold").pack() n_var = Label(root, text="Name", font="georgia 10 bold").place(x=10, y=100) f_var = Label(root, text="Diet Entry", font="georgia 10 bold").place(x=10, y=135) w_var = Label(root, text="Workout Entry", font="georgia 10 bold").place(x=10, y=170) us_n = StringVar() us_f_w = StringVar() us_f_w.set("1") n_en = Entry(root, text=us_n).place(x=60, y=100) radio_en = Radiobutton(root, value="diet", variable=us_f_w).place(x=100, y=135) radio_en = Radiobutton(root, value="workout", variable=us_f_w).place(x=125, y=170) Button(root, text="Enter", font="georgia 10 bold", command=check).place(x=10, y=205) Button(root, text="New User", font="georgia 10 bold", command=register).place(x=100, y=205) root.mainloop()
11th Jul 2021, 5:10 PM
😇Abhinaya😇
😇Abhinaya😇 - avatar