Tkinter的问题关闭窗口协议

时间:2019-05-28 20:06:52

标签: python tkinter

这是我第一次使用TKinter,我对python还是很陌生。我正在制作一个日历应用程序,该应用程序具有一个显示简单日历的主窗口。我还向每个框添加了一个事件,这将打开另一个窗口,其中包含当天的任务列表(现在是准系统)。我的问题是,当我创建名为taskWin的顶级窗口并使用“ WM_DELETE_WINDOW”设置其关闭协议时,在按下窗口上的X按钮之前调用了回调函数,它是否应该等到按下该按钮后才能调用?对于更多的上下文,我只希望一次打开一个taskWin窗口!我是python的新手,所以如果您有其他建议,请分享:)

我已经尝试过使用窗口状态并检查它们是否等于“正常”状态,但是我不能这样做,因为taskWin在我什至无法检查之前就没有初始化

class AppFrame:
    def __init__(self):
        self.days = []
        self.isTaskWinOpen = False
        self.window = tk.Tk()
        self.window.title("EZPlanner")
        self.window.geometry("800x600")
        self.window.resizable(width=False,height=False)
        self.frame = tk.Frame(self.window)
        self.frame.pack()
        self.createButtons()
        print(self.days)
        self.window.mainloop()
    # Should be responsible for keeping track of the month and how many days in each month
    def createButtons(self):
        counter = 0
        for x in range(0,5):
            for y in range(0,7):
                counter +=1
                labels = tk.Label(self.frame,font=("Courier",15),bg = "grey",borderwidth = 2,relief="solid",anchor = 'ne',width = 12,height=6,text=str(counter))
                labels.grid(column = y,row= x)
                labels.bind("<Button-1>",lambda event, nCounter = counter: self.taskWindow(event, nCounter))
                self.days.append(labels)

    # restrictions include: only one window open at a time
    # both task and tasks of current date are shown 
    # text input to add tasks
    # then list view to show tasks 
    def taskWindow(self,event,nCounter):
        if(self.isTaskWinOpen == False):
            self.isTaskWinOpen = True
            taskWin = Toplevel(self.window)
            taskWin.protocol("WM_DELETE_WINDOW", self.taskClosed())
            taskWin.geometry("300x300")
            taskLabel = tk.Label(taskWin,text = "Task Name: ")
            taskLabel.grid(column = 0,row = 0)
            taskInput = tk.Entry(taskWin,width = 30)
            taskInput.grid(column = 1,row = 0)
            # self.days[nCounter - 1].config(text="Day:{}\n-NEW TASK".format(nCounter))
        else:
            taskWin.focus_set()
        print("IS OPENED: "+str(self.isTaskWinOpen))

    def taskClosed(self):
        print("TASK WAS CLOSED")
        self.isTaskWinOpen = False

我没有任何错误。

0 个答案:

没有答案