Python Tkinter - 包含可扩展小部件的可滚动画布
我正在使用 Python Tkinter,我想在框架或画布中放置可变数量的文本框小部件。文本框沿着框架垂直排列,因此第一个位于顶部,第二个位于下面,等等。我可以将所有按钮、列表框等小部件放在 GUI 的“左侧部分”中,而“右侧部分”将仅包含文本框小部件。我希望文本框小部件在主窗口最大化时水平展开,但由于这些小部件的数量可变,因此包含文本框的“右侧部分”也需要能够垂直滚动才能查看全部内容。
目前,我正在使用 Canvas.create_window 将可变数量的文本框添加到画布中,虽然我可以滚动画布以查看所有文本框,但当我调整窗口大小时,它们不会水平展开。我有一个备用 GUI,它使用“右侧部分”的框架,它允许小部件水平扩展,但如果打包太多,我无法滚动框架以查看其他文本框,因为我无法滚动绑在框架上的酒吧。
有没有办法绕过这种权衡?
I'm using Python Tkinter and I want to place a variable number of text box widgets in a frame or canvas. The text boxes are packed vertically down the frame, so the first one is on top, the second one is found below, etc.. I can have all the button, listbox, etc widgets in a "left section" of the GUI, while a "right section" will only contain the text box widgets. I want the text box widgets to horizontally expand when the master window is maximized, but because there's a variable number of these widgets, the "right section" containing the text boxes also needs to be able to vertically scroll to view them all.
Currently, I'm using Canvas.create_window to add my variable number of text boxes to the canvas, and while I can scroll the canvas to view all the text boxes, they do not horizontally expand when I resize the window. I have an alternate GUI that uses a frame for the "right section", which allows the widgets to horizontally expand, but if too many are packed, I cannot scroll the frame to see the additional text boxes because I can't have a scroll bar tied to a frame.
Is there any way around this trade-off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案非常简单:绑定到画布的
事件 - 这将导致每当画布小部件调整大小时调用您的回调。然后,您只需获取画布的宽度并使用它来迭代调整所有嵌入窗口的大小。The solution is pretty simple: bind to the
<Configure>
event of the canvas -- this will cause your callback to be called whenever the canvas widget is resized. You then simply need to get the width of the canvas and use that to iteratively resize all the embedded windows.