代码之家  ›  专栏  ›  技术社区  ›  tomasvana10

更改ctk.set_widget_scalending时,具有可滚动ctk小部件的自定义交互画布不会调整大小

  •  0
  • tomasvana10  · 技术社区  · 2 年前

    我正在创建一个水平滚动的画布,其中有一个滚动条来控制 ctkframe 。在这个 ctk框架 ,我已经为我的填字游戏浏览器打包了信息块。使用时 customtkinter 小部件缩放为1.0填字游戏块完全适合画布,然而,当这个值增加时,它们会被切断,我还没有找到一种方法来调整画布的大小,以适应这种大小差异,而不会破坏一切。

    以下是水平滚动框对象的代码片段:

    class HorizontalScrollFrame(ctk.CTkFrame):
        def __init__(self, container, master):
            super().__init__(container)
            self.master = master
            # Make the scrollbar
            h_scrollbar = ctk.CTkScrollbar(self, orientation="horizontal")
            h_scrollbar.pack(fill="x", side="bottom", expand=False)
            
            # make the canvas, link it to `h_scrollbar``
            self.canvas = ctk.CTkCanvas(self, bd=7.5, highlightthickness=1, 
                                        xscrollcommand=h_scrollbar.set, background=canvas_bg_colour)
            self.canvas.pack(side="bottom", fill="both", expand=True)
            # whenever the user resizes the window, change the scrollregion of h_scrollbar to the 
            # dimensions of the canvas
            self.canvas.bind('<Configure>', 
                             lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
            h_scrollbar.configure(command=self.canvas.xview)
    
            # this is the frame that is moved by `h_scrollbar` within `self.canvas`
            self.scrollable_frame = ctk.CTkFrame(self.canvas, corner_radius=0)
            
            self.canvas.create_window(0, 0, window=self.scrollable_frame, anchor="w")
    

    下面是另一个代码片段,展示了我如何将小部件放入滚动框中:

    # This was normally a class by the way but i removed it so there is less stuff to look at
    
    # This contains the horizontal scroll frame
    self.container = ctk.CTkFrame(self)
    
    # Instantiating the scroll frame (dont worry about self.master its for something else)
    self.horizontal_scroll_frame = HorizontalScrollFrame(self.container, self.master) 
    
    self.container.grid(row=0, column=0, sticky="ew")
    self.horizontal_scroll_frame.pack(expand=True, fill="both")
    
    def _generate_crossword_blocks(self):
        file_name_example = "capitals"
        # `CrosswordInfoBlock` is just a frame with some widgets placed in it, nothing else
        block = CrosswordInfoBlock(self.horizontal_scroll_frame.scrollable_frame, name=file_name_example)
        
        # packing the block in self.horizontal_scroll_frame.scrollable_frame
        block.pack(side="left", padx=5)
    

    有人知道如何在使用ctk.set_widget_scalending将小部件缩放到更大的尺寸时,使画布有足够的空间来配合小部件吗?

    编辑:当将小部件缩放设置为低于1.0的值时,解决方案还应该能够减小画布的大小。

    0 回复  |  直到 2 年前
        1
  •  0
  •   tomasvana10    2 年前

    仔细一看,答案其实很明显。

    HorizontalScrollFrame 类,它从容器继承 horizontal_scroll_frame ,我必须将画布的高度设置为容器所需的高度:

    self.canvas = ctk.CTkCanvas(self, bd=7.5, highlightthickness=1, xscrollcommand=h_scrollbar.set, 
                                background=canvas_bg_colour, 
                                height=self.container.winfo_reqheight()) # < here