1

我有一个带有datechoose date按钮的 tkinker 窗口,我将其称为主页。当用户选择日期时,我希望在主页中更新日期。我在函数中重新调整了选定的日期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()

截屏:

脚本运行截图

4

1 回答 1

1

这是您未使用的代码版本,multiprocessing因为我认为没有必要使用它-尽管我并不真正了解您对两个date_stringtimestamp全局变量的尝试以及它们与一个的关系其他。下面代码中发生的所有事情都是后者被fun()函数定期复制到第一个,该函数每 1000 毫秒调用一次(例如每秒一次)。

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

为了在用户选择新日期后让标签显示要更改的日期,我date_label向函数添加了一个参数,datecheck()并修改了homepage()函数,以便在调用函数时将其作为参数传递给函数Choose Date Button

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

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.
于 2019-09-27T19:29:28.887 回答