Python Tkinter 的问题:画布中的框架

发布于 2024-12-10 09:20:31 字数 2868 浏览 0 评论 0原文

我正在尝试设置一系列可滚动的框架,因此我将它们嵌套到画布中。下面,我提供了一些演示该问题的示例代码。

但是,我遇到三个问题:

  1. 框架 c 似乎没有水平扩展以填充画布。
  2. 如果我使用多个框架 c,画布会滚动,但如果我尝试将 FrameRow 嵌套到单个框架 c 中,画布不会滚动更长的卷轴。
  3. 对于多个帧 c,最后一个 FrameRow 被切断。

按钮“左”和“右”演示了当按钮未嵌套在画布中时我得到的行为。

我更喜欢使用单个框架 c 并将所有 FrameRows 嵌套在其中,但我需要滚动才能正常工作。

import * from Tkinter
import FrameRow
root = Tk()
root.title("Testing")

containerFrame =  Frame(root)
containerFrame.config(bg="red")
containerFrame.pack(side=TOP, fill=BOTH, expand=1)

# Frame showing proper behavior
a = Frame(containerFrame, bg="black")
a.pack(side=TOP, fill=X, expand=0)

btnLeft = Button(a)
btnLeft.config(text="LEFT")
btnLeft.pack(side=LEFT, fill=X, expand=1)

btnRight = Button(a)
btnRight.config(text="RIGHT")
btnRight.pack(side=RIGHT, fill=X, expand=0)


# Canvas
canvas = Canvas(containerFrame)
scrollbar = Scrollbar(containerFrame, orient=VERTICAL)

scrollbar.config(command=canvas.yview)
canvas.config(bg="blue", yscrollcommand=scrollbar.set)
scrollbar.pack(side=RIGHT, fill=Y)
canvas.pack(side=LEFT, fill=BOTH, expand=1)

# Multiple Frames within Canvas    
frameRow = range(30)
c = range(30)
for r in xrange(0,30):
    c[r] = Frame(canvas)
    c[r].config(bg="green")

    frameRow[r] = FrameRow.FrameRow()
    frameRow[r].setFrame(c[r])
    frameRow[r].setRow(r)
    frameRow[r].createRow()

    c[r].pack(side=TOP, fill=X, expand=1)
    canvas.create_window(0, (r+1)*30, window=c[r], anchor="nw")

canvas.config(scrollregion = canvas.bbox(ALL))

# OR
# Single Frame within Canvas
##c = Frame(canvas)
##c.config(bg="green")
##
##frameRow = range(30)
##for r in xrange(0,30):
##    frameRow[r] = FrameRow.FrameRow()
##    frameRow[r].setFrame(c)
##    frameRow[r].setRow(r)
##    frameRow[r].createRow()
##
##c.pack(side=TOP, fill=X, expand=1)
##canvas.create_window(0, 0, window=c, anchor="nw")

root.mainloop()

FrameRow 类定义为:

from Tkinter import *

class FrameRow():
    _frame = "Default"
    _row = 0

    def setFrame(self, frame):
        self._frame = frame

    def setRow(self, row):
        self._row = row

    def createRow(self):
        self.frameRow = Frame(self._frame)
        self.frameRow.config(bg="yellow")
        self.frameRow.pack(side=TOP, fill=X, expand=1)

        self.btn1 = Button(self.frameRow)
        self.btn1.config(text="Button "+str(self._row)+"A")
        self.btn1.pack(side=LEFT, fill=X, expand=1)

        self.btn2 = Button(self.frameRow)
        self.btn2.config(text="Button "+str(self._row)+"B")
        self.btn2.pack(side=RIGHT, anchor=E, fill=X, expand=0)

在让 Framec 扩展至画布宽度时是否缺少某些内容?

我在滚动和单帧c方面做错了什么吗?

是否有任何不错的教程或示例可以展示我应该如何做到这一点?

谢谢!

I'm trying to set up a scrollable series of Frames, so I've nested them into a Canvas. Below, I've included some sample code that demonstrates the problem.

However, I have three issues:

  1. Frame c doesn't seem to expand horizontally to fill the canvas.
  2. If I use multiple frames c, the canvas scrolls, but if I try to nest my FrameRows into a single frame c, the canvas no longer scrolls.
  3. With multiple frames c, the last of the FrameRows is cut off.

Buttons "Left" and "Right" demonstrate the behavior I get when the buttons are not nested in a Canvas.

I'd prefer using the single Frame c and nesting all FrameRows in that, but I would need the scrolling to work properly for that.

