Python Forum

Full Version: I need help for my python homework (getting the user input into a .txt file)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please help me, because this is for a project and it's very important!!! Wall Wall Wall

-Python version used:Python 3.12.5-


import tkinter as tk
    
def clear_form():
    name_entry.delete(0, tk.END)
    email_entry.delete(0, tk.END)
    contact_entry.delete(0, tk.END)
    age_entry.delete(0, tk.END)
    gender_menu.set("Gender")
    course_menu.set("Select a course")

window = tk.Tk()
window.title("Registration Form")

name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1)
tk.Label(window, text="Name:").grid(row=0, column=0)

email_entry = tk.Entry(window)
email_entry.grid(row=1, column=1)
tk.Label(window, text="Email:").grid(row=1, column=0)

contact_entry = tk.Entry(window)
contact_entry.grid(row=2, column=1)
tk.Label(window, text="Contact:").grid(row=2, column=0)

age_entry = tk.Entry(window)
age_entry.grid(row=3, column=1)
tk.Label(window, text="Age:").grid(row=3, column=0)

gender_menu = tk.StringVar(window)
gender_menu.set("Gender")
tk.OptionMenu(window, gender_menu, "Male", "Female").grid(row=4, column=0, columnspan=2)

course_menu = tk.StringVar(window)
course_menu.set("Select a course")
tk.OptionMenu(window, course_menu, "PY4Y", "PY4Y(Adv)", "AIFF", "Machine Learning").grid(row=5, column=0, columnspan=2)

tk.Button(window, text="Submit").grid(row=6, column=0)
tk.Button(window, text="Clear", command=clear_form).grid(row=6, column=1)

def submit_form():
    name = name_entry.get()
    email = email_entry.get()
    contact = contact_entry.get()
    age = age_entry.get()
    gender = gender_menu.get()
    course = course_menu.get()




window.mainloop()
--------------------------------------------------------------------------------------------------------------------------------------------------------------
I need to get the user input of those fields(run the code) into a .txt file. Please make it simple. Thanks in advance for those helping!
submit_form() should OPEN a file for Writing, and WRITE name, age, .. to the file
Learn about a button press callback https://dafarry.github.io/tkinterbook/button.htm
Just looking at saving the data for now: as deanhystad said, you need to open a file and save the data to a file.

Just using a name as an example:

# declare the save path
savepath = '/home/pedro/temp/user_data.txt'

# get the name
name = input('Hi! What is your name? ')

# a little function to write the name to the savepath
def write_data_2_drive(a_name):
    with open(savepath, 'a') as save_my_data:
        save_my_data.write(a_name)

# once you have the name, write it to the file
write_data_2_drive(name)
If I now look in my temp folder I find a file called user_data.txt it only has 1 name in it at the moment: Josef Stalin

As long as you open the savepath 'a' for 'append' you can keep adding data. If you open the savepath 'w' for 'write' you will wipe out out all existing data in the file!

Of course, if you had thousands of users, a databank is a better choice to save the data.
(Mar-24-2025, 02:05 PM)woooee Wrote: [ -> ]You know about emojis (useless and irritating) but don't seem to know about a button press callback https://dafarry.github.io/tkinterbook/button.htm Start paying attention in class.

Now be nice. This is not Stack Overflow (at least most of the time)
I missed that the submit button is not connected to the submit_form() function. The clear button is correctly connected to the clear_form function, so no indication of sleeping in class, just a mistake.

The gender_menu and course_menu should have labels like the Entry widgets, and the menus should default to the first selection. I would use StringVars with the Entry widgets. set("") is simpler than entry.delete(0, tk.end). Functions should be above the main body of code in the file, not inline with it.