私はユーザーが日付を入力し、ウィンドウを閉じる日付を選択した後に、破棄機能を使用しようとしましたが、フレームの内容を完全に閉じるのではなく、すべてのウィンドウを閉じるようにQuitを使用することはできません。誰かがポップアップ機能が適切にそれを閉じていない理由を私は理解するのに役立つことができればPython Tkinterがtopenvelの葉のウィンドウを破棄します
class GooglePageTime(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.entries = []
self.dates = ['','']
self.numOfKeywords = 0
self.keywordDisplay = tk.StringVar(self)
self.keywordDisplay.set('Current Keywords: ')
self.entryVar = tk.StringVar(self)
self.label = ttk.Label(self, text="Google Trends\nTime", font='Helvetica 12 bold')
self.label.grid(row=1,column=0,padx = 10, columnspan = 2)
self.button = ttk.Button(self, text="Return", command=lambda: controller.show_frame(StartPage))
self.button.grid(row=0, column = 0)
self.button = ttk.Button(self, text="Location", command=lambda: controller.show_frame(GooglePageLoc))
self.button.grid(row=2, column = 0)
self.button2 = ttk.Button(self, text="Time", command=lambda: controller.show_frame(GooglePageTime))
self.button2.grid(row=2, column = 1)
self.label2 = ttk.Label(self, text="Enter keyword:", font = 'Helvetica 10')
self.label2.grid(row=3, column= 0)
self.entry = ttk.Entry(self, textvariable=self.entryVar)
self.entry.grid(row = 3, column =1)
self.entry.bind('<Return>', lambda x:self.addToList())
self.label3 = ttk.Label(self, textvariable= self.keywordDisplay, font = 'Helvetica 10')
self.label3.grid(row=4, column=0, columnspan= 2, sticky = 'w')
self.button3 = tk.Button(self, text="X", command= lambda: self.resetString(),height = 1, width = 1)
self.button3.grid(row=4, column=2)
self.button3 = ttk.Button(self, text="Select Date", command = lambda : self.getDates(0))
self.button3.grid(row=5, column=0)
self.button4 = ttk.Button(self, text='Print', command = lambda:print(self.dates))
self.button4.grid(row=5, column=1)
def getDates(self, num):
window = tk.Toplevel(self)
dateApp = DateSelector(window)
dateApp.pack()
self.dates[num] = dateApp.returnData()
def resetString(self):
self.keywordDisplay.set('Current keywords: ')
self.entries = []
self.numOfKeywords = 0
def addToList(self):
if self.entryVar.get() != '':
self.entries.append(self.entryVar.get())
self.numOfKeywords += 1
if self.numOfKeywords == 1:
self.keywordDisplay.set(self.keywordDisplay.get() + self.entryVar.get())
else:
self.keywordDisplay.set(self.keywordDisplay.get() + ', ' + self.entryVar.get())
self.entry.delete(0,'end')
ます:
class DateSelector(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
master.wm_title("Date Selector")
self.dayVar = tk.IntVar(self)
self.monthVar = tk.IntVar(self)
self.yearVar = tk.IntVar(self)
self.now = datetime.now()
self.currentYear = self.now.year
self.currentMonth = self.now.month
self.currentDay = self.now.day
self.yearList = []
for year in range(self.currentYear, 2003, -1):
self.yearList.append(str(year))
self.monthsDay = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
self.yearVar.set(self.currentYear)
self.yearOptions = ttk.OptionMenu(self, self.yearVar, self.currentYear, *self.yearList, command = self.optupdate)
self.yearOptions.grid(row=0, column=2)
self.monthVar.set(1)
self.monthsOptions = ttk.OptionMenu(self, self.monthVar, self.currentMonth, *[x for x in range(1, self.currentMonth + 1)], command = self.optupdate)
self.monthsOptions.grid(row=0, column=1)
self.dayVar.set(1)
self.dayOptions = ttk.OptionMenu(self, self.dayVar, self.currentDay,*[x for x in range(1,self.currentDay+1)])
self.dayOptions.grid(row=0,column=0)
self.button = ttk.Button(self, text='Select', command = lambda:self.destroy())
self.button.grid(row=1, column = 1)
def returnData(self):
return str(self.yearVar.get()) + '-' + str(self.monthVar.get()) + '-' + str(self.dayVar.get())
def optupdate(self, value):
x = 0
if self.yearVar.get() == self.currentYear:
self.monthsOptions = ttk.OptionMenu(self, self.monthVar, self.monthVar.get(), *[x for x in range(1, self.currentMonth + 1)], command = self.optupdate)
self.monthsOptions.grid(row=0, column=1)
else:
self.monthsOptions = ttk.OptionMenu(self, self.monthVar, self.monthVar.get(), *[x for x in range(1, 13)], command = self.optupdate)
self.monthsOptions.grid(row=0, column=1)
if self.isLeapYear(self.yearVar.get()) == True and self.monthVar.get() == 2:
x = 1
if self.monthVar.get() == self.currentMonth and self.yearVar.get() == self.currentYear:
self.dayOptions = ttk.OptionMenu(self, self.dayVar, self.currentDay,*[x for x in range(1,self.currentDay+1)])
self.dayOptions.grid(row=0,column=0)
else:
self.dayOptions = ttk.OptionMenu(self, self.dayVar, self.currentDay,*[x for x in range(1,(self.monthsDay[self.monthVar.get()] + x + 1))])
self.dayOptions.grid(row=0,column=0)
def isLeapYear(self, year):
if ((year % 4 == 0 and (year % 100 != 0))) or (year % 400 == 0):
return True
return False
と私はポップアップを呼び出すところ、これは次のとおりです。ここで
はポップアップ機能であります素晴らしいよあなたのコードself
で
でなければなりません
を使用する必要が
Frame
ですがself.master
がToplevel
であるあなたはself.button = ttk.Button 'てみました(self、text = 'Select'、command = self.destroy) '? – Aemyl
@Aemyl私はちょうどそれを試したが、どちらもうまくいかない、self.destroyはウィンドウに何もしないように見える。 –
フレーズ_itはフレームを完全に閉じる代わりにフレームの内容を削除するだけでは本質的に間違っている。 OPはToplevelを意味していたに違いない。より良い方法は、 'Frame'ではなく' Toplevel'から 'DateSelector'を継承することです。 – Nae