有没有办法将文本隐藏在 Tkinter 的 button/label 中?

Is there away to hide the text inside a button/label in Tkinter?

我是一名 Python 初学者,正在尝试学习 Tkinter,我需要你的帮助。

假设我创建了一个像这样的简单按钮:

import tkinter as tk

window = tk.Tk()

button = tk.Button(text="Hello World!")
button.pack()

window.mainloop()

有没有办法隐藏然后再次显示文本?鉴于我可以再创建两个按钮来完成隐藏和显示的工作。我曾尝试使用 button.pack_forget(),但它会隐藏整个按钮。 任何帮助将不胜感激。

为了使按钮文字看起来像消失了,可以通过cget方法使文字颜色与背景颜色相同。

import tkinter as tk

def hide_text():
    color = button['bg']
    button.config(foreground=color, activeforeground=color)

window = tk.Tk()

button = tk.Button(text="Hello World!",command=hide_text)
button.pack()

window.mainloop()

方法二ttk.Button:

import tkinter as tk
from tkinter import ttk

def hide_text():
    button.config(text='')

window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()

window.mainloop()

方法 3 使用风格:

import tkinter as tk
from tkinter import ttk

def change_button_style(event):
    widget = event.widget
    if widget['style'] == 'TButton':
        widget.configure(style='VanishedText.TButton')
    else:
        event.widget.config(style='TButton')


BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'

window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')

button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()

style.map('VanishedText.TButton',
            foreground =[('disabled',BACKGROUND),
                        ('!disabled',BACKGROUND),
                        ('pressed',BACKGROUND),
                        ('!pressed',BACKGROUND),
                        ('active',BACKGROUND),
                        ('!active',BACKGROUND)],
            background =[('disabled',BACKGROUND),
                        ('!disabled',BACKGROUND),
                        ('pressed',BACKGROUND),
                        ('!pressed',BACKGROUND),
                        ('active',BACKGROUND),
                        ('!active',BACKGROUND)],
            focuscolor=[('disabled',BACKGROUND),
                        ('!disabled',BACKGROUND),
                        ('pressed',BACKGROUND),
                        ('!pressed',BACKGROUND),
                        ('active',BACKGROUND),
                        ('!active',BACKGROUND)])

window.mainloop()

您可以简单地配置和设置文本:

import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
    button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()