程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python getting started project] Python calendar GUI application using Tkinter

編輯:Python

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 step : install Tkinter

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 .

The second step : The import module

First, import. calendar Module and tkinter modular

import calendar
from tkinter import *
Copy code 

The third step : Displays the calendar for the given year

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 .

Step four : Set driver code

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 .

Step five : Output calendar GUI

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved