1

I'm new to python and I'm trying to create an application in which I want a button to be visible only after I click the "show" button. The button should not be visible from the start of application it should only be visible after clicking on "show" button.

I have this code which hides the button after clicking on another button. It changes its text to "show" after hiding the button.


from tkinter import *

root = Tk()

btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()

btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)

def plugin():
    master = Tk()

    def toggle1():
        if btn1.visible:
            btnToggle1["text"] = "Show Example"
            print ("Now you don't")
            btn1.place_forget()
        else:
            btn1.place(btn1.pi)
            print ("Now you see it")
            btnToggle1["text"] = "Hide Example"
        btn1.visible = not btn1.visible

    
    btnToggle1 = Button(master, text="Hide Example", command=toggle1)
    btnToggle1.place(x=70, y=150)

    master.mainloop()

root.mainloop()

I want the button to show only after I click on the "show" button, not from the start.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Prashil Shah
  • 75
  • 1
  • 5

3 Answers3

0

At the risk of stating the obvious, if you don't want the button to be visible at startup, don't show the button at startup.

You're explicitly making the button visible with this line of code near the start of the program:

btn1.place(x=20, y=50)

You're then setting btn1.pi by calling place_info, but you don't need to do that. You can directly set btn1.p without first calling .place followed by place_info.

btn1 = Button(root,text="Example")
btn1.visible = False
btn1.pi = {"x": 20, "y": 50}

Notice that I also changed btn1.visible to False. You don't actually need a separate attribute to track if it's visible, tkinter can answer that question with the method winfo_viewable().

Or, you can simply remove the button after calculating btn1.pi:

btn1.place(x=20, y=50)
btn1.visible = False
btn1.pi = btn1.place_info()
btn1.place_forget()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

I know this is not for the question I had a hard time finding this. If you are using the GTK library then ButtonName.set_visible(False) sets it invisible

the below is for tkinter I just tested the below for tkinter:

from tkinter import *

def hide_me(event):
    event.widget.pack_forget()
def make_invisible(widget):
   widget.pack_forget()
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', hide_me)
btn.pack()
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', hide_me)
btn2.pack()
make_invisible(btn2)
root.mainloop()

the make_invisible(btn2) method sets it invisible however make sure to call it after you pack the button otherwise it will still show up. here is my resources I used to get to this code https://www.tutorialspoint.com/how-to-make-a-tkinter-widget-invisible and https://newbedev.com/in-tkinter-is-there-any-way-to-make-a-widget-not-visible

-2

i hope that this is what you mean:

from tkinter import *
import tkinter

root = Tk()

btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()

btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)

def plugin():
    master = Tk()

    def toggle1():
        if btn1.visible:
            btnToggle1["text"] = "Show Example"
            btnToggle1["state"] = DISABLED
            print ("Now you don't")
            btn1.place_forget()
        else:
            btn1.place(btn1.pi)
            print ("Now you see it")
            btnToggle1["state"] = NORMAL
            btnToggle1["text"] = "Hide Example"
        btn1.visible = not btn1.visible


    btnToggle1 = Button(master, text="Hide Example", command=toggle1)
    btnToggle1.place(x=70, y=150)

    master.mainloop()

root.mainloop()
Matthijs990
  • 637
  • 3
  • 26
  • 1
    Actually, this does not change the visibility, but rather clickability. Whether the button is clickable or not. – victorkolis Apr 28 '21 at 02:40