0

I have two frames in the root. And I want to add a button in one of the frames. Both frames have different background colours. When I try to add a button in any of them, the frame that contains the button disappears.

Without button

from tkinter import *

root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")

Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)

Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)

root.mainloop()

Which results in...

enter image description here

With button

But after adding the button, with the following code...

from tkinter import *

root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")

Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)

Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)

b1 = Button (Rf, text="Load", fg="red", bg="black")
b1.pack(side=LEFT)

root.mainloop()

I get...

enter image description here

Now the button is visible but the frame and background colours are gone. What am I doing wrong?

Thanks for your help!

Community
  • 1
  • 1
  • I don't think the frame is gone, but the background color is reset to gray. Try setting the border width and relief of the frame and see whether it is actually gone. – acw1668 Feb 12 '19 at 11:55

1 Answers1

3

Your frame is actually disappearing it is just resize and that is why you cant see it. Add Rf.pack_propagate(False) to your frame it will prevent the frame from resizing when a new widget is added.

from tkinter import *

root= Tk()
root.geometry("1600x800+0+0")
root.title("ABC")

Rf=Frame(root,width=100, height=800, bg="black")
Rf.pack_propagate(False)
Rf.pack(side=RIGHT)

Lf=Frame(root,width=1500, height=800, bg="green")
Lf.pack(side=LEFT)

b1 = Button (Rf, text= "Load", fg= "red", bg="black")
b1.pack(side=LEFT)

root.mainloop()
cdw100100
  • 192
  • 1
  • 13