import * from Tkinter
import FrameRow
root = Tk()
root.title("Testing")

containerFrame =  Frame(root)
containerFrame.config(bg="red")
containerFrame.pack(side=TOP, fill=BOTH, expand=1)

# Frame showing proper behavior
a = Frame(containerFrame, bg="black")
a.pack(side=TOP, fill=X, expand=0)

btnLeft = Button(a)
btnLeft.config(text="LEFT")
btnLeft.pack(side=LEFT, fill=X, expand=1)

btnRight = Button(a)
btnRight.config(text="RIGHT")
btnRight.pack(side=RIGHT, fill=X, expand=0)


# Canvas
canvas = Canvas(containerFrame)
scrollbar = Scrollbar(containerFrame, orient=VERTICAL)

scrollbar.config(command=canvas.yview)
canvas.config(bg="blue", yscrollcommand=scrollbar.set)
scrollbar.pack(side=RIGHT, fill=Y)
canvas.pack(side=LEFT, fill=BOTH, expand=1)

# Multiple Frames within Canvas    
frameRow = range(30)
c = range(30)
for r in xrange(0,30):
    c[r] = Frame(canvas)
    c[r].config(bg="green")

    frameRow[r] = FrameRow.FrameRow()
    frameRow[r].setFrame(c[r])
    frameRow[r].setRow(r)
    frameRow[r].createRow()

    c[r].pack(side=TOP, fill=X, expand=1)
    canvas.create_window(0, (r+1)*30, window=c[r], anchor="nw")

canvas.config(scrollregion = canvas.bbox(ALL))

# OR
# Single Frame within Canvas
##c = Frame(canvas)
##c.config(bg="green")
##
##frameRow = range(30)
##for r in xrange(0,30):
##    frameRow[r] = FrameRow.FrameRow()
##    frameRow[r].setFrame(c)
##    frameRow[r].setRow(r)
##    frameRow[r].createRow()
##
##c.pack(side=TOP, fill=X, expand=1)
##canvas.create_window(0, 0, window=c, anchor="nw")

root.mainloop()

The FrameRow class is defined as:

from Tkinter import *

class FrameRow():
    _frame = "Default"
    _row = 0

    def setFrame(self, frame):
        self._frame = frame

    def setRow(self, row):
        self._row = row

    def createRow(self):
        self.frameRow = Frame(self._frame)
        self.frameRow.config(bg="yellow")
        self.frameRow.pack(side=TOP, fill=X, expand=1)

        self.btn1 = Button(self.frameRow)
        self.btn1.config(text="Button "+str(self._row)+"A")
        self.btn1.pack(side=LEFT, fill=X, expand=1)

        self.btn2 = Button(self.frameRow)
        self.btn2.config(text="Button "+str(self._row)+"B")
        self.btn2.pack(side=RIGHT, anchor=E, fill=X, expand=0)

Is there something I'm missing in getting Frame(s) c to expand to the width of the canvas?

Am I doing something wrong with the scrolling and the single Frame c?

Are there any decent tutorials or examples that show how I should be doing this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2024-12-17 09:20:31

看起来您既将窗口打包在画布中又创建了一个窗口对象。老实说,我从来没有这样做过,但我认为包装没有任何好处。

当您创建作为画布中的对象的小部件时,它将不会扩展以填充画布。您需要做的是创建与画布的 事件的绑定。每当画布大小发生变化(以及其他几次)时,都会触发此事件。然后,您可以调整每个框架的大小以适合画布的宽度。

既然您说您更喜欢使用单个框架,将其他行嵌套在其中,那么这可能是最简单的解决方案。但同样,这个单一框架不会增大或缩小以适应画布,您必须使用 绑定来完成此操作。

确保在使用 create_window 将框架添加到画布后配置画布的滚动区域。看起来您忘记在注释掉的代码中执行此操作,这肯定会阻止画布滚动。

It looks like you are both packing the window in a canvas and creating a window object. I honestly have never done that but I don't think the packing does any good.

When you create a widget that is an object in a canvas, it will not expand to fill the canvas. What you'll need to do is create a binding to the <Configure> event of the canvas. This event will fire whenever the canvas changes size (and a few other times). You can then resize each frame to fit the width of the canvas.

Since you say you prefer using a single frame where you nest the other rows inside it, that is probably the easiest solution. But again, this single frame won't grow or shrink to fit the canvas, you'll have to do that with a <Configure> binding.

Make sure that you configure the scrollregion of the canvas after adding the frame to the canvas using create_window. It looks like you're forgetting to do that in the commented-out code, and that would definitely prevent the canvas from scrolling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文