Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Create a GUI Marksheet using Tkinter
Next article icon

Prerequisites :Introduction to tkinter


Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Now, it’s up to the imagination or necessity of a developer, what he/she wants to develop using this toolkit.


To create a Tkinter : 

  • Importing the module – Tkinter
  • Create the main window (container)
  • Add any number of widgets to the main window.
  • Apply the event Trigger on the widgets.

The GUI would look like below:


Let’s create a GUI based simple Age Calculator application that can calculate the age with respect to the given date and birth date, given by the user.
 

Below is the implementation :

Python3
# import all functions from the tkinterfromtkinterimport*# import messagebox class from tkinterfromtkinterimportmessagebox# Function for clearing the# contents of all text entry boxesdefclearAll():# deleting the content from the entry boxdayField.delete(0,END)monthField.delete(0,END)yearField.delete(0,END)givenDayField.delete(0,END)givenMonthField.delete(0,END)givenYearField.delete(0,END)rsltDayField.delete(0,END)rsltMonthField.delete(0,END)rsltYearField.delete(0,END)# function for checking errordefcheckError():# if any of the entry field is empty# then show an error message and clear# all the entriesif(dayField.get()==""ormonthField.get()==""oryearField.get()==""orgivenDayField.get()==""orgivenMonthField.get()==""orgivenYearField.get()==""):# show the error messagemessagebox.showerror("Input Error")# clearAll function callingclearAll()return-1# function to calculate AgedefcalculateAge():# check for errorvalue=checkError()# if error is occur then returnifvalue==-1:returnelse:# take a value from the respective entry boxes# get method returns current text as stringbirth_day=int(dayField.get())birth_month=int(monthField.get())birth_year=int(yearField.get())given_day=int(givenDayField.get())given_month=int(givenMonthField.get())given_year=int(givenYearField.get())# if birth date is greater then given birth_month# then donot count this month and add 30 to the date so# as to subtract the date and get the remaining daysmonth=[31,28,31,30,31,30,31,31,30,31,30,31]if(birth_day>given_day):given_month=given_month-1given_day=given_day+month[birth_month-1]# if birth month exceeds given month, then# donot count this year and add 12 to the# month so that we can subtract and find out# the differenceif(birth_month>given_month):given_year=given_year-1given_month=given_month+12# calculate day, month, yearcalculated_day=given_day-birth_day;calculated_month=given_month-birth_month;calculated_year=given_year-birth_year;# calculated day, month, year write back# to the respective entry boxes# insert method inserting the# value in the text entry box.rsltDayField.insert(10,str(calculated_day))rsltMonthField.insert(10,str(calculated_month))rsltYearField.insert(10,str(calculated_year))# Driver Codeif__name__=="__main__":# Create a GUI windowgui=Tk()# Set the background colour of GUI windowgui.configure(background="light green")# set the name of tkinter GUI windowgui.title("Age Calculator")# Set the configuration of GUI windowgui.geometry("525x260")# Create a Date Of Birth : labeldob=Label(gui,text="Date Of Birth",bg="blue")# Create a Given Date : labelgivenDate=Label(gui,text="Given Date",bg="blue")# Create a Day : labelday=Label(gui,text="Day",bg="light green")# Create a Month : labelmonth=Label(gui,text="Month",bg="light green")# Create a Year : labelyear=Label(gui,text="Year",bg="light green")# Create a Given Day : labelgivenDay=Label(gui,text="Given Day",bg="light green")# Create a Given Month : labelgivenMonth=Label(gui,text="Given Month",bg="light green")# Create a Given Year : labelgivenYear=Label(gui,text="Given Year",bg="light green")# Create a Years : labelrsltYear=Label(gui,text="Years",bg="light green")# Create a Months : labelrsltMonth=Label(gui,text="Months",bg="light green")# Create a Days : labelrsltDay=Label(gui,text="Days",bg="light green")# Create a Resultant Age Button and attached to calculateAge functionresultantAge=Button(gui,text="Resultant Age",fg="Black",bg="Red",command=calculateAge)# Create a Clear All Button and attached to clearAll functionclearAllEntry=Button(gui,text="Clear All",fg="Black",bg="Red",command=clearAll)# Create a text entry box for filling or typing the information.dayField=Entry(gui)monthField=Entry(gui)yearField=Entry(gui)givenDayField=Entry(gui)givenMonthField=Entry(gui)givenYearField=Entry(gui)rsltYearField=Entry(gui)rsltMonthField=Entry(gui)rsltDayField=Entry(gui)# grid method is used for placing# the widgets at respective positions# in table like structure .dob.grid(row=0,column=1)day.grid(row=1,column=0)dayField.grid(row=1,column=1)month.grid(row=2,column=0)monthField.grid(row=2,column=1)year.grid(row=3,column=0)yearField.grid(row=3,column=1)givenDate.grid(row=0,column=4)givenDay.grid(row=1,column=3)givenDayField.grid(row=1,column=4)givenMonth.grid(row=2,column=3)givenMonthField.grid(row=2,column=4)givenYear.grid(row=3,column=3)givenYearField.grid(row=3,column=4)resultantAge.grid(row=4,column=2)rsltYear.grid(row=5,column=2)rsltYearField.grid(row=6,column=2)rsltMonth.grid(row=7,column=2)rsltMonthField.grid(row=8,column=2)rsltDay.grid(row=9,column=2)rsltDayField.grid(row=10,column=2)clearAllEntry.grid(row=12,column=2)# Start the GUIgui.mainloop()

Output : 
 


Age Calculator using Tkinter

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