单击按钮时如何更改日期?

How to change date when button is clicked?

我有一个带有 datechoose date 按钮的 tkinker window,我将其称为主页。当用户选择日期时,我希望在主页中更新日期。我在 datecheck 函数中重新调整了选定的日期。然后我想用返回日期更新主页日期。我不知道如何让它成为可能。帮我解决一下。

这是我写的示例代码:

from tkinter import *
from tkinter import ttk
from tkinter import scrolledtext
import time
import tkinter.messagebox
from datetime import datetime
import tkinter as tk
import sys
import os
from tkcalendar import Calendar, DateEntry
from datetime import date
import multiprocessing


def datecheck():
    global date_string
    root = Tk()
    s = ttk.Style(root)
    s.theme_use('clam')
    def print_sel():
        global date_string,timestamp
        date_string =cal.selection_get()
        date_string=date_string.strftime("%d-%b-%Y")
        print("returned_date",date_string)
        root.destroy()
    today = date.today()
    d = int(today.strftime("%d"))
    m= int(today.strftime("%m"))
    y =int(today.strftime("%Y"))
    cal = Calendar(root,
                   font="Arial 14", selectmode='day',
                   cursor="hand1",   day=d,month=m,year=y)
    cal.pack(fill="both", expand=True)
    ttk.Button(root, text="ok", command=print_sel).pack()
def homepage():
    global date_string,timestamp
    if date_string == "":
            timestamp = datetime.now().strftime("%d-%b-%Y")

    else:
        timestamp = date_string
    def close_window():
        window.destroy()

    window = Tk()
    window.title("Status Uploader")
    window.geometry('500x200')
    Label(window,
          text="Status Uploader",
          fg="blue",
          bg="yellow",
          font="Verdana 10 bold").pack()
    Label(window,
          text="Date : {}".format(timestamp),
          fg="red",
          bg="yellow",
          font="Verdana 10 bold").pack()

    txt = scrolledtext.ScrolledText(window, width=60, height=9.5)
    txt.pack()
    button = Button(window, fg='white', bg='blue',
                    text="Choose Date", command=datecheck)
    button.place(x=35, y=152)
    button = Button(window, fg='white', bg='red',
                    text="Close", command=close_window)
    button.place(x=405, y=152)
    window.mainloop()        

global date_string,timestamp
date_string = ""
homepage()

截图:

这是您的代码的一个版本,它没有使用 multiprocessing,因为我认为没有必要使用它 — 尽管我不太明白您要针对两个 date_stringtimestamp 全局变量以及它们之间的关系。下面代码中发生的所有事情是后者被 fun() 函数周期性地复制到第一个,该函数每 1000 毫秒调用一次(例如每秒一次)。

这是通过使用通用 tkinter 小部件 after{} 方法定期检查日期和时间并更新全局变量来完成的 - multiprocessing 不需要能够在这种情况下要做。

为了让标签在用户选择新日期后显示要更改的日期,我在 datecheck() 函数中添加了一个 date_label 参数并修改了 homepage() 函数通过单击 Choose Date Button.

调用时将其作为参数传递给函数

我通常还清理了代码并使其遵循 PEP 8 - Style Guide for Python Code 指南,使其更易于阅读和维护。

from datetime import date, datetime
from functools import partial
import os
import sys
from tkcalendar import Calendar, DateEntry
from tkinter import ttk
from tkinter import scrolledtext
import tkinter as tk

def datecheck(date_label):
    global date_string, timestamp

    root = tk.Toplevel()
    s = ttk.Style(root)
    s.theme_use('clam')

    def print_sel():
        global date_string

        cal_selection = cal.selection_get()
        date_string = cal_selection.strftime("%d-%b-%Y")
        # Update the text on the date label.
        date_label.configure(text="Date : {}".format(date_string))
        root.destroy()

    today = date.today()
    d = int(today.strftime("%d"))
    m = int(today.strftime("%m"))
    y =int(today.strftime("%Y"))

    cal = Calendar(root,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", day=d,month=m, year=y)
    cal.pack(fill="both", expand=True)

    ttk.Button(root, text="ok", command=print_sel).pack()


def homepage():
    global date_string, timestamp

    if date_string == "":
        timestamp = datetime.now().strftime("%d-%b-%Y")
    else:
        timestamp = date_string

    def close_window():
        window.destroy()

    window = tk.Tk()
    window.title("Status Uploader")
    window.geometry('500x200')
    tk.Label(window,
             text="Status Uploader",
             fg="blue",
             bg="yellow",
             font="Verdana 10 bold").pack()
    date_label = tk.Label(window,
                          text="Date : {}".format(timestamp),
                          fg="red",
                          bg="yellow",
                          font="Verdana 10 bold")
    date_label.pack()

    # Create callback function for "Choose Date" Button with data_label
    # positional argument automatically supplied to datecheck() function.
    datecheck_callback = partial(datecheck, date_label)

    txt = scrolledtext.ScrolledText(window, width=60, height=9.5)
    txt.pack()

    button = tk.Button(window, fg='white', bg='blue',
                       text="Choose Date",
                       command=datecheck_callback)
    button.place(x=35, y=152)
    button = tk.Button(window, fg='white', bg='red', text="Close", command=close_window)
    button.place(x=405, y=152)

    window.after(1000, fun, window)  # Start periodic global variable updates.
    window.mainloop()


def fun(window):
    """ Updates some global time and date variables. """
    global date_string, timestamp

    if date_string == "":
        timestamp = datetime.now().strftime("%d-%b-%Y")
    else:
        timestamp = date_string

    window.after(1000, fun, window)  # Check again in 1000 milliseconds.


# Define globals.
date_string = ""
timestamp = None

homepage() # Run GUI application.