我正在使用 customtkinter 库用 python 制作一个应用程序。我正在使用 grid() 小部件管理器。到目前为止,我只有几个框架作为基本布局,但无法正确对齐它们。 问题是,我希望蓝色框架(stats_frame)是其下方的白色和黑色框架(control_frame 和 current_result_frame)大小的两倍。
它的样子:(https://i.sstatic.net/9VUQRAKN.png)
这是代码:
import customtkinter as ctk
class Neural_Net_GUI():
def __init__(self):
# Defining the window:
self.window = ctk.CTk()
self.window.geometry('1200x800')
self.window.columnconfigure(0, weight=3)
self.window.columnconfigure(1, weight=1)
self.window.rowconfigure(0, weight=1)
self.window.rowconfigure(1, weight=10)
# self.window.rowconfigure((2, 3, 4), weight=1)
# self.window.rowconfigure(1, weight=1)
# self.window.rowconfigure((2, 3), weight=1)
# Sample frames:
self.input_frame = ctk.CTkFrame(master=self.window, fg_color='green')
self.input_frame.grid(row=0, column=0, columnspan=2, sticky='swne')
self.graph_frame = ctk.CTkFrame(master=self.window, fg_color='red')
self.graph_frame.grid(row=1, column=0, sticky='swne')
self.left_frame = ctk.CTkFrame(master=self.window)
self.left_frame.columnconfigure(0, weight=1)
# self.left_frame.rowconfigure(0, weight=1)
self.left_frame.rowconfigure(0, weight=10)
self.left_frame.rowconfigure(1, weight=1)
self.left_frame.rowconfigure(2, weight=1)
self.stats_frame = ctk.CTkFrame(master=self.left_frame, fg_color='blue')
self.stats_frame.grid(row=0, column=0, sticky='nesw')
self.control_frame = ctk.CTkFrame(master=self.left_frame, fg_color='black')
self.control_frame.grid(row=1, column=0, sticky='nesw')
self.current_result_frame = ctk.CTkFrame(master=self.left_frame, fg_color='white')
self.current_result_frame.grid(row=2, column=0, sticky='nesw')
self.left_frame.grid(row=1, column=1, sticky='swne')
N = Neural_Net_GUI()
N.window.mainloop()
我尝试调整行的重量,增加行数并增加框架的行距,但没有任何效果。
有人知道如何解决这个问题吗?
您的行和列配置适用于 tkinter。
customtkinter
设置CTkFrame
的宽度和高度选项。
这会影响窗口的布局。 如果我们将所有框架的这些选项设置为相同的值,例如
width=0, height=0
,那么窗口将看起来像预期的那样。
import customtkinter as ctk
class Neural_Net_GUI():
def __init__(self):
# Defining the window:
self.window = ctk.CTk()
self.window.geometry('1200x800')
self.window.columnconfigure(0, weight=3)
self.window.columnconfigure(1, weight=1)
self.window.rowconfigure(0, weight=1)
self.window.rowconfigure(1, weight=10)
# Sample frames:
self.input_frame = ctk.CTkFrame(master=self.window, fg_color='green', width=0, height=0)
self.input_frame.grid(row=0, column=0, columnspan=2, sticky='swne')
self.graph_frame = ctk.CTkFrame(master=self.window, fg_color='red', width=0, height=0)
self.graph_frame.grid(row=1, column=0, sticky='swne')
self.left_frame = ctk.CTkFrame(master=self.window, width=0, height=0)
self.left_frame.columnconfigure(0, weight=1)
self.left_frame.rowconfigure(0, weight=2)
self.left_frame.rowconfigure(1, weight=1)
self.left_frame.rowconfigure(2, weight=1)
self.stats_frame = ctk.CTkFrame(master=self.left_frame, fg_color='blue', width=0, height=0)
self.stats_frame.grid(row=0, column=0, sticky='nesw')
self.control_frame = ctk.CTkFrame(master=self.left_frame, fg_color='black', width=0, height=0)
self.control_frame.grid(row=1, column=0, sticky='nesw')
self.current_result_frame = ctk.CTkFrame(master=self.left_frame, fg_color='white', width=0, height=0)
self.current_result_frame.grid(row=2, column=0, sticky='nesw')
self.left_frame.grid(row=1, column=1, sticky='swne')
N = Neural_Net_GUI()
N.window.mainloop()