简体   繁体   English

我不能在一个 tkinter 窗口中使用两个框架

[英]I can't use two frames in one tkinter window

i am developing a application with tkinter.我正在用 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... Tkinter 只允许一个窗口中的 1 帧吗?,orrr ...

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM