Cómo hacer que aparezca un botón después de hacer clic en otro botón en tkinter?
Tengo este problema aquí que quiero que aparezca un botón (b1
en el código) después de que el usuario haga clic en el botón principal (b0
).
from tkinter import *
from tkinter import filedialog
import tkinter.messagebox
import os
def openword():
my_program = filedialog.askopenfilename()
os.system("%s" % my_program)
def btn_clicked():
tkinter.messagebox.showinfo("Inicio de sesión", "Inicio de sesión exitoso, ¡Bienvenido!")
window = Tk()
window.geometry("1000x600")
window.configure(bg="#293335")
canvas = Canvas(
window,
bg="#293335",
height=600,
width=1000,
bd=0,
highlightthickness=0,
relief="ridge")
canvas.place(x=0, y=0)
background_img = PhotoImage(file=f"background.png")
background = canvas.create_image(
508.5, 228.0,
image=background_img)
entry0_img = PhotoImage(file=f"imgtextBox0.png")
entry0_bg = canvas.create_image(
166.0, 367.0,
image=entry0_img)
entry0 = Entry(
bd=0,
bg="#ffffff",
highlightthickness=0)
entry0.place(
x=22, y=351,
width=288,
height=30)
entry1_img = PhotoImage(file=f"imgtextBox1.png")
entry1_bg = canvas.create_image(
166.0, 456.0,
image=entry1_img)
entry1 = Entry(
bd=0,
bg="#ffffff",
highlightthickness=0)
entry1.place(
x=22, y=440,
width=288,
height=30)
img0 = PhotoImage(file=f"img0.png")
b0 = Button(
image=img0,
borderwidth=0,
highlightthickness=0,
command=btn_clicked,
relief="flat")
b0.place(
x=28, y=500,
width=102,
height=38)
img1 = PhotoImage(file=f"img1.png")
b1 = Button(
image=img1,
borderwidth=0,
highlightthickness=0,
command=openword,
relief="flat")
b1.place(
x=766, y=505,
width=213,
height=72)
window.resizable(False, False)
window.mainloop()
El código funciona perfectamente, pero los dos botones aparecen al mismo tiempo. Necesito que el botón b1
aparezca después de que el usuario presione el botón b0
.
davy.ai
Una posible solución para lograr esto sería establecer la visibilidad de
b1
comohidden
de forma predeterminada, y luego cambiarla avisible
una vez que se haga clic enb0
. Esto se puede hacer usando el métodoconfig
en tkinter. Aquí tienes un ejemplo de código actualizado:Cuando el usuario hace clic en
b0
, se llama a la funciónbtn_clicked
que muestra un mensaje de inicio de sesión exitoso y luego cambia la visibilidad deb1
avisible
. El estado inicial deb1
se establece comohidden
al establecer su atributostate
comodisabled
. Una vez que elstate
se cambia anormal
, el botón se vuelve visible para el usuario.