【问题标题】:Tkinter: Why the perimeter of a rectangle grid is not showing?Tkinter:为什么没有显示矩形网格的周长?
【发布时间】:2015-09-08 20:55:46
【问题描述】:

我正在尝试使用 Tkinter 将网格绘制成具有多行和多列的矩形。我几乎有了它,但我不知道为什么,画布中没有显示两条周边线。

import tkinter as tk

class GridWindow:
    def __init__(self, parent):
        self.myParent = parent

        self.myContainer1 = tk.Frame(parent)
        self.myContainer1.pack()

        self.cellwidth = 25
        self.cellheight = 25
        self.rect = {}

    def draw_grid(self, rows, columns):
        self.myCanvas = tk.Canvas(self.myContainer1)
        self.myCanvas.configure(width=self.cellheight*rows,
                                height=self.cellwidth*columns)
        self.myCanvas.pack(side=tk.RIGHT)

        for column in range(rows):
            for row in range(columns):
                x1 = column * self.cellwidth
                y1 = row * self.cellheight
                x2 = x1 + self.cellwidth
                y2 = y1 + self.cellheight
                self.rect[row, column] = self.myCanvas.create_rectangle(x1, y1, x2, y2, fill="white")

def runApp(rows, columns):
    root = tk.Tk()
    myapp = GridWindow(root)
    myapp.draw_grid(rows, columns)
    root.mainloop()

if __name__ == '__main__':
runApp(10, 10)

使用该代码,窗口如下所示:

我设法通过一些小技巧解决了这个问题,但我不知道它为什么会起作用。唯一的区别是现在我向每个画布维度添加 2 个像素,向每个单元格维度添加 4 个像素。代码如下:

import tkinter as tk

class GridWindow:
    def __init__(self, parent):
        self.myParent = parent

        self.myContainer1 = tk.Frame(parent)
        self.myContainer1.pack()

        self.cellwidth = 25
        self.cellheight = 25
        self.rect = {}

    def draw_grid(self, rows, columns):
        self.myCanvas = tk.Canvas(self.myContainer1)
        self.myCanvas.configure(width=self.cellheight*rows+2,
                                height=self.cellwidth*columns+2)
        self.myCanvas.pack(side=tk.RIGHT)

        for column in range(rows):
            for row in range(columns):
                x1 = column * self.cellwidth+4
                y1 = row * self.cellheight+4
                x2 = x1 + self.cellwidth
                y2 = y1 + self.cellheight
                self.rect[row, column] = self.myCanvas.create_rectangle(x1, y1, x2, y2, fill="white")

def runApp(rows, columns):
    root = tk.Tk()
    myapp = GridWindow(root)
    myapp.draw_grid(rows, columns)
    root.mainloop()

if __name__ == '__main__':
runApp(10, 10)

现在看起来像这样:

因此,如果有人可以解释我的这种行为,或者我做错了什么,我将不胜感激,欢迎提出任何建议。

【问题讨论】:

    标签: python user-interface python-3.x tkinter


    【解决方案1】:

    我认为问题与坐标系包括边框所需的空间有关。也就是说,如果画布有边框,并且您在 0,0 到 0,100 之间绘制一条线,则该线将被边框透支。默认情况下,画布将有一个或两个像素宽的边框。

    尝试将borderwidthhighlightthickness 属性都设置为零,以便画布的整个宽度和高度都可绘制。这样,您就不必在坐标中添加任何内容了。

    self.myCanvas.configure(borderwidth=0, highlightthickness=0)
    

    【讨论】:

    • -2 的值用于移除边框(在 tkinter 上)。
    • @dsgdfg:你在说什么?该值不是加法(或减法)。使用零值表示您希望边框宽度为零。使用 -2 只做零值。
    • a = Canvas(root), a.config("highlightthickness") is ('highlightthickness', 'highlightThickness', 'HighlightThickness', '1', '1') and a.config("bd") is ('borderwidth', 'borderWidth', 'BorderWidth', '0', '0') 这里我有一个像素,第二个像素值?
    • @dsgdfg:我不明白你的问题。 “我有一个像素,第二个像素值”是什么意思?
    • @Plorenzo:当我使用我建议的修改运行您的第一个代码块时,画布宽度为 250。您绘制的最后一个 x 和 y 坐标是 250。因为画布坐标从零,最后一个可见坐标将是 249。
    猜你喜欢
    • 2011-06-14
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2014-08-06
    相关资源
    最近更新 更多