3

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.

Nate
  • 33
  • 3

2 Answers2

3

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(foreground=color, activeforeground=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()
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • Yup that's one way I can do. Sorry if I am being annoying, but when you clicked and hold the button the text still appeared somehow. Is there a way that making the text temporarily disappeared even when you hold the button, and then only pop back up when you want it to be displayed. – Nate Nov 13 '21 at 06:59
  • @Nate it is but you probably need to use another button. There are different ways to achive this. I'll will show you some exampels. – Thingamabobs Nov 13 '21 at 07:04
  • @aAtlas435 yes I'm fine with creating a new button or even buttons – Nate Nov 13 '21 at 07:12
  • I also said in the question that I can create two or more buttons with the function of hiding and displaying, because I thought that there is no way you can do it without defining new functions. – Nate Nov 13 '21 at 07:14
  • @Nate there could be other but I think you are looking for [styling options](https://stackoverflow.com/questions/69889281/theming-ttk-widgets) – Thingamabobs Nov 13 '21 at 07:48
2

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()
Abhimanyu Sharma
  • 858
  • 1
  • 9
  • 17
  • Woah it works just as I have hoped. I have one more question: what do I do to get the text to displayed again? – Nate Nov 13 '21 at 07:26
  • create a function `showtext` and write : `button.config(text="Hello World")` and give command in `hidetext` `button.config(text="",command=showtext)` – Abhimanyu Sharma Nov 13 '21 at 10:02