1. <tfoot id='eSOrZ'></tfoot>
      <bdo id='eSOrZ'></bdo><ul id='eSOrZ'></ul>

    <small id='eSOrZ'></small><noframes id='eSOrZ'>

  2. <legend id='eSOrZ'><style id='eSOrZ'><dir id='eSOrZ'><q id='eSOrZ'></q></dir></style></legend>
    1. <i id='eSOrZ'><tr id='eSOrZ'><dt id='eSOrZ'><q id='eSOrZ'><span id='eSOrZ'><b id='eSOrZ'><form id='eSOrZ'><ins id='eSOrZ'></ins><ul id='eSOrZ'></ul><sub id='eSOrZ'></sub></form><legend id='eSOrZ'></legend><bdo id='eSOrZ'><pre id='eSOrZ'><center id='eSOrZ'></center></pre></bdo></b><th id='eSOrZ'></th></span></q></dt></tr></i><div id='eSOrZ'><tfoot id='eSOrZ'></tfoot><dl id='eSOrZ'><fieldset id='eSOrZ'></fieldset></dl></div>

      Tkinter 帆布 &amp;带网格的滚动条

      时间:2023-07-23
      <tfoot id='72USZ'></tfoot>

      <i id='72USZ'><tr id='72USZ'><dt id='72USZ'><q id='72USZ'><span id='72USZ'><b id='72USZ'><form id='72USZ'><ins id='72USZ'></ins><ul id='72USZ'></ul><sub id='72USZ'></sub></form><legend id='72USZ'></legend><bdo id='72USZ'><pre id='72USZ'><center id='72USZ'></center></pre></bdo></b><th id='72USZ'></th></span></q></dt></tr></i><div id='72USZ'><tfoot id='72USZ'></tfoot><dl id='72USZ'><fieldset id='72USZ'></fieldset></dl></div>

      <small id='72USZ'></small><noframes id='72USZ'>

          <bdo id='72USZ'></bdo><ul id='72USZ'></ul>
            <tbody id='72USZ'></tbody>
              <legend id='72USZ'><style id='72USZ'><dir id='72USZ'><q id='72USZ'></q></dir></style></legend>
              1. 本文介绍了Tkinter 帆布 &amp;带网格的滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个画布在一个框架中

                I have a canvas in a frame

                photoFrame = Frame(centerFrame, width=250, height=190, bg="#EBEBEB")
                photoFrame.grid(row=0, column=1, sticky="nsew")
                photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
                photoCanvas.grid(row=0, column=0, sticky="nsew")
                

                我尝试用这个在我的画布上放置一个滚动条

                and I try to put a scrollbar to my canvas with this

                photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
                photoScroll.config(command=photoCanvas.yview)
                photoCanvas.config(yscrollcommand=photoScroll.set)
                photoScroll.grid(row=0, column=1, sticky="ns")
                

                滚动条出现但被禁用.你能帮帮我吗?

                The scrollbar appears but it's disabled. Can you help me please ?

                对不起,我的英语不好.

                Sorry for my bad english.

                在 for 循环中,我使用此代码添加了许多图像按钮

                In a for loop I add lots of Image button with this code

                element = Button(photoCanvas, image = listPhotos[i], borderwidth=0, height = 200, width = 200, bg="#EBEBEB")
                element.grid(row=rowPhoto, column=columnPhoto, padx=5, pady=5, sticky="nsew")
                

                终于有了这个

                    root = Tk()    
                    photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
                        
                
                    photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
                    photoCanvas.grid(row=0, column=0, sticky="nsew")
                    
                    for i in range(0, len(listPhotos), 1):
                       element = Button(photoCanvas, image = listPhotos[i], borderwidth=0, height = 200, width = 200, bg="#EBEBEB")
                       element.grid(row=rowPhoto, column=columnPhoto, padx=5, pady=5, sticky="nsew")
                    
                    photoScroll=Scrollbar(photoFrame,orient=VERTICAL)
                    photoScroll.config(command=photoCanvas.yview)
                    photoCanvas.config(yscrollcommand=photoScroll.set)
                    photoScroll.grid(row=0, column=1, sticky="ns")
                

                在我的应用程序中,紫色矩形是下一帧,我需要一个垂直滚动条

                in my app, the purple rectangle is the next frame and I need a vertical scrollbar

                有什么问题就说吧

                推荐答案

                滚动一组小部件的一种方法是将它们(使用 packgrid)放在里面一个框架并将这个框架放在画布中.

                One way to scroll a group of widgets is to put them (with grid of pack) inside a frame and put this frame inside a canvas.

                滚动工作的两个关键元素(除了将滚动条连接到画布之外)是:

                The two key elements (besides connecting the scrollbar to the canvas) for the scrolling to work are:

                • 使用 canvas.create_window(x, y, window=frame) 将框架放在画布内,以便将其视为画布项.
                • 使用 canvas.configure(scrollregion=canvas.bbox('all')) 每当框架大小发生变化时(例如添加新小部件后)更新画布滚动区域..李>
                • Use canvas.create_window(x, y, window=frame) to put the frame inside the canvas so that it is treated like a canvas item.
                • Update the canvas scrollregion each time the size of the frame changes (for instance after adding a new widget) with canvas.configure(scrollregion=canvas.bbox('all')).

                这是对问题代码的改编Python Tkinter scrollbar for frame,但使用 OP 问题中的小部件名称和 grid 而不是 pack:

                Here is an adaptation of the code of the question Python Tkinter scrollbar for frame, but using the widgets name from the OP's question and grid instead of pack:

                import tkinter as tk
                
                def update_scrollregion(event):
                    photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))
                
                root = tk.Tk()   
                
                
                photoFrame = tk.Frame(root, width=250, height=190, bg="#EBEBEB")
                photoFrame.grid()
                photoFrame.rowconfigure(0, weight=1) 
                photoFrame.columnconfigure(0, weight=1) 
                
                photoCanvas = tk.Canvas(photoFrame, bg="#EBEBEB")
                photoCanvas.grid(row=0, column=0, sticky="nsew")
                
                canvasFrame = tk.Frame(photoCanvas, bg="#EBEBEB")
                photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')
                
                for i in range(10):
                   element = tk.Button(canvasFrame, text='Button %i' % i, borderwidth=0, bg="#EBEBEB")
                   element.grid(padx=5, pady=5, sticky="nsew")
                
                photoScroll = tk.Scrollbar(photoFrame, orient=tk.VERTICAL)
                photoScroll.config(command=photoCanvas.yview)
                photoCanvas.config(yscrollcommand=photoScroll.set)
                photoScroll.grid(row=0, column=1, sticky="ns")
                
                canvasFrame.bind("<Configure>", update_scrollregion)
                
                root.mainloop()
                

                这篇关于Tkinter 帆布 &amp;带网格的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为战舰创建和初始化 5x5 网格 下一篇:如何在 Matplotlib(Numpy)中生成 MATLAB 图(插值)?

                相关文章

                <small id='rkDSy'></small><noframes id='rkDSy'>

                  <legend id='rkDSy'><style id='rkDSy'><dir id='rkDSy'><q id='rkDSy'></q></dir></style></legend>

                      <bdo id='rkDSy'></bdo><ul id='rkDSy'></ul>
                  1. <tfoot id='rkDSy'></tfoot>
                  2. <i id='rkDSy'><tr id='rkDSy'><dt id='rkDSy'><q id='rkDSy'><span id='rkDSy'><b id='rkDSy'><form id='rkDSy'><ins id='rkDSy'></ins><ul id='rkDSy'></ul><sub id='rkDSy'></sub></form><legend id='rkDSy'></legend><bdo id='rkDSy'><pre id='rkDSy'><center id='rkDSy'></center></pre></bdo></b><th id='rkDSy'></th></span></q></dt></tr></i><div id='rkDSy'><tfoot id='rkDSy'></tfoot><dl id='rkDSy'><fieldset id='rkDSy'></fieldset></dl></div>