【问题标题】:How to space out rows and columns in Python Tkinter如何在 Python Tkinter 中分隔行和列
【发布时间】:2014-11-24 22:27:38
【问题描述】:

我正在开发一个程序,它有一个使用画布的可滚动框架。但在这个框架内,我想设置一个瓷砖。但是,当我尝试分隔列和行时,它不起作用。有人知道为什么吗?我正在使用网格几何管理器。

代码:

def fields(self):
    frame_row = 0
    frame_column = 0
    row_count = 0
    color = "red"

    for i in range(10):
        self.frame = Frame(self.bfr2, bg=color, width=229, height=120)
        self.frame.grid(row=frame_row, column=frame_column)

        self.columnconfigure(frame_column, pad=3) #Where it is supposed to add the padding between the columns.
        self.rowconfigure(frame_row, pad=3) #Where it is supposed to add the padding between the rows.

        frame_column = frame_column + 1
        row_count = row_count + 1

        if row_count == 2:
            frame_row = frame_row + 1
            frame_column = 0
            row_count = 0

            if color == "red":
                color = "green"
            else:
                color = "red"

        if color == "red":
            color = "green"
        else:
            color = "red"

【问题讨论】:

  • “不起作用”是什么意思?你的程序崩溃了吗?你有错误吗?什么错误?如果它没有崩溃,它正在做什么你不希望它做的事情?
  • @BryanOakley 在字段函数中,我试图分隔我创建的帧,但它没有分隔它们,就像我没有使用 rowconfigure 和 columnconfigure 一样。
  • “间距”太模糊了。你想创建一个棋盘格吗?一个长排?一根高柱子?你想要每个彩色框架之间的空间吗?有多少空间?
  • @BryanOakley 是的,我正在尝试使其成为棋盘图案,但每行和列之间有 3 个像素或 5 个像素。

标签: python tkinter grid tile


【解决方案1】:

您将框架创建为self.bfr2 的子级,但您调用的是self.row_configureself.columnconfigure 而不是self.bfr2.rowconfigureself.bfr2.columnconfigure。您正在做的是为包含框架的父级而不是包含框架设置填充。

【讨论】:

  • 谢谢我一整天都被困在这上面。 :D
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
相关资源
最近更新 更多