将两个框架与相对宽度放在一起
Place two frames together with relative width
我正在尝试将两个框架放在另一个框架内,但我无法使用布局管理器完成此操作。
框架是从ttk导入的,灰色区域是边框的结果。我的目标是,第一个图像填充左侧的所有灰色区域,第二个图像填充右侧的灰色区域。最后我要用另一个小部件更改第二张图片。
我的代码是:
class Window(Frame):
def __init__(self, master, *args):
super().__init__(master, *args)
self.principal_frame = Frame(self.master)
self.principal_frame.grid(column=1, row=1, sticky='nsew')
self.master.columnconfigure(1, weight=1)
self.master.rowconfigure(1, weight=1)
self.widgets()
def tactical_window(self):
self.pages.select([self.third_frame])
def widgets(self):
self.image_pitch = PhotoImage(file=...)
self.pages = ttk.Notebook(self.principal_frame)
self.pages.grid(column=0, row=0, sticky='nsew')
self.third_frame = Frame(self.pages, bg='white')
self.third_frame.pack(fill=tk.BOTH, expand=True)
self.pages.add(self.third_frame)
# Third Frame
self.frame_tactic_pitch = ttk.Frame(self.third_frame, borderwidth=2)
self.frame_player_list = ttk.Frame(self.third_frame, borderwidth=3)
self.frame_pitch_tactic.pack(side=tk.LEFT, expand=True, fill=tk.BOTH, pady=2, padx=2)
self.frame_players_tactic.pack(side=tk.LEFT, expand=False, fill=tk.Y, pady=2, padx=2)
# Pitch
self.canvas_pitch = tk.Canvas(self.frame_tactic_pitch)
self.canvas_pitch.pack()
self.canvas_pitch.create_image(0, 0, image=self.image_pitch, anchor='nw')
self.canvas_pitch1 = tk.Canvas(self.frame_player_list)
self.canvas_pitch1.pack()
self.canvas_pitch1.create_image(0, 0, image=self.image_pitch, anchor='nw')
...
我正在尝试 self.frame_tactic_pitch
使用 ≈75% 的 self.third_frame
和 self.frame_player_list
剩余的。
而小部件canvas使用了self.frame_tactic_pitch
的总和,但是图像还算合身,放大window使图像变形也没关系。
一个简单的方法来实现你想要的是创建一个统一的组,通过为 uniform
选项给两列相同的值。然后,给每列一个比例权重。在您的情况下,它是 3 和 1。grid
将保持尺寸与重量严格成比例。
来自官方网格文档:
The -uniform option, when a non-empty value is supplied, places the column in a uniform group with other columns that have the same value for -uniform. The space for columns belonging to a uniform group is allocated so that their sizes are always in strict proportion to their -weight values.
它看起来像下面的例子。此示例将两个框架放在根 window 中,但通过将这些框架放在另一个框架中,该技术也同样有效。
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
f1 = tk.Frame(root, bd=1, relief="raised")
f2 = tk.Frame(root, bd=1, relief="raised")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=3, uniform="group1")
root.grid_columnconfigure(1, weight=1, uniform="group1")
f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")
root.mainloop()
我正在尝试将两个框架放在另一个框架内,但我无法使用布局管理器完成此操作。
框架是从ttk导入的,灰色区域是边框的结果。我的目标是,第一个图像填充左侧的所有灰色区域,第二个图像填充右侧的灰色区域。最后我要用另一个小部件更改第二张图片。 我的代码是:
class Window(Frame):
def __init__(self, master, *args):
super().__init__(master, *args)
self.principal_frame = Frame(self.master)
self.principal_frame.grid(column=1, row=1, sticky='nsew')
self.master.columnconfigure(1, weight=1)
self.master.rowconfigure(1, weight=1)
self.widgets()
def tactical_window(self):
self.pages.select([self.third_frame])
def widgets(self):
self.image_pitch = PhotoImage(file=...)
self.pages = ttk.Notebook(self.principal_frame)
self.pages.grid(column=0, row=0, sticky='nsew')
self.third_frame = Frame(self.pages, bg='white')
self.third_frame.pack(fill=tk.BOTH, expand=True)
self.pages.add(self.third_frame)
# Third Frame
self.frame_tactic_pitch = ttk.Frame(self.third_frame, borderwidth=2)
self.frame_player_list = ttk.Frame(self.third_frame, borderwidth=3)
self.frame_pitch_tactic.pack(side=tk.LEFT, expand=True, fill=tk.BOTH, pady=2, padx=2)
self.frame_players_tactic.pack(side=tk.LEFT, expand=False, fill=tk.Y, pady=2, padx=2)
# Pitch
self.canvas_pitch = tk.Canvas(self.frame_tactic_pitch)
self.canvas_pitch.pack()
self.canvas_pitch.create_image(0, 0, image=self.image_pitch, anchor='nw')
self.canvas_pitch1 = tk.Canvas(self.frame_player_list)
self.canvas_pitch1.pack()
self.canvas_pitch1.create_image(0, 0, image=self.image_pitch, anchor='nw')
...
我正在尝试 self.frame_tactic_pitch
使用 ≈75% 的 self.third_frame
和 self.frame_player_list
剩余的。
而小部件canvas使用了self.frame_tactic_pitch
的总和,但是图像还算合身,放大window使图像变形也没关系。
一个简单的方法来实现你想要的是创建一个统一的组,通过为 uniform
选项给两列相同的值。然后,给每列一个比例权重。在您的情况下,它是 3 和 1。grid
将保持尺寸与重量严格成比例。
来自官方网格文档:
The -uniform option, when a non-empty value is supplied, places the column in a uniform group with other columns that have the same value for -uniform. The space for columns belonging to a uniform group is allocated so that their sizes are always in strict proportion to their -weight values.
它看起来像下面的例子。此示例将两个框架放在根 window 中,但通过将这些框架放在另一个框架中,该技术也同样有效。
import tkinter as tk
root = tk.Tk()
root.geometry("300x200")
f1 = tk.Frame(root, bd=1, relief="raised")
f2 = tk.Frame(root, bd=1, relief="raised")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=3, uniform="group1")
root.grid_columnconfigure(1, weight=1, uniform="group1")
f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")
root.mainloop()