英文:
Tkinter Frame won't fill remaining space
问题
以下是您要翻译的内容:
"I am just trying to make the green frame fill the remaining empty space of a window, but I can only get it to fill the bottom half no matter what I do.
Here is my code:
from tkinter import *
class CredsWindow:
def __init__(self, root):
root.title("Greenbox")
root.geometry("300x200")
root.attributes('-topmost', True)
self.drag_offsetx = IntVar()
self.drag_offsety = IntVar()
def drag_window_start(v):
self.drag_offsetx = v.x
self.drag_offsety = v.y
def drag_window(v):
x = root.winfo_pointerx()-self.drag_offsetx
y = root.winfo_pointery()-self.drag_offsety
root.geometry(f'+{x}+{y}')
title_bar = Frame(root, bg="#252525")
title_bar.pack(anchor=N, expand=True, fill=X)
title_bar.bind("<Button-1>", drag_window_start)
title_bar.bind("<B1-Motion>", drag_window)
title_label = Label(title_bar, text="Greenbox", bg="#252525", fg="white")
title_label.pack(side=LEFT, pady=3, padx=2)
close_button = Button(title_bar, text="X", bg="#252525", fg="white", relief="flat", command=root.quit)
close_button.pack(side=RIGHT, pady=3, padx=2)
mainframe = Frame(root, bg='#171717')
mainframe.pack(side=TOP, expand=True, fill=BOTH)
root = Tk()
CredsWindow(root)
root.mainloop()
英文:
I am just trying to make the green frame fill the remaining empty space of a window, but I can only get it to fill the bottom half no matter what I do.
Here is my code:
from tkinter import *
class CredsWindow:
def __init__(self, root):
root.title("Greenbox")
root.geometry("300x200")
root.attributes('-topmost', True)
self.drag_offsetx = IntVar()
self.drag_offsety = IntVar()
def drag_window_start(v):
self.drag_offsetx = v.x
self.drag_offsety = v.y
def drag_window(v):
x = root.winfo_pointerx()-self.drag_offsetx
y = root.winfo_pointery()-self.drag_offsety
root.geometry(f'+{x}+{y}')
title_bar = Frame(root, bg="#252525")
title_bar.pack(anchor=N, expand=True, fill=X)
title_bar.bind("<Button-1>", drag_window_start)
title_bar.bind("<B1-Motion>", drag_window)
title_label = Label(title_bar, text="Greenbox", bg="#252525", fg="white")
title_label.pack(side=LEFT, pady=3, padx=2)
close_button = Button(title_bar, text="X", bg="#252525", fg="white", relief="flat", command=root.quit)
close_button.pack(side=RIGHT, pady=3, padx=2)
mainframe = Frame(root, bg='#171717')
mainframe.pack(side=TOP, expand=True, fill=BOTH)
root = Tk()
CredsWindow(root)
root.mainloop()
答案1
得分: 0
你需要在这个语句中移除 `expand=True`:
title_bar.pack(anchor=N, expand=True, fill=X)
当你将其设为 `True` 时,你在告诉 `pack` 为该部件分配额外的空间。你还在打包 `mainframe` 时添加了 `expand=True`,这意味着所有未使用的空间将在 `title_bar` 和 `mainframe` 之间均匀分配。
英文:
You need to remove expand=True
in this statement:
title_bar.pack(anchor=N, expand=True, fill=X)
When you set it to True
, you're telling pack
to have it allocate extra space to that widget. You've also added expand=True
when packing mainframe
, which means that all unused space will be evenly distributed between title_bar
and mainframe
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论