Python Tkinter破坏顶层窗口打开

时间:2017-12-12 11:45:09

标签: python python-3.x tkinter

我有一段代码创建了一个弹出窗口,用户输入了一个日期,在选择了窗口关闭的日期之后,我尝试使用Destroy函数,但是它只删除了框架的内容而不是完全关闭它,我不能使用Quit,因为这会关闭每个窗口。

这是弹出功能:

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

这就是我打电话给弹出窗口的地方:

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')

如果有人能帮助我理解为什么弹出功能没有正确关闭那就太好了。

1 个答案:

答案 0 :(得分:2)

在您的代码中,selfFrameself.masterToplevel

你必须使用

self.master.destroy()

所以它应该是

self.button = ttk.Button(self, text='Select', command=self.master.destroy)