Home > database >  How to stack buttons on the right side with tkinter
How to stack buttons on the right side with tkinter

Time:10-16

this is my code:

from tkinter import *
root = Tk()

"root.attributes('-alpha', 0.3)"
root.attributes('-fullscreen', True)
root.attributes("-topmost", True)
root.wm_attributes("-transparentcolor", "grey")
root.configure(bg='grey')

def btn_command():
    print("Hallo")

class btn:
  def __init__(self, name, command):
    button = Button(root, text=name, command=command)
    button.config(height=10, width=10)
    button.pack(side=RIGHT)

btn("Clicker1", btn_command)
btn("Clicker2", btn_command)
btn("Clicker3", btn_command)

root.mainloop()

What it looks like

But I want to stack the buttons on the right side on top of each other everytime i create a new one using the class.

Thanks for helping!

CodePudding user response:

The solution is to create a frame specifically for the buttons, and put the frame on the right side. Then, inside the frame, add the buttons to the top.

First, create the frame:

button_frame = Frame(root)
button_frame.pack(side=RIGHT)

Next, create the buttons as children of the frame and pack them to the top:

class btn:
  def __init__(self, name, command):
    ...
    button = Button(button_frame, ...)
    button.pack(side=TOP)
    ...
  • Related