如何在tkinter中关闭带有绑定事件的窗口

时间:2015-09-08 17:55:31

标签: python tkinter

我的问题是当光标在问题按钮上移动时关闭窗口,并在光标离开问题按钮时尝试关闭新窗口。我试过quit方法的交换销毁方法也不行。代码在

之下
#!/usr/bin/env python

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):

        self.texto = tk.Label(self, text='defina o tamanho do lado')
        self.texto.grid(row=0, column=0)

        self.texto2 = tk.Label(self, text='defina o valor do lado')
        self.texto2.grid(row=1, column=0)

        self.imagem_1 = tk.PhotoImage(file='F:/.../question.png')

        self.button_ajuda = tk.Button(self, image= self.imagem_1, height=15, width=15)
        self.button_ajuda.grid(row= 1, column=2)
        self.button_ajuda.bind('<Enter>', self.info)# evento para abrir janela info ao passar o cursor sobre o botão
        self.button_ajuda.bind('<Leave>', self.info_close)# evento para fechar janela info

        self.entrada = tk.Entry(self, width=5) #define o widget de entrada 'width' é o tamanho
        self.entrada.grid(row=0, column=1)# define a posição do widget de entrada

        self.entrada2 = tk.Entry(self, width=5)
        self.entrada2.grid(row=1, column=1)

    def info(self, event):
        val = 0
        win = tk.Toplevel(self)
        texto_info='''b is basis of rectangle
                    (a) will be (l) if (b) is lowest side
                    or (h) if (b) is the highest side of
                    the rectangle'''
        tk.Label(win, text= texto_info).pack()
        x= win.winfo_pointerx()+100
        y= win.winfo_pointery()
        win.geometry('+%d+%d'%(x,y))
        win.overrideredirect(1)
        if val == 1:
            win.destroy()
     def info_close(self, event):
        global val
        val =1

app = Application()
app.master.title('Aureum')
app.mainloop()

1 个答案:

答案 0 :(得分:0)

如果要在鼠标悬停在按钮上时显示信息窗口并在离开时将其销毁,则可以将窗口本身保存为类属性,这样就不需要使用{{1}跟踪它:

global val