简体   繁体   English

TKInter 插入滚动条时框架丢失网格布局

[英]TKInter frame losing grid layout when insert scrollbar

I'm trying to create an app with a frame with two frames inside, but I want one of then to be wider than the other... I found a way to do it using grid, but when I add a scrollbar on one of the frames it readjusts the grid and both frames get the same size.我正在尝试创建一个带有内部两个框架的框架的应用程序,但我希望其中一个比另一个更宽......我找到了一种使用网格的方法,但是当我在其中一个上添加滚动条时它重新调整网格的框架,并且两个框架的大小相同。

Here is the code working without the scrollbar:这是没有滚动条的代码:

def __init__(self, master=None):
    ctk.set_appearance_mode('system')
    ctk.set_default_color_theme('blue')

    self.__root = ctk.CTk() if master is None else ctk.CTkToplevel(master)
    self.__root. Configure(height=1500, width=944)
    self.__root.minsize(1500, 944)

    self.__main_container = ctk.CTkFrame(self.__root)

    self.__image_frame = ctk.CTkFrame(self.__main_container)
    self.__image_frame.configure(height=800, width=1000)

    self.__image_canvas = ctk.CTkCanvas(self.__image_frame)
    self.__image_canvas.configure(confine="true", cursor="crosshair")
    self.__image_canvas.grid(column=0, row=0, sticky="nsew")
    self.__image_canvas.bind("<ButtonPress-1>", self.__start_drawing)
    self.__image_canvas.bind("<ButtonRelease-1>", self.__end_drawing)
    self.__image_canvas.bind("<B1-Motion>", self.__draw_rectangle)

    self.__image_frame.grid(column=0, padx=10, pady=20, row=0, sticky="nsew")
    self.__image_frame.grid_propagate(0)
    self.__image_frame.grid_anchor("center")
    self.__image_frame.rowconfigure(0, weight=1)
    self.__image_frame.columnconfigure(0, weight=1)

    self.__boxes_frame = ctk.CTkFrame(self.__main_container)
    self.__boxes_frame.configure(height=800, width=400)
    self.__boxes_frame.grid(column=1, padx=10, pady=20, row=0, sticky="ns")

    self.__main_container.grid(column=0, padx=20, pady=40, row=0, sticky="ns")
    self.__main_container.grid_anchor("center")
    self.__main_container.rowconfigure(0, weight=1)

    self.__root.grid_anchor("center")
    self.__root.rowconfigure(0, weight=1)

    self.mainwindow = self.__root

工作代码生成此帧

And this is the code when I add the scrollbar and messes the grid这是我添加滚动条并弄乱网格时的代码

def __init__(self, master=None):
    ctk.set_appearance_mode('system')
    ctk.set_default_color_theme('blue')

    self.__root = ctk.CTk() if master is None else ctk.CTkToplevel(master)
    self.__root. Configure(height=1500, width=944)
    self.__root.minsize(1500, 944)

    self.__main_container = ctk.CTkFrame(self.__root)

    self.__image_frame = ctk.CTkFrame(self.__main_container)
    self.__image_frame.configure(height=800, width=1000)

    self.__image_canvas = ctk.CTkCanvas(self.__image_frame)
    self.__image_canvas.configure(confine="true", cursor="crosshair")
    self.__image_canvas.grid(column=0, row=0, sticky="nsew")
    self.__image_canvas.bind("<ButtonPress-1>", self.__start_drawing)
    self.__image_canvas.bind("<ButtonRelease-1>", self.__end_drawing)
    self.__image_canvas.bind("<B1-Motion>", self.__draw_rectangle)

    #------ adding the scrollbar -----
    self.__image_frame_vertical_scrollbar = ctk.CTkScrollbar(self.__image_frame, orientation="vertical")
    self.__image_frame_vertical_scrollbar.grid(column=1, row=0, sticky="ns")
    self.__image_frame_vertical_scrollbar.configure(command=self.__image_canvas.yview)
    self.__image_canvas.configure(yscrollcommand=self.__image_frame_vertical_scrollbar.set)
    #---------------------------------

    self.__image_frame.grid(column=0, padx=10, pady=20, row=0, sticky="nsew")
    self.__image_frame.grid_propagate(0)
    self.__image_frame.grid_anchor("center")
    self.__image_frame.rowconfigure(0, weight=1)
    self.__image_frame.columnconfigure(0, weight=1)

    self.__boxes_frame = ctk.CTkFrame(self.__main_container)
    self.__boxes_frame.configure(height=800, width=400)
    self.__boxes_frame.grid(column=1, padx=10, pady=20, row=0, sticky="ns")

    self.__main_container.grid(column=0, padx=20, pady=40, row=0, sticky="ns")
    self.__main_container.grid_anchor("center")
    self.__main_container.rowconfigure(0, weight=1)

    self.__root.grid_anchor("center")
    self.__root.rowconfigure(0, weight=1)

    self.mainwindow = self.__root

在此处输入图像描述

What is wrong with the grid definition that is messing things up?把事情搞砸的网格定义有什么问题? How can I set the __image_frame grid to it fills the __main_container keeping the desired dimensions?如何将 __image_frame 网格设置为填充 __main_container 并保持所需的尺寸?

Set the size of the canvas explicitly显式设置 canvas 的大小

self.__image_canvas = ctk.CTkCanvas(self.__image_frame, width=900)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM