先决条件:Tkinter简介
Python提供了多种开发GUI(图形用户界面)的选项。在所有的GUI方法中, Tkinter是最常用的方法。带有tkinter的Python输出了创建GUI应用程序的最快, 最简单的方法。在本文中, 我们将通过分步指南学习如何使用Tkinter创建GUI日历应用程序。
创建一个tkinter:
- 导入模块– tkinter
- 创建主窗口(容器)
- 将任意数量的小部件添加到主窗口。
- 将事件触发器应用于小部件。
下面是实现:
# import all methods and classes from the tkinter
from tkinter import *# import calendar module
import calendar# Function for showing the calendar of the given year
def showCal() :# Create a GUI window
new_gui = Tk()# Set the background colour of GUI window
new_gui.config(background = "white" )# set the name of tkinter GUI window
new_gui.title( "CALENDER" )# Set the configuration of GUI window
new_gui.geometry( "550x600" )# get method returns current text as string
fetch_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 calender
cal_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 GUI
new_gui.mainloop()# Driver Code
if __name__ = = "__main__" :# Create a GUI window
gui = Tk()# Set the background colour of GUI window
gui.config(background = "white" )# set the name of tkinter GUI window
gui.title( "CALENDER" )# Set the configuration of GUI window
gui.geometry( "250x140" )# Create a CALENDAR : label with specified font and size
cal = Label(gui, text = "CALENDAR" , bg = "dark gray" , font = ( "times" , 28 , 'bold' ))# Create a Enter Year : label
year = 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 function
Show = Button(gui, text = "Show Calendar" , fg = "Black" , bg = "Red" , command = showCal)# Create a Exit Button and attached to exit function
Exit = 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 GUI
gui.mainloop()
输出:
文章图片
文章图片
【Python使用Tkinter的GUI日历示例】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python按符号分组连续元素
- Python电影推荐系统的实现
- Python输入方法的竞争编程
- Python如何以及在哪里应用特征缩放()
- Python如何动态更改Checkbutton的文本()
- Python内部函数用法完整指南
- Python Itertool模块用法完整指南
- Python lambda(匿名函数)|filter、map和reduce
- Python语言的优势和应用介绍