es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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.

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Una posible solución para lograr esto sería establecer la visibilidad de b1 como hidden de forma predeterminada, y luego cambiarla a visible una vez que se haga clic en b0. Esto se puede hacer usando el método config en tkinter. Aquí tienes un ejemplo de código actualizado:

    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!")
        b1.config(state="normal")  # Cambiar la visibilidad de b1 a visible
    
    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 = "background.png")
    background = canvas.create_image(
        508.5, 228.0,
        image=background_img)
    
    entry0_img = PhotoImage(file = "img_textBox0.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 = "img_textBox1.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 = "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 = "img1.png")
    b1 = Button(
        image = img1,
        borderwidth = 0,
        highlightthickness = 0,
        command = openword,
        relief = "flat")
    
    # Establecer la visibilidad de b1 como oculta
    b1.config(state="disabled")
    
    b1.place(
        x = 766, y = 505,
        width = 213,
        height = 72)
    
    window.resizable(False, False)
    window.mainloop()
    

    Cuando el usuario hace clic en b0, se llama a la función btn_clicked que muestra un mensaje de inicio de sesión exitoso y luego cambia la visibilidad de b1 a visible. El estado inicial de b1 se establece como hidden al establecer su atributo state como disabled. Una vez que el state se cambia a normal, el botón se vuelve visible para el usuario.

Comments are closed.