Home > Software design >  I can't use two frames in one tkinter window
I can't use two frames in one tkinter window

Time:10-20

i am developing a application with tkinter. I want to display two frames in one window, one frame on the right side of the window and another one on the left side.

with open("C:/gui_assistant/res/settings/frame_amount.txt", "r") as f:
     check_amount = f.read()

     if "one" in check_amount:
        one_frame = tk.Frame(main, bg="#1a1919", relief=SUNKEN)
        one_frame.place(relx=0.85, rely=0, relwidth=0.5, relheight=1)

    elif "two" in check_amount:
        first_frame = tk.Frame(main, bg="#1a1919", relief=SUNKEN)
        first_frame.place(relx=0.85, rely=0, relwidth=0.5, relheight=1)
        second_frame = tk.Frame(main, bg="#1a1919", relief=SUNKEN)
        second_frame.place(relx=0, rely=0, relwidth=0.5, relheight=1)

So as you can tell, it first opens the text file, then checks the given amount, so if the text file says "one", then only one frame should be created, or if "two" then two frames should be created. Now, the text file says "two", but only one frame is being created after running the application. Does Tkinter only allow 1 frame in a window?, orrr...

CodePudding user response:

try this

from tkinter import *
main = Tk()
main.config(background='red')
def check():
    with open("frame_amount.txt", "r") as f:
        check_amount = f.read()
        s_w= int(main.winfo_width())
        s_h= int(main.winfo_height())
        if "one" in check_amount:
            one_frame = Frame(main,background='green')
            one_frame.place(x=0, y=0, width=s_w, height=s_h)

        elif "two" in check_amount:
            first_frame = Frame(main,background='blue')
            first_frame.place(x=0, y=0, width=s_w//2, height=s_h)
            second_frame = Frame(main,background='black')
            second_frame.place( x=s_w // 2, y= 0, width=s_w//2, height=s_h)
        b = Button(main,text='chack',command=check)
        b.place(x=1,y=1)
b = Button(main,text='chack',command=check)
b.place(x=1,y=1)

main.mainloop()
  • Related