Идея состоит в том, чтобы: иметь главное окно для пользовательских вводов с помощью инструмента, чтобы вызвать календарь для ввода даты, а затем сохранить все входные данные для последующего использования.
Моя проблема: дополнительное пустое окно открывается каждый раз, когда открывается окно календаря. Когда я добавляю self. root .withdraw () к классу календаря, он работает, но тогда я не могу получить доступ к своим переменным вне основного l oop.
Чего здесь не хватает? Как мне избежать этого дополнительного третьего окна?
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar
from tkcalendar import Calendar, DateEntry
class calendar_box():
def __init__(self, root):
self.top = tk.Toplevel(root)
self.cal = Calendar(self.top, font="Arial 14", selectmode='day', cursor="hand1")
self.cal.pack(fill="both", expand=True)
ttk.Button(self.top, text="Select",
command=lambda:[self.print_sel(), self.top.destroy()]).pack()
self.date = ''
self.top.grab_set()
def print_sel(self):
self.date = self.cal.selection_get()
class MyWindow:
def __init__(self, win):
self.lbl=Label(win, text="Calendar Tool")
self.lbl1=Label(win, text='ID')
self.lbl2=Label(win, text='Name')
self.lbl3=Label(win, text='days back')
self.lbl6=Label(win, text="Message")
self.t1=Entry()
self.t2=Entry()
self.t3=Entry()
self.t4=Entry()
self.t5=Entry()
self.t6=Entry()
self.lbl.place(x=150, y=10)
self.lbl1.place(x=50, y=50)
self.t1.place(x=150, y=50)
self.lbl2.place(x=50, y=90)
self.t2.place(x=150, y=90)
self.lbl3.place(x=50, y=140)
self.t3.place(x=150, y=140)
#self.lbl4.place(x=50, y=180)
self.t4.place(x=150, y=180)
#self.lbl5.place(x=50, y=220)
self.t5.place(x=150, y=220)
self.lbl6.place(x=50, y=300)
self.t6.place(x=150, y=300)
self.b1=Button(win, text='Submit', command=self.submit)
self.b2=Button(win, text='Refresh')
self.b2.bind('<Button-1>', self.refresh)
self.b1.place(x=150, y=260)
self.b2.place(x=270, y=260)
#calendar button
self.b3=Button(win, text="Screen Date", command=self.date1)
self.b4=Button(win, text="End TLFB Date", command=self.date2)
self.b3.place(x=50, y=180)
self.b4.place(x=50, y=220)
def date1(self):
self.root=tk.Tk()
s=ttk.Style(self.root)
s.theme_use("clam")
cal = calendar_box(self.root)
self.root.wait_window(cal.top)
self.screen = cal.date
self.t4.delete(0, 'end') #remove entry in the field
self.t4.insert(END, cal.date) #replace with selected date
def date2(self):
self.root=tk.Tk()
s=ttk.Style(self.root)
s.theme_use("clam")
cal = calendar_box(self.root)
self.root.wait_window(cal.top)
self.baseline = cal.date
self.t5.delete(0, 'end') #remove entry in the field
self.t5.insert(END, cal.date) #replace with selected date
def refresh(self, event):
self.t1.delete(0, 'end')
self.t2.delete(0, 'end')
self.t3.delete(0, 'end')
self.t4.delete(0, 'end')
self.t5.delete(0, 'end')
self.t6.delete(0, 'end')
def submit(self):
global CID, name, ndays, screen, baseline
self.t6.delete(0, 'end')
CID=self.t1.get()
name=self.t2.get()
ndays=self.t3.get()
screen=self.t4.get()
baseline=self.t5.get()
self.t6.insert(END, str("All set. Close the Window. "))
window=Tk()
mywin=MyWindow(window)
window.title("Calendar Tool")
window.geometry("400x400+10+10")
window.mainloop()