You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python/Tkinter全屏界面响应式调整:底部固定顶部扩展

Fixing Responsive Layout in Tkinter: Keep Bottom Section Fixed, Expand Top Section on Fullscreen

Hey there! Let's get your Tkinter layout behaving exactly how you want it. The main issue with your current code is that you're not telling the top frame and listbox to expand into the available space when the window resizes (including fullscreen mode). Here's how to fix it, step by step:

Key Changes Needed

  • Top Frame Expansion: Add expand=True to the topFrame.pack() call. This tells Tkinter to allocate all remaining vertical space to the top frame when the window grows.
  • Listbox Expansion: Similarly, add expand=True to listbox.pack() so the listbox fills the entire top frame instead of just fitting its content.
  • Bottom Frame Stability: Your bottom frame is already on the right track with side=BOTTOM and fill=X, but we can make its behavior explicit by adding anchor=S (this reinforces that it stays anchored to the bottom, even if other widgets change).
  • Variable Cleanup: You reused the b1 variable for all buttons—this doesn't break functionality, but using unique names makes it easier to reference individual buttons later if you need to modify them.

Modified Full Code

from tkinter import *

root = Tk()
root.state('zoomed')  # Optional: Start window in fullscreen/zoomed mode

# Top Frame - now expands to fill remaining space
topFrame = Frame(root)
topFrame.pack(side=TOP, fill=BOTH, expand=True)

scrollbar = Scrollbar(topFrame)
scrollbar.pack(side=RIGHT, fill=Y)

# Listbox - now fills the entire topFrame
listbox = Listbox(topFrame)
listbox.pack(fill=BOTH, expand=True)

for i in range(100):
    listbox.insert(END, i)

# Attach scrollbar to listbox
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

# Bottom Frame - stays fixed at the bottom, stretches horizontally
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM, fill=X, anchor=S)

# Bottom Left Frame
b_leftFrame = Frame(bottomFrame)
b_leftFrame.pack(side=LEFT, fill=X, padx=5, pady=5)  # Added padding for cleaner spacing

# Labels
label1 = Label(b_leftFrame, text="Title")
label1.grid(row=0, column=0, sticky=E, padx=2)
label2 = Label(b_leftFrame, text="Author")
label2.grid(row=0, column=2, sticky=E, padx=2)
label3 = Label(b_leftFrame, text="Publisher")
label3.grid(row=0, column=4, sticky=E, padx=2)
label4 = Label(b_leftFrame, text="Year")
label4.grid(row=0, column=6, sticky=E, padx=2)
label5 = Label(b_leftFrame, text="Translator")
label5.grid(row=0, column=8, sticky=E, padx=2)

# Entry Widgets
title_text = StringVar()
entry1 = Entry(b_leftFrame, textvariable=title_text)
entry1.grid(row=0, column=1, padx=2)

author_text = StringVar()
entry2 = Entry(b_leftFrame, textvariable=author_text)
entry2.grid(row=0, column=3, padx=2)

publisher_text = StringVar()
entry3 = Entry(b_leftFrame, textvariable=publisher_text)
entry3.grid(row=0, column=5, padx=2)

year_text = StringVar()
entry4 = Entry(b_leftFrame, textvariable=year_text)
entry4.grid(row=0, column=7, padx=2)

translator_text = StringVar()
entry5 = Entry(b_leftFrame, textvariable=translator_text)  # Fixed duplicate entry4 variable
entry5.grid(row=0, column=9, padx=2)

# Bottom Right Frame
b_rightFrame = Frame(bottomFrame)
b_rightFrame.pack(side=RIGHT, padx=5, pady=5)

# Buttons with unique variable names
view_btn = Button(b_rightFrame, text="View All", width=12)
view_btn.grid(row=2, column=3, pady=2)

search_btn = Button(b_rightFrame, text="Search Entry", width=12)
search_btn.grid(row=3, column=3, pady=2)

add_btn = Button(b_rightFrame, text="Add Entry", width=12)
add_btn.grid(row=4, column=3, pady=2)

update_btn = Button(b_rightFrame, text="Update selected", width=12)
update_btn.grid(row=5, column=3, pady=2)

delete_btn = Button(b_rightFrame, text="Delete selected", width=12)
delete_btn.grid(row=6, column=3, pady=2)

close_btn = Button(b_rightFrame, text="Close", width=12)
close_btn.grid(row=7, column=3, pady=2)

root.mainloop()

Extra Notes

  • I added root.state('zoomed') to start the window in fullscreen/zoomed mode (you can remove this if you prefer a default window size).
  • Added padx and pady to widgets for better spacing—this makes the UI look less cramped.
  • Fixed the duplicate entry4 variable for the translator entry (reusing variables like that could cause unexpected behavior later).

Now when you resize the window or go fullscreen, the top section with the listbox will expand to fill all available space above the fixed bottom toolbar.

内容的提问来源于stack exchange,提问作者eoeroglu

火山引擎 最新活动