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

Time:11-13

I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.

Let say I create a simple button like this:

import tkinter as tk

window = tk.Tk()

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

window.mainloop()

Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead. Any help would be appreciated.

CodePudding user response:

You Can Simply Config And Set Text :

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()

CodePudding user response:

To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.

import tkinter as tk

def hide_text():
    color = button['bg']
    button.config(fg=color)

window = tk.Tk()

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

window.mainloop()

Approach 2 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()

Approach3 using style:

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()
  • Related