Python tkinter place put frame to the bottom

99封情书 提交于 2021-01-01 07:24:06

问题


I have this code

class App(object):
    def __init__(self):
        self.root = Tk()
        self.root.attributes('-zoomed', True)

        f1 = Frame(self.root, bd=1, bg="green", relief=SUNKEN)
        f2 = Frame(self.root, bd=1, bg="red", relief=SUNKEN)
        f3 = Frame(self.root, bd=1, bg="blue", relief=SUNKEN)

        split = 0.5
        f1.place(relx=0, relheight=1, relwidth=split)
        f2.place(relx=split, relheight=1, relwidth=1.0 - split)
        f3.place(height=50, width=self.screen_width)

app = App()
app.root.mainloop()

and the output is this

How to put the blue frame to the bottom of the window/screen?


回答1:


I know you specifically asked about place, but honestly, you should not use place except in very rare circumstances. The layout you're trying to achieve is very easy with both grid and pack.

Using pack:

f3.pack(side="bottom", fill="x")
f1.pack(side="left", fill="both", expand=True)
f2.pack(side="right", fill="both", expand=True)

Using grid:

# row zero takes up all extra vertical space
# column 0 and column 1 take an equal amount of all horizontal space
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_columnconfigure(1, weight=1)

f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")
f3.grid(row=1, column=0, columnspan=2, sticky="ew")


来源:https://stackoverflow.com/questions/37017472/python-tkinter-place-put-frame-to-the-bottom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!