ttk 菜单不会取消发布 [英] ttk Menu wont unpost

查看:56
本文介绍了ttk 菜单不会取消发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的弹出消息,当鼠标进入时显示选项的名称,并在鼠标离开时隐藏.

I wanted to make a simple pop-up message that shows the name of the options represented as icons when the mouse enters them and hide when it leavs.

这个图标是按钮内的图像,我已经能够在使用菜单小部件输入按钮时显示消息,但是当鼠标离开按钮时,它不会取消发布,除非有点击.

This icons are images within buttons, and I have been able to show the message when entering the button using a Menu widget, but when the mouse leaves the button it dose NOT unpost, unless there's a klick.

我尝试删除级联,但发生了同样的情况,不同之处在于他当时的菜单没有文字.

I tryed deleting the cascade, but the same happens, the difference is that he menu at that moment has no text.

我也尝试了 .destroy() 菜单对象,但它什么也没做.

I tryed to .destroy() the menu object as well, but it does nothing.


from tkinter import *           
from tkinter import ttk         
from pyautogui import position  

Raiz = Tk()                                         
Raiz.title("Mi app")    
Ancho = Raiz.winfo_screenwidth()                    
Alto = Raiz.winfo_screenheight()                    
Raiz.geometry("{}x{}".format(Ancho, Alto))          
Raiz.config(bg="#F4F4F4")                           


def Despliega(Texto):
    global MenuDesplegable

    MenuDesplegable = Menu(master=None, tearoff=0, activebackground='#F0F0F0')
    MenuDesplegable.add_cascade(label=Texto)
    MenuDesplegable.post(position().x, position().y)


def Repliega():
    global MenuDesplegable
    #MenuDesplegable.delete(0) -- dosen't work
    #MenuDesplegable.unpost() -- dosen't work
    #MenuDesplegable.destroy() -- dosen't work




Raiz.columnconfigure(0, weight=1)

BarraMenu = Frame(Raiz, bg="light grey", height=50, width="{}".format(Ancho),               
                    bd="4", relief="groove")

BarraMenu.grid(row=0, column=0, sticky="nsew")

I_Abrir = PhotoImage(file="Abrir.png")
B_Abrir = Button(BarraMenu, bg="light grey", image=I_Abrir, bd=0)
B_Abrir.grid(row=0, column=0, padx=10)
B_Abrir.bind('<Enter>', lambda event: Despliega('Abrir'))
B_Abrir.bind('<Leave>', lambda event: Repliega())


I_Nuevo = PhotoImage(file="Nuevo.png")
B_Nuevo = Button(BarraMenu, bg="light grey", image=I_Nuevo, bd=0)
B_Nuevo.grid(row=0, column=1, padx=10)
B_Nuevo.bind('<Enter>', lambda event: Despliega('Nuevo'))
B_Nuevo.bind('<Leave>', lambda event: Repliega())


Raiz.mainloop()

如果有人理解为什么它不像我描述的那样工作,那就太好了.另外,如果有人知道一种显示延迟很小的消息的方法,请告诉我.

It would be nice if someone understood why it dosen't work as I descrived. Also, if someone knows a way to show the message with a littel delay, please, show it o me.

推荐答案

您可以创建一个以小部件和消息作为参数的类,然后应用于任何需要该信息的小部件.

You can create a class that takes a widget and a message as parameters, and then apply to any widget requiring the info.

import tkinter as tk

root = tk.Tk()

class CreateToolTip:
    def __init__(self, widget, text='widget info'):
        self.waittime = 100 #500     #miliseconds
        self.wraplength = 180   #pixels
        self.widget = widget
        self.text = text
        self.widget.bind("<Enter>", self.enter)
        self.widget.bind("<Leave>", self.leave)
        self.widget.bind("<ButtonPress>", self.leave)
        self.id = None
        self.tw = None

    def enter(self, event=None):
        self.schedule()

    def leave(self, event=None):
        self.unschedule()
        self.hidetip()

    def schedule(self):
        self.unschedule()
        self.id = self.widget.after(self.waittime, self.showtip)

    def unschedule(self):
        id = self.id
        self.id = None
        if id:
            self.widget.after_cancel(id)

    def showtip(self, event=None):
        x = y = 0
        x, y, cx, cy = self.widget.bbox("insert")
        x += self.widget.winfo_rootx() + 25
        y += self.widget.winfo_rooty() + 40
        # creates a toplevel window
        self.tw = tk.Toplevel(self.widget)
        # Leaves only the label and removes the app window
        self.tw.wm_overrideredirect(True)
        self.tw.wm_geometry("+%d+%d" % (x, y))
        label = tk.Label(self.tw, text=self.text, justify='left',
                       background="#ffffff", relief='solid', borderwidth=1,
                       wraplength = self.wraplength)
        label.pack(ipadx=1)

    def hidetip(self):
        tw = self.tw
        self.tw= None
        if tw:
            tw.destroy()

a = tk.Button(root,text="Something")
a.pack()
CreateToolTip(a,"This is something button")
b = tk.Button(root,text="Another")
b.pack()
CreateToolTip(b,"This is another button")

root.mainloop()

这篇关于ttk 菜单不会取消发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