我正在用 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 只允許一個視窗中的 1 幀嗎?,orrr ...
uj5u.com熱心網友回復:
嘗試這個
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327491.html
下一篇:繼承類回傳NoneType實體
