首页 > 解决方案 > 如何从另一个 gui 填充 tkinter 中的输入框

问题描述

我导入了一个名为“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

标签: python-2.7tkintertkinter-entry

解决方案


推荐阅读