Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Image Viewer App in Python using Tkinter
Next article icon

Tkinter is Python's standard GUI (Graphical User Interface) library andOpenPyXL is a module that allows for reading and writing Excel files. This guide shows you how to create a simple registration form with Tkinter, where users enter their details and those details are written into an Excel file. This form will automatically store the information in a pre-existing Excel file.

Steps to create the registration form

Python code

Python
fromopenpyxlimportload_workbookfromtkinterimport*wb=load_workbook('C:\\Users\\Admin\\Desktop\\excel.xlsx')ws=wb.active# Set header rowdefinit_excel():headers=["Name","Course","Semester","Form No.","Contact No.","Email","Address"]fori,hinenumerate(headers,1):ws.cell(row=1,column=i).value=hwb.save('C:\\Users\\Admin\\Desktop\\excel.xlsx')# Add form datadefinsert_data():ifall(f.get()forfinentries):row=ws.max_row+1fori,finenumerate(entries,1):ws.cell(row=row,column=i).value=f.get()wb.save('C:\\Users\\Admin\\Desktop\\excel.xlsx')clear_fields()else:print("Please fill all fields.")# Clear formdefclear_fields():forfinentries:f.delete(0,END)# Move to next fielddeffocus_next(entry):entry.focus_set()init_excel()root=Tk()root.title("Registration Form")root.geometry("500x300")root.config(bg="light green")labels=["Name","Course","Semester","Form No.","Contact No.","Email","Address"]entries=[Entry(root)for_inlabels]fori,lblinenumerate(labels):Label(root,text=lbl,bg="light green").grid(row=i+1,column=0)entries[i].grid(row=i+1,column=1,ipadx=100)ifi<len(labels)-1:entries[i].bind("<Return>",lambdae,nf=entries[i+1]:focus_next(nf))Button(root,text="Submit",fg="black",bg="red",command=insert_data).grid(row=len(labels)+1,column=1)root.mainloop()

Output
 



Explanation:

  • Loads an existing Excel file and sets up the active worksheet to store form data.
  • init_excel() writes column headers (Name, Course, etc.) in the first row of the Excel sheet.
  • Creates a Tkinter window with labeled input fields for user details like Name, Course, Contact, etc.
  • On clicking the "Submit" button, theinsert_data() checks if all fields are filled, then appends the data to the next row in the Excel file.
  • Pressing Enter moves to the next input field and after submission, fields are cleared using clear_fields().
  • The form window stays open using mainloop() until the user closes it.

Related Articles


Registration Form Using Python Tkinter
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp