Tkinter Frame无法填充剩余空间。

huangapple go评论72阅读模式
英文:

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.

Screenshot

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.

Screenshot

Here is my code:

from tkinter import *

class CredsWindow:

	def __init__(self, root):

		root.title(&quot;Greenbox&quot;)
		root.geometry(&quot;300x200&quot;)
		root.attributes(&#39;-topmost&#39;, 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&#39;+{x}+{y}&#39;)

		title_bar = Frame(root, bg=&quot;#252525&quot;)
		title_bar.pack(anchor=N, expand=True, fill=X)
		title_bar.bind(&quot;&lt;Button-1&gt;&quot;, drag_window_start)
		title_bar.bind(&quot;&lt;B1-Motion&gt;&quot;, drag_window)

		title_label = Label(title_bar, text=&quot;Greenbox&quot;, bg=&quot;#252525&quot;, fg=&quot;white&quot;)
		title_label.pack(side=LEFT, pady=3, padx=2)

		close_button = Button(title_bar, text=&quot;X&quot;, bg=&quot;#252525&quot;, fg=&quot;white&quot;, relief=&quot;flat&quot;, command=root.quit)
		close_button.pack(side=RIGHT, pady=3, padx=2)

		
		mainframe = Frame(root, bg=&#39;#171717&#39;)
		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.

huangapple
  • 本文由 发表于 2023年7月28日 00:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定