python-3.xtkinterbuttonpositionpack

change the position of already packed widget in tkinter


Here is the code,

from tkinter import *
root = Tk()

update_button = Button(root, text='Update')
update_button.pack()


def button():
    frame1 = Frame(root)
    frame1.pack()

    button1 = Button(frame1, text="Button 1")
    button1.pack(side=LEFT)

    button2 = Button(frame1, text="Button 2")
    button2.pack(side=LEFT)


button()

root.mainloop()

I want button1 to be stacked upon button2 when I click update button. Help please.


Solution

  • I want button1 to be stacked upon button2 when I click update button.
    

    The problem can be fixed.

    That all!

    Snippet:

    from tkinter import *
    root = Tk()
    
    def button_1():     
        button1.pack(side=TOP)
    
    frame1 = Frame(root)    
    button1 = Button(frame1, text="Button 1")
    
    update_button = Button(root, text='Update', command=button_1)
    update_button.pack()
    
    def button():
        frame1.pack()
        
        button1.pack(side=LEFT)
    
        button2 = Button(frame1, text="Button 2")
        button2.pack(side=LEFT)
    
    
    button()
    
    root.mainloop()
    

    Screenshot:

    enter image description here