Python Forum
I need help for my python homework (getting the user input into a .txt file)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help for my python homework (getting the user input into a .txt file)
#1
Question 
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!
Larz60+ write Mar-23-2025, 11:19 AM:
Added BBCode tags.
Please use bbcode tags on future posts, Thank you.
Reply
#2
submit_form() should OPEN a file for Writing, and WRITE name, age, .. to the file
Reply
#3
Learn about a button press callback https://dafarry.github.io/tkinterbook/button.htm
buran write Apr-06-2025, 05:40 AM:
Post edited
Reply
#4
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.
Reply
#5
(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)
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,886 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Need help implementing an input with file operations Gaijin 3 2,803 Jun-08-2022, 05:50 PM
Last Post: Gaijin
  HELP in python homework makashito 4 4,819 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 2,489 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 3,162 May-09-2021, 11:19 AM
Last Post: Yoriz
  Print user input into triangle djtjhokie 1 3,105 Nov-07-2020, 07:01 PM
Last Post: buran
  Homework with python Johnsonmfw 1 2,209 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Changing Directory based on user input paulmerton4pope 13 12,066 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 5,670 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 3,601 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020