当鼠标箭头停留在选中按钮上时如何显示标题

2024-05-29 06:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

这很难解释。在我的程序中有两个Checkbutton(这可能是它们的名字,但是我说的是可以标记的正方形)。我希望当鼠标放在它们身上时出现一个小标题。如果我能在消息出现之前设置一个时间,那就更好了。 谢谢你

from tkinter import*
from tkinter import messagebox
root = Tk()
root.configure(bg='lightblue')
root.title("#1")
root.geometry("180x60+350+220")
request = StringVar() #i did the same with all other variables
inte=["explorer" , "Explorer","rete"]
chro=["chrome" ,"Chrome","ricerca",]
of=["openoffice","foglio bianco","scrittura"]
dia = Entry(root, textvariable = request, bg="white").grid(row=0,column=1)
chk = Checkbutton(root,text="Ricerca in internet", textvariable = fla, 
bg="lightblue").grid(row=0,column=0) #first checkbutton
chk1 = Checkbutton(root,text="Ricerca in internet", textvariable = ran, 
bg="lightblue").grid(row=0,column=2) #the second one
def apri():     
     if request.get().strip() in inte:
     subprocess.Popen(['C:\Program Files\Internet Explorer\\iexplore.exe']) #i did the same with all other lists 
else:
          webbrowser.open("https://www.google.it/search?q={0}&oq={0}&aqs=chrome..69i57j0j69i61j0l3.1306j0j7&sourceid=chrome&ie=UTF-8".format(request.get().strip()))

bott = Button(root, text="Cerca", command = apri, 
bg="white").grid(row=1,column=1)

这是我的程序,我从任何一个步骤中取一个例子


Tags: thetextinfrom程序requestcolumnroot
1条回答
网友
1楼 · 发布于 2024-05-29 06:46:15

下面的代码将实现您想要的:

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.check = Checkbutton(self.root)
        self.check.pack()
        self.check.bind("<Enter>", self.enter)
        self.check.bind("<Leave>", self.leave)
    def enter(self, *args):
        self.top = Toplevel(self.root)
        self.top.wm_overrideredirect(1)
        self.top.geometry("+%d+%d" % (self.root.winfo_rootx()+self.check.winfo_x()+self.check.winfo_width(), self.root.winfo_rooty()+self.check.winfo_y()))
        self.label = Label(self.top, text="Lorem Ipsum", bg="white")
        self.label.pack()
    def leave(self, *args):
        self.top.destroy()

root = Tk()
App(root)
root.mainloop()

现在我们来分解一下

我们使用.bind("<Enter>", *callback*).bind("<Leave>", *callback*)分别在鼠标进入或离开小部件的边界框时创建回调

然后创建两个回调enter()leave()


enter()中,我们执行以下操作:

1)创建Toplevel小部件

2)使用wm_overrideredirect(),我们实际上只使用它来防止窗口有顶部栏和边框

3)设置车窗位置

4)创建一个包Label小部件到Toplevel


我们来分解第三步

所以我们要创建一个几何值,相当于+n1+n2,其中n1n2是两个独立的数字(可以相同),其中n1是x坐标,n2是y坐标。为了得到x坐标,我们将以下各项相加:

root上使用时.winfo_rootx()的值,它返回屏幕上根窗口的x坐标

check上使用时.winfo_x()的值,返回小部件左边缘相对于根窗口的x坐标

check上使用时.winfo_width()的值,返回小部件的宽度

这三个值使窗口左侧的x坐标等于小部件的右边缘,而不管它在哪里绘制,窗口在哪里。


为了得到y坐标,我们将以下各项相加:

root上使用时.winfo_rooty()的值,它返回屏幕上根窗口的y坐标

check上使用时.winfo_y()的值,返回小部件上边缘相对于根窗口的y坐标

这两个值将窗口顶边的y坐标放置在与小部件顶边相等的位置,而不管它绘制在何处以及窗口位于何处。


leave()要简单得多,当我们调用它时,我们只需销毁Toplevel小部件

相关问题 更多 >

    热门问题