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

2024-04-27 01:07:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用tkinter开发一个应用程序。我想在一个窗口中显示两个帧,一个在窗口的右侧,另一个在左侧

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)

如您所知,它首先打开文本文件,然后检查给定的数量,因此如果文本文件显示“一”,则只应创建一个帧,或者如果显示“两”,则应创建两个帧。现在,文本文件显示“两个”,但在运行应用程序后只创建一个帧。Tkinter是否只允许窗口中有一帧?或者


Tags: maincheckplaceamountoneframetkbg
1条回答
网友
1楼 · 发布于 2024-04-27 01:07:06

试试这个

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

相关问题 更多 >