📜  使用 Tkinter 加减天数的日期计算器 – Python

📅  最后修改于: 2022-05-13 01:54:28.762000             🧑  作者: Mango

使用 Tkinter 加减天数的日期计算器 – Python

先决条件: Tkinter、Tkinter 中的 tkcalendar 和 DateTime

Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 方法中,Tkinter 是最常用的方法。它是Python附带的 Tk GUI 工具包的标准Python接口。 Python with Tkinter 是创建 GUI 应用程序的最快、最简单的方法。

在本文中,我们将看到如何在 Tkinter 中从日期计算器中创建天数,从日期计算器中的天数用于添加或减去所选日期的天数以获得新日期。此计算器用于获取给定天数后的确切日期。

让我们一步一步地理解实现:

1.创建普通Tkinter窗口并添加日历

Python3
# Import Required Library
from tkinter import *
from tkcalendar import Calendar
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode = 'day',
            year = 2020, month = 5,
            day = 22)
 
cal.pack(pady = 20)
 
# Execute Tkinter
root.mainloop()


Python3
# Import Required Library
from tkinter import *
from tkcalendar import Calendar
import datetime
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode='day',
               year=2020, month=5,
               day=22)
 
cal.pack(pady=20)
 
frame1 = Frame()
frame2 = Frame()
 
frame1.pack()
frame2.pack()
 
# making label
Label(frame1, text="Days", bd=1, bg="white", width=20,
      relief="solid", font="italic 10 bold").pack(side=LEFT)
 
# input for days
days = Spinbox(frame1, from_=0, to=10000000, font="italic 10")
days.pack(pady=20, padx=10)
 
# making buttons
Button(frame2, text="Add Days", font="italic 15").pack(side=LEFT)
Button(frame2, text="Subtract Days", font="italic 15").pack(padx=10)
 
# making label
converted_date = Label(text="Date: ", bd=2, bg="white", relief="solid",
                       font="italic 10 bold", width=30)
converted_date.pack(pady=20)
 
# Execute Tkinter
root.mainloop()


Python3
# Import Required Library
from tkinter import *
from tkcalendar import Calendar
import datetime
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode = 'day',
            year = 2020, month = 5,
            day = 22)
 
cal.pack(pady = 20)
 
# method to add days
def add_days():
 
    date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
 
    end_date = date_1 + datetime.timedelta(days=int(days.get()))
 
    converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")
 
# method to subtract days
def subtract_days():
 
    date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
 
    end_date = date_1 - datetime.timedelta(days=int(days.get()))
 
    converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")
 
frame1 = Frame()
frame2 = Frame()
 
frame1.pack()
frame2.pack()
 
# making label
Label(frame1, text="Days", bd=1, bg="white", width=20, relief="solid",
      font="italic 10 bold").pack(side=LEFT)
 
# making spinbox
days = Spinbox(frame1, from_= 0, to = 10000000, font="italic 10")
days.pack(pady=20,padx=10)
 
# making buttons
Button(frame2, text = "Add Days",
    command = add_days,font="italic 15").pack(side=LEFT)
Button(frame2, text = "Subtract Days",
    command = subtract_days,font="italic 15").pack(padx=10)
 
# making label
converted_date = Label(text="Date: ", bd=2, bg="white",relief="solid",
                       font="italic 10 bold", width=30)
converted_date.pack(pady=20)
 
# Execute Tkinter
root.mainloop()



输出:

2. 添加按钮和标签

蟒蛇3

# Import Required Library
from tkinter import *
from tkcalendar import Calendar
import datetime
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode='day',
               year=2020, month=5,
               day=22)
 
cal.pack(pady=20)
 
frame1 = Frame()
frame2 = Frame()
 
frame1.pack()
frame2.pack()
 
# making label
Label(frame1, text="Days", bd=1, bg="white", width=20,
      relief="solid", font="italic 10 bold").pack(side=LEFT)
 
# input for days
days = Spinbox(frame1, from_=0, to=10000000, font="italic 10")
days.pack(pady=20, padx=10)
 
# making buttons
Button(frame2, text="Add Days", font="italic 15").pack(side=LEFT)
Button(frame2, text="Subtract Days", font="italic 15").pack(padx=10)
 
# making label
converted_date = Label(text="Date: ", bd=2, bg="white", relief="solid",
                       font="italic 10 bold", width=30)
converted_date.pack(pady=20)
 
# Execute Tkinter
root.mainloop()

输出:

3. 给按钮添加功能

脚步:

  • 使用get_date()方法从日历中获取所选日期。
  • 使用datetime模块中的strptime()方法将日期转换为不同的时间格式
  • 然后我们将从日期中添加/减去天数。
def add_days(): 

    date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
    end_date = date_1 + datetime.timedelta(days=int(days.get()))
    converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")

def subtract_days(): 

    date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
    end_date = date_1 - datetime.timedelta(days=int(days.get()))
    converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")

以下是实现:-

蟒蛇3

# Import Required Library
from tkinter import *
from tkcalendar import Calendar
import datetime
 
# Create Object
root = Tk()
 
# Set geometry
root.geometry("400x400")
 
# Add Calendar
cal = Calendar(root, selectmode = 'day',
            year = 2020, month = 5,
            day = 22)
 
cal.pack(pady = 20)
 
# method to add days
def add_days():
 
    date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
 
    end_date = date_1 + datetime.timedelta(days=int(days.get()))
 
    converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")
 
# method to subtract days
def subtract_days():
 
    date_1 = datetime.datetime.strptime(cal.get_date(), "%m/%d/%y")
 
    end_date = date_1 - datetime.timedelta(days=int(days.get()))
 
    converted_date.config(text=f"Date: {end_date.strftime('%m/%d/%Y')}")
 
frame1 = Frame()
frame2 = Frame()
 
frame1.pack()
frame2.pack()
 
# making label
Label(frame1, text="Days", bd=1, bg="white", width=20, relief="solid",
      font="italic 10 bold").pack(side=LEFT)
 
# making spinbox
days = Spinbox(frame1, from_= 0, to = 10000000, font="italic 10")
days.pack(pady=20,padx=10)
 
# making buttons
Button(frame2, text = "Add Days",
    command = add_days,font="italic 15").pack(side=LEFT)
Button(frame2, text = "Subtract Days",
    command = subtract_days,font="italic 15").pack(padx=10)
 
# making label
converted_date = Label(text="Date: ", bd=2, bg="white",relief="solid",
                       font="italic 10 bold", width=30)
converted_date.pack(pady=20)
 
# Execute Tkinter
root.mainloop()


输出: