Search code examples
pythontkintertkinter-button

When using buttons on tkinter they get overlaped


When I run my code it pop up the window I want but the buttons get overlaped and I got no idea why.

main_action = tk.Tk()

    main_action.geometry("600x600")
    main_action.title("Keymanager")
    main_action.resizable(0, 0)

    main_action.columnconfigure(0, weight=1)
    main_action.columnconfigure(1, weight=3)
    main_action.columnconfigure(2, weight=4)

    main_action = tk.Label(main_action, text="Escoja que accion quiere realizar")
    main_action.grid(column=1, row=0, sticky=tk.E, padx=5, pady=5)

    main_action = tk.Button(main_action, text="Ver")
    main_action.grid(column=1, row=2, sticky=tk.E, padx=5, pady=5)

    main_action = tk.Button(main_action, text="Añadir")
    main_action.grid(column=2, row=2, sticky=tk.E, padx=5, pady=5)

    main_action = tk.Button(main_action, text="Eliminar")
    main_action.grid(column=3, row=2, sticky=tk.E, padx=5, pady=5)
    
    main_action = tk.Button(main_action, text="Salir")
    main_action.grid(column=4, row=2, sticky=tk.E, padx=5, pady=5)

    main_action.mainloop()

Here I show you how it looks like when I run the code


Solution

  • The issue with the code is that you are using the same variable name "main_action" to represent both the main window and the buttons. This causes the buttons to be nested inside the label widget, resulting in the overlapping behavior that you are seeing.

    To fix the issue, you should use different variable names for the main window and the buttons. Here's a modified version of your code that should work as expected:

    import tkinter as tk
    
    main_action_window = tk.Tk()
    
    main_action_window.geometry("600x600")
    main_action_window.title("Keymanager")
    main_action_window.resizable(0, 0)
    
    main_action_window.columnconfigure(0, weight=1)
    main_action_window.columnconfigure(1, weight=3)
    main_action_window.columnconfigure(2, weight=4)
    
    main_action_label = tk.Label(main_action_window, text="Escoja que accion quiere realizar")
    main_action_label.grid(column=1, row=0, sticky=tk.E, padx=5, pady=5)
    
    main_action_button1 = tk.Button(main_action_window, text="Ver")
    main_action_button1.grid(column=1, row=2, sticky=tk.E, padx=5, pady=5)
    
    main_action_button2 = tk.Button(main_action_window, text="Añadir")
    main_action_button2.grid(column=2, row=2, sticky=tk.E, padx=5, pady=5)
    
    main_action_button3 = tk.Button(main_action_window, text="Eliminar")
    main_action_button3.grid(column=3, row=2, sticky=tk.E, padx=5, pady=5)
    
    main_action_button4 = tk.Button(main_action_window, text="Salir")
    main_action_button4.grid(column=4, row=2, sticky=tk.E, padx=5, pady=5)
    
    main_action_window.mainloop()
    

    In this modified code, we use different variable names for the main window ("main_action_window") and the label and buttons. This ensures that each widget is created independently and does not interfere with the positioning of other widgets.