Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
File Explorer in Python using Tkinter
Next article icon

Prerequisites:Introduction toTkinter

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter outputs the fastest and easiest way to create GUI applications. In this article, we will learn how to create a GUI Calendar application using Tkinter, with a step-by-step guide. 

To create aTkinter: 

  • 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 Calendar application that can show the calendar with respect to the given year, given by the user.

Below is the implementation: 

Python3
# import all methods and classes from the tkinterfromtkinterimport*# import calendar moduleimportcalendar# Function for showing the calendar of the given yeardefshowCal():# Create a GUI windownew_gui=Tk()# Set the background colour of GUI windownew_gui.config(background="white")# set the name of tkinter GUI windownew_gui.title("CALENDAR")# Set the configuration of GUI windownew_gui.geometry("550x600")# get method returns current text as stringfetch_year=int(year_field.get())# calendar method of calendar module return# the calendar of the given year .cal_content=calendar.calendar(fetch_year)# Create a label for showing the content of the calendarcal_year=Label(new_gui,text=cal_content,font="Consolas 10 bold")# grid method is used for placing# the widgets at respective positions# in table like structure.cal_year.grid(row=5,column=1,padx=20)# start the GUInew_gui.mainloop()# Driver Codeif__name__=="__main__":# Create a GUI windowgui=Tk()# Set the background colour of GUI windowgui.config(background="white")# set the name of tkinter GUI windowgui.title("CALENDAR")# Set the configuration of GUI windowgui.geometry("250x140")# Create a CALENDAR : label with specified font and sizecal=Label(gui,text="CALENDAR",bg="dark gray",font=("times",28,'bold'))# Create a Enter Year : labelyear=Label(gui,text="Enter Year",bg="light green")# Create a text entry box for filling or typing the information.year_field=Entry(gui)# Create a Show Calendar Button and attached to showCal functionShow=Button(gui,text="Show Calendar",fg="Black",bg="Red",command=showCal)# Create a Exit Button and attached to exit functionExit=Button(gui,text="Exit",fg="Black",bg="Red",command=exit)# grid method is used for placing# the widgets at respective positions# in table like structure.cal.grid(row=1,column=1)year.grid(row=2,column=1)year_field.grid(row=3,column=1)Show.grid(row=4,column=1)Exit.grid(row=6,column=1)# start the GUIgui.mainloop()

Output : 

Code Explanation:

  1. The code starts by creating a new Tkinter window.
  2. The window has a white background and the title "CALENDAR".
  3. Next, the window's geometry is set to be 550x600 pixels.
  4. The calendar module is imported and the showCal() function is defined.
  5. This function will be used to display the calendar for the given year.
  6. The showCal() function first creates a GUI window and sets its background color to white.
  7. It also sets the title of the GUI window to "CALENDAR".
  8. Finally, it sets the configuration of the GUI window.
  9. Next, two labels are created: CALENDAR and ENTER YEAR.
  10. CALENDAR's text is set to be "CALENDAR", its background color is changed from white to dark gray, and its font size is set to 28 points bold.
  11. ENTER YEAR's text is set to be "Enter Year", its background color is changed from light green to red, and its font size is set to 10 points bold.
  12. A text entry box named year_field is created next and assigned as an attribute of year_field object instance .
  13. Finally, a Show Calendar button (with appropriate attributes) and an Exit button (with appropriate attributes) are created and
  14. The code creates a window and sets its background color to white.
  15. The window's title is set to "CALENDAR" and its geometry is set to 550x600.
  16. The calendar module is imported and the method calendar.calendar() is called with the given year as an argument.
  17. This returns the calendar for the given year.
  18. A label named cal_year is created and it has a grid with five rows and one column.
  19. The first row of the grid will be at position (5, 1), the second row at (4, 1), etc., until row 5 which will be at position (0, 0).
  20. Similarly, column 1 will be at position (0, 0), column 2 at (1, 0).
     

Build Your Own GUI CALENDAR with Tkinter in Python | Python Projects

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