我如何从另一个gui填充tkinter中的输入框

时间:2019-03-03 18:26:50

标签: python-2.7 tkinter tkinter-entry

我导入一个名为“ select_date”的函数。该函数运行gui应用程序(calendar-datepicker),并返回所选的年,月和日(该函数经过了良好的测试)。 我想用所选日期填充tkinter条目。当我运行代码时,条目未填写。但是,当不使用select_date函数获取日期时,我们使用定义的值,例如date =“ yyyy / mm / dd”(在代码中注释),程序按预期运行,并且该条目填充有“ yyyy / mm / dd”。

import Tkinter as tk
from mycalendar import select_date


root = tk.Tk()
root.title("My App")
root.geometry("400x400")
w = tk.Label(root, text="Aerosol Optical Depth & Trends")
w.grid(column = 0, row = 100)
w.pack()

# ~ date="yyyy/mm/dd"

e = tk.Entry()
e.pack()

e.delete(0, tk.END)

year,month,day=select_date(myinput="From Date")
date=str(year)+'/'+str(month)+'/'+str(day)

e.insert(0, date)

root.mainloop()

select_date在以下代码中:

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    import Tkinter as tk
    import ttk

import datetime
from tkcalendar import Calendar, DateEntry
import tkMessageBox



def select_date(myinput):

    def example1():
        def print_sel():
            print(cal.selection_get())
            global year
            global month
            global day
            year=cal.selection_get().year
            month=cal.selection_get().month
            day=cal.selection_get().day
            #~ tkMessageBox.askyesno("Date Select", cal.selection_get())
            root.withdraw()
            result = tkMessageBox.askyesno("Date Select", cal.selection_get())
            if result == True:
                root.destroy()

        top = tk.Toplevel(root)

        cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US',
                       cursor="hand1", year=datetime.date.today().year, month=datetime.date.today().month, day=datetime.date.today().day)

        cal.pack(fill="both", expand=True)
        ttk.Button(top, text="select", command=print_sel).pack()



    root = tk.Tk()
    ttk.Button(root, text=myinput, command=example1).pack(padx=10, pady=10)




    root.mainloop()

    return year,month,day

0 个答案:

没有答案