gpt4 book ai didi

python - Tkinter:如何在 Canvas 窗口中获取框架以扩展到 Canvas 的大小?

转载 作者:太空狗 更新时间:2023-10-29 17:24:46 26 4
gpt4 key购买 nike

所以我一直在使用 tkinter 中的 Canvas 小部件来创建一个充满标签的框架,它有一个滚动条。一切正常,除了框架仅扩展到放置在其中的标签的大小 - 我希望框架扩展到父 Canvas 的大小。

如果我对 Canvas 中的框架使用 pack(expand = True)(我已在下面的代码中注释掉)但滚动条不起作用,则可以轻松完成此操作。

这是适当的代码:

    ...
self.canvas = Canvas(frame, bg = 'pink')
self.canvas.pack(side = RIGHT, fill = BOTH, expand = True)

self.mailbox_frame = Frame(self.canvas, bg = 'purple')

self.canvas.create_window((0,0),window=self.mailbox_frame, anchor = NW)

#self.mailbox_frame.pack(side = LEFT, fill = BOTH, expand = True)

mail_scroll = Scrollbar(self.canvas, orient = "vertical",
command = self.canvas.yview)
mail_scroll.pack(side = RIGHT, fill = Y)

self.canvas.config(yscrollcommand = mail_scroll.set)

self.mailbox_frame.bind("<Configure>", self.OnFrameConfigure)


def OnFrameConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))

我还提供了一张带有彩色边框的图片,以便您了解我的意思。粉色区域是需要被mailbox_frame填充的 Canvas (可以看到右边的滚动条):

最佳答案

仅供将来引用,以防其他人需要知道:

        frame = Frame(self.bottom_frame)
frame.pack(side = LEFT, fill = BOTH, expand = True, padx = 10, pady = 10)

self.canvas = Canvas(frame, bg = 'pink')
self.canvas.pack(side = RIGHT, fill = BOTH, expand = True)

self.mailbox_frame = Frame(self.canvas, bg = 'purple')

self.canvas_frame = self.canvas.create_window((0,0),
window=self.mailbox_frame, anchor = NW)
#self.mailbox_frame.pack(side = LEFT, fill = BOTH, expand = True)

mail_scroll = Scrollbar(self.canvas, orient = "vertical",
command = self.canvas.yview)
mail_scroll.pack(side = RIGHT, fill = Y)

self.canvas.config(yscrollcommand = mail_scroll.set)

self.mailbox_frame.bind("<Configure>", self.OnFrameConfigure)
self.canvas.bind('<Configure>', self.FrameWidth)

def FrameWidth(self, event):
canvas_width = event.width
self.canvas.itemconfig(self.canvas_frame, width = canvas_width)

def OnFrameConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))

关于python - Tkinter:如何在 Canvas 窗口中获取框架以扩展到 Canvas 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29319445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com