为什么 canvas 添加小部件时不使用滚动条滚动和自动调整大小?

Why canvas not scrolling with scrollbar and auto resizing when adding widgets?

当我将小部件放入 canvas 时,小部件会绕过 canvas 大小。但是当我滚动时,canvas 不会移动。
最重要的是,当我添加更多小部件时,canvas 会自动调整大小。我认为 canvas 不应该调整大小,因为我已经添加了滚动条?

这是我的代码:

from tkinter import *
from tkinter import ttk
import mysql.connector

class CanvasScroll:

    def on_resize(self,event):
        self.canvas.config(width = 1185, height = 530)

    def add_crew(self):
        crewFrame = CrewFrame()


    def callback_list(self):
        index = self.notebook.index(self.notebook.select()) + 1
        self.tmpframe = Try2(self.contentframe, index)


    def __init__(self, master):
        self.canvas = Canvas(master, width = 1185, height = 530, scrollregion = (0, 0, 1216, 700), bg = 'white', confine = True)
        self.canvas.grid(row = 2, column = 0, sticky = 'news')

        self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
        self.xscroll.grid(row = 3, column = 0, sticky = 'we')

        self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
        self.yscroll.grid(row = 2, column = 1, sticky = 'ns')

        self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
        self.canvas.bind("<Configure>", self.on_resize)


        self.option = ttk.Frame(master, height = 150, width = 1206)
        self.option.grid(row = 0, column = 0, sticky = 'we', columnspan = 5)

        self.addicon = PhotoImage(file = 'C:\Users\rain\Desktop\Fyosh!\logo\add.gif').subsample(2,2)
        self.btnAdd = ttk.Button(self.option, image = self.addicon, text = 'Add Crew', compound = TOP, command = self.add_crew)
        self.btnAdd.grid(row = 0, column = 0, padx = 50, pady = 5)

        self.updateicon = PhotoImage(file = 'C:\Users\rain\Desktop\Fyosh!\logo\update.gif').subsample(2,2)
        self.btnUpdate = ttk.Button(self.option, image = self.updateicon, text = 'Update Crew', compound = TOP)
        self.btnUpdate.grid(row = 0, column = 1, padx = 50)

        self.deleteicon = PhotoImage(file = 'C:\Users\rain\Desktop\Fyosh!\logo\delete.gif').subsample(2,2)
        self.btnDelete = ttk.Button(self.option, image = self.deleteicon, text = 'Delete Crew', compound = TOP)
        self.btnDelete.grid(row = 0, column = 2, padx = 50)

        self.reloadicon = PhotoImage(file = 'C:\Users\rain\Desktop\Fyosh!\logo\Refresh.png').subsample(7,7)
        self.reloadbtn = ttk.Button(self.option, image = self.reloadicon, command = self.callback_list, text = 'Load List', compound = TOP)
        self.reloadbtn.grid(row = 0, column = 3, padx = 50)

        self.tabframe = ttk.Frame(master, height = 20)
        self.tabframe.grid(row = 1, sticky = 'we')

        self.notebook = ttk.Notebook(self.tabframe)
        self.notebook.grid(row = 0, column = 0, sticky = 'we')

        db = mysql.connector.connect(user = 'root', password = '', host = 'localhost', database = 'fat2x_payroll')
        cursor = db.cursor()

        ships = ("SELECT * from tbl_shiplist")

        try:
            cursor.execute(ships)
            result = cursor.fetchall()
            self.tab = {}
            for row in result:
                self.tab[row[0]] = {
                    "shipname": ttk.Frame(self.notebook)
                    }
                self.notebook.add(self.tab[row[0]]["shipname"], text = row[1])
                db.close()

        except:
            messagebox.showinfo("alert", "ship list error")

        self.canvas.label = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 0)
        self.canvas.label1 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 1)
        self.canvas.label2 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 2)
        self.canvas.label3 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 3)
        self.canvas.label4 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 4)
        self.canvas.label5 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 5)
        self.canvas.label6 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 6)
        self.canvas.label7 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 7)
        self.canvas.label8 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 8)

def start():
    master = Tk()
    master.geometry("1206x690+10+10")
    master.resizable(0,0)
    master.title("Crew Panel")
    canvasScroll = CanvasScroll(master)

if __name__ == "__main__":
    start()

canvas 只能滚动嵌入在 canvas 上的对象。对于小部件,这意味着您已经为希望受滚动条影响的小部件调用了 canvas 的 create_window 方法。

如果你想用grid来组织widgets,并且希望这些widgets在一个可滚动的容器中,通常的方法是将这些widgets放在一个frame中,然后将frame添加到canvas 与 create_window。您可以在这个答案中看到此技术的示例:

至于自动调整大小,此行为与使用滚动条完全无关。如果您在小部件上使用 gridpack,除非您关闭几何传播,否则父级将始终调整大小。它是 Canvas 或 Frame 或 Toplevel,或任何其他 class 小部件都没有关系。