-1

I have 2 frames, one of them has a button "click". On clicking that button, the same button should be destroyed from the original frame and move to frame2. How to achieve it using tkinter.

Ishaan Joshi
  • 49
  • 1
  • 4
  • You cannot *move* widget from one frame to another frame. You need to destroy the widget from one frame and create new one in other frame. – acw1668 May 25 '21 at 04:54
  • 1
    Does this answer your question? [Can you change a widget's parent in python tkinter?](https://stackoverflow.com/questions/6285648/can-you-change-a-widgets-parent-in-python-tkinter) – acw1668 May 25 '21 at 04:56
  • What I really wanted to know is that if there is a way to destroy a widget from one frame and create a new widget with the same attributes as the destroyed one on a new frame. In this case the widget is the button and the text part of the newly created button should be the same as the destroyed one. – Ishaan Joshi May 25 '21 at 08:07
  • You can use `.config()` to get most of the configurations (not including reference to function) of the button. Then use this configurations to create the button again. – acw1668 May 25 '21 at 08:31

1 Answers1

0

there is tow frames frame1 and frame2 first we want to show this button using pack(). now we want to add function on command of my_btn then we want to destroy that button and re define it in the second frame.

final code:

from tkinter import *
window = Tk()
frame1 = Frame(window, width=50, height=50, bg='red')
frame2 = Frame(window, width=50, height=50, bg='blue')
frame1.propagate(0); frame2.propagate(0)
frame1.pack(); frame2.pack()

def click():
    global my_btn
    my_btn.destroy()
    my_btn = Button(master=frame2)
    my_btn.pack()

my_btn = Button(master=frame1, command=click)
my_btn.pack()

window.mainloop()

Hope this works.

Shihab
  • 94
  • 10