【问题标题】:How to change the color of a tooltip using the Pmw module in python?如何使用 python 中的 Pmw 模块更改工具提示的颜色?
【发布时间】:2021-09-04 03:06:58
【问题描述】:

我正在尝试使用 Pmw 模块在 Tkinter 中制作工具提示。当用户将鼠标悬停在按钮上时,我想显示一个黑色背景和白色文本的工具提示,但我不知道该怎么做。

这是我正在使用的代码:

from tkinter import *
import Pmw

root = Tk()
Pmw.initialise(root)

# Create some random widget
button = Button(root, text=" This is a Test", pady=30)
button.pack(pady=10)

# create balloon object and bind it to the widget
balloon = Pmw.Balloon(root)
balloon.bind(button, "Text for the tool tip")

mainloop()

如何更改工具提示的文本颜色和背景颜色?

【问题讨论】:

    标签: python tkinter tooltip pmw


    【解决方案1】:

    使用balloon.component("label") 获取工具提示的标签组件,然后在该标签上使用config

    这是一个例子:

    from tkinter import *
    import Pmw
    
    root = Tk()
    Pmw.initialise(root)
    
    # Create some random widget
    button = Button(root, text=" This is a Test", pady=30)
    button.pack(pady=10)
    
    # create balloon object and bind it to the widget
    balloon = Pmw.Balloon(root)
    balloon.bind(button, "Text for the tool tip")
    
    lbl = balloon.component("label")
    lbl.config(background="black", foreground="white")
    # Pmw.Color.changecolor(lbl, background="black", foreground="white")
    
    root.mainloop()
    

    【讨论】:

    • 请不要使用wildcard import,而是使用别名-import tkinter as tk
    • @PCM OP 使用通配符导入。我只是复制它。此外,这是一个很小的程序,不会造成太大的问题。
    • 至少你可以告诉他们不要使用它。使用通配符导入是不好的做法
    • @PCM 许多初学者每天都使用通配符导入,不可能告诉所有人不要这样做。对于许多阅读答案的其他人来说,这也会很烦人。随着他们的进步,他们肯定会学习最佳实践。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多