nickberredo
nickberredo

Reputation: 1

Can you make a functional invisible button in tkinter?

Can I make a button that is functional yet isn't visible? I've looked into a bunch of Tkinter threads, but when I tested the codes, they all led to the button completely disappearing and being disabled. Here is what I've got so far:

import tkinter as tk
import time

app=tk.Tk()
app.minsize(500, 500)

#button function
def move():
    button.config(state='disabled')
    for i in range(50):
        frame.place(x=250+i)
        frame.update()
        time.sleep(0.01)
    frame.place(x=250, y=250)
    button.config(state='normal')


block=tk.Frame(app, height=50, width=50, bg='red')
frame=tk.Frame(app, height=400, width=400, bg='blue')

#button I wish to be invisible
button=tk.Button(app, text='clickme', command=move)

button.place(x=40, y=40)
frame.place(x=250, y=250, anchor='center')
block.place(x=50, y=50)



app.mainloop()

Upvotes: 0

Views: 3805

Answers (3)

Doyousketch2
Doyousketch2

Reputation: 2147

There's a little more to it than just seting the fg and bg, but yeah it can be done.

#! /usr/bin/env python3
from tkinter import *

color = 'SlateGray4'
width, height = 300, 150
desiredX, desiredY = width *0.5, height *0.75
root = Tk() ; root .title( 'Snozberries' )

root .geometry( f'{width}x{height}' )
root .bind( '<Escape>',  lambda e: root .destroy() )
root .configure( bg = color )

def one():  ##  takes many parameters during construction
    print( 'Found a button!' )

def two( event ):
    ##  print( f'x: {event .x}, y: {event.y}' )
    if event .x >= desiredX -25 and event .x <= desiredX +25 \
    and event .y >= desiredY -25 and event .y <= desiredY +25:
        print( 'Found another button!' )

Label( root, bg=color, text='We are the musicmakers,' ) .pack()
Label( root, bg=color, text='and we are the dreamers of the dreams.' ) .pack()

Button( root, bg=color, fg=color, activebackground=color, activeforeground=color,
        highlightbackground=color, borderwidth=0, command=one ) .pack()

Label( root, bg=color, text='Button, button' ) .pack()
Label( root, bg=color, text="who's got the button?" ) .pack()

root .bind( '<Button-1>', two )
root .mainloop()

Upvotes: 0

Abhishek Aggarwal
Abhishek Aggarwal

Reputation: 190

You can set buttons bg and fg same as the frame/root color and set borderwidth=0. This will create a invisible button. For example refer this

from tkinter import *
root = Tk()

masterFrame = Frame(root, bg='orange', height=300, width=600)
masterFrame.grid(row=0, column=0, sticky='nsew')
Button(masterFrame, text="Invisible button", bg='orange', fg='orange', borderwidth=0).pack()
root.mainloop()

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385940

No, you cannot make an invisible button in tkinter. It must be on the screen for the user to be able to click it.

You can, however, react to clicks anywhere in the window without the need to create a button.

Upvotes: 2

Related Questions