是什么导致tkinter标签小部件周围出现白色边框?

时间:2019-01-23 21:25:20

标签: python tkinter

我正在创建一个棋盘,并为每个棋盘零件创建Canvas小部件。我在面板上贴了标签,但是显示时带有这个奇怪的白色边框,而不是我不知道如何删除。

def createGrid(self):
    colour = True
    self.grid_list = []
    for rows in range(8):
        label = Label(self.canvas, text='{}'.format(rows+1), bg='#727272', highlightthickness=0)
        label.grid(row=rows, column=0)
        row_list = []
        self.grid_list.append(row_list)
        colour = not colour
        for columns in range(8):
            label = Label(self.canvas, text='{}'.format(self.letters[columns]), bg='#727272',
                          fg='white', highlightthickness=0, height = 2)
            label.grid(row=8, column=columns+1)
            if colour:
                grid_piece = Canvas(self.canvas, width=self.size / 8, height=self.size / 8, bg=self.colour1,
                                    highlightthickness=0, highlightbackground='#727272')
                grid_piece.grid(row=rows, column=columns+1, padx = 0, pady = 0)
            else:
                grid_piece = Canvas(self.canvas, width=self.size / 8, height=self.size / 8, bg=self.colour2,
                                    highlightthickness = 0)
                grid_piece.grid(row=rows, column=columns+1)
            colour = not colour
            row_list.append(grid_piece)

边界怪异的木板

1 个答案:

答案 0 :(得分:0)

您的问题中没有足够的代码供其他人运行,因此我添加了自己的代码,经过大量的尝试和错误,能够重现该问题。我认为边界是由于未在所有需要的地方都指定borderwidth=0, highlightthickness=0引起的,因此我在Frame子类及其包含的主要Canvas周围添加了边界。

结果如下:

screenshot of script running with no white border shown

这是使用的代码:

from tkinter import *


class Foo(Frame):
    def __init__(self, *args):
        super(Foo, self).__init__(*args, bg='#727272', borderwidth=0,
                                  highlightthickness=0)
        self.pack()
        self.canvas = Canvas(self, borderwidth=0, bg='#727272', highlightthickness=0)
        self.canvas.pack()
        self.letters = list('ABCDEFHI')
        self.size = 768
        self.colour1 = '#7C1900'
        self.colour2 = '#FFDFC4'

    def createGrid(self):
        colour = True
        self.grid_list = []
        for rows in range(8):
            label = Label(self.canvas, text='{}'.format(rows+1), bg='#727272',
                          highlightthickness=0)
            label.grid(row=rows, column=0)
            row_list = []
            self.grid_list.append(row_list)
            colour = not colour
            for columns in range(8):
                label = Label(self.canvas, text='{}'.format(self.letters[columns]),
                              bg='#727272', fg='white', highlightthickness=0, height=2)
                label.grid(row=8, column=columns+1)
                if colour:
                    grid_piece = Canvas(self.canvas, width=self.size / 8,
                                        height=self.size / 8, bg=self.colour1,
                                        highlightthickness=0,
                                        highlightbackground='#727272')
                    grid_piece.grid(row=rows, column=columns+1, padx=0, pady=0)
                else:
                    grid_piece = Canvas(self.canvas, width=self.size / 8,
                                        height=self.size / 8, bg=self.colour2,
                                        highlightthickness=0)
                    grid_piece.grid(row=rows, column=columns+1)
                colour = not colour
                row_list.append(grid_piece)

root = Tk()
foo = Foo()
foo.createGrid()
root.mainloop()