【问题标题】:How to show up a caption when the mouse arrow stays on the checkbutton当鼠标箭头停留在复选按钮上时如何显示标题
【发布时间】:2017-09-22 20:55:35
【问题描述】:

这很难解释。在我的程序中有两个 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)

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

【问题讨论】:

  • 您在谈论工具提示,许多库都实现了它们,您在项目中使用的是哪一个?用一些代码来解释也不太难......
  • 我正在使用 tkinter,我会尽快编辑我的帖子
  • 我认为它在标准 tkinter 中不可用,但您可以找到许多自定义类的示例来处理它。尝试使用“tooltip and tkinter”进行一些查询
  • 完美,谢谢。

标签: python checkbox tkinter


【解决方案1】:

下面的代码将实现你正在寻找的:

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("&lt;Enter&gt;", *callback*).bind("&lt;Leave&gt;", *callback*) 分别在鼠标进入或离开小部件的边界框时创建回调。

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


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

1) 创建一个Toplevel 小部件。

2) 使用wm_overrideredirect(),我们实际上只是用来防止窗口出现顶栏和边框。

3) 设置窗口位置。

4) 创建一个将Label 小部件打包到Toplevel 中。


让我们分解第 3 步。

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

.winfo_rootx() 的值在用于 root 时返回屏幕上根窗口的 x 坐标。

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

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

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


为了得到 y 坐标,我们将以下内容加在一起:

.winfo_rooty() 的值在用于 root 时返回屏幕上根窗口的 y 坐标。

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

这两个值使窗口顶部的 y 坐标等于窗口小部件的顶部边缘,无论它在哪里绘制以及窗口在哪里。


leave() 非常简单,当我们调用它时,我们只是破坏了 Toplevel 小部件。

【讨论】:

  • 主要问题是我不知道什么是类,也找不到我的语言好的指南。我不懂英语指南,因为我的英语水平刚好适合口语演讲。我应该问我父亲,但他从 1991 年起就没有写过代码了。不过谢谢,但我只能复制粘贴代码,没有足够的了解
  • 类只是python的面向对象工具。查找“面向对象编程”并找到使用您的母语的指南。另外,如果此答案回答了您的问题,请您将其标记为已接受,以便其他用户将来可以使用它。
猜你喜欢
  • 2019-09-02
  • 1970-01-01
  • 2022-08-06
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多