stay Python in , We can use Tkinter Make GUI. If you are very imaginative and creative , You can use it. Tkinter Make a lot of interesting things . ad locum , We will use Tkinter To make a Python The calendar GUI Applications . In this application , The user must enter the year you want to view the calendar , Then the calendar will appear .
Use Tkinter Of The calendar GUI Applications
This article is a series of articles , Three in total python Entry project . Beginners can try to implement these projects , And in Python Hands on operation in the compilation environment .
First, use the following command to install Tkinter:
pip install tk Copy code
We also need a calendar package , But we don't have to install it , It is python The default package comes with .
First, import. calendar Module and tkinter modular
import calendar from tkinter import * Copy code
The following function displays the calendar for a given year
def showCalender(): gui = Tk() gui.config(background='grey') gui.title("Calender for the year") gui.geometry("550x600") year = int(year_field.get()) gui_content= calendar.calendar(year) calYear = Label(gui, text= gui_content, font= "Consolas 10 bold") calYear.grid(row=5, column=1,padx=20) gui.mainloop() Copy code
explain
ShowCalender The function is displaying the calendar . You enter the year in the search box and press Enter Key time , The display of the calendar will be managed here . You can set the gray background color here , And you can make changes in the code as needed . You can also set the size of the calendar here to 550×600. Then you ask to enter the year as an integer . Once the user enters the year calendar content , It will start from... By taking the year as a parameter python Get... From the calendar module of .
Here is the driver code
if __name__=='__main__': new = Tk() new.config(background='grey') new.title("Calender") new.geometry("250x140") cal = Label(new, text="Calender",bg='grey',font=("times", 28, "bold")) # Enter a label for the year year = Label(new, text="Enter year", bg='dark grey') # Year input text box year_field=Entry(new) button = Button(new, text='Show Calender',fg='Black',bg='Blue',command=showCalender) Copy code
Adjust the position of the widget
cal.grid(row=1, column=1) year.grid(row=2, column=1) year_field.grid(row=3, column=1) button.grid(row=4, column=1) Exit.grid(row=6, column=1) new.mainloop() Copy code
explain
In the driver code , First, we provide the background color for the left part of the screen ( As shown in the figure below ). Because the window giving the input year is very small , So we set its size to 250×140. stay year_field In the button line below , We call the... Created above showCalendar function . This function shows us the full calendar of the year entered .
Now? , We also need to adjust the widgets in the calendar , To this end, we define the location of all content in the grid . You can explore more by changing the grid row and column parameters .
Although the style is ugly , But as a novice, making such a small program is still a little sense of achievement . Link to the original text :https://juejin.cn/post/7011800472663097380