【发布时间】:2020-01-28 02:04:01
【问题描述】:
我有一个带有date 和choose 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()
截图:
【问题讨论】:
-
你做了什么来调试这个?您是否已确认其他进程已启动?您是否确认您实际上正在更改
datestring?您是否知道仅更改timestamp不会改变显示屏上的任何内容? -
是的,在
tkinter的mainloop()运行时(必须调用它以便 GUI 可以处理用户事件)执行其他操作的常用方法是使用通用小部件after()方法来安排对函数的定期调用。 -
@Bryan Oakley,是的,这两个过程开始了。
-
@crussalty:删除所有与
multiprocessing相关的东西并遵循此模式python calendar widget - return the user-selected date -
@Bryan Oakley,我已经删除了多处理并返回了日期。我已经更新了代码。之后,如何用我的
returned_date更新homepage date
标签: python python-3.x tkinter python-multiprocessing tkcalendar