我正在尝试在python中使用tkinter编写一个小的simon说游戏,但存在一些间距问题。在我的代码中,我创建了四个按钮,每种颜色各一个,并将它们分隔在不同的列中。然后,我创建了两个标签,边框用于分隔按钮。有一个水平标签和一个垂直标签。这两个标签的代码相似,但有高度和宽度等开关。但是,我发现水平标签的显示高度要比垂直标签的显示高度高。
我的代码如下:
from Tkinter import *
var = 100
def colour(col):
print col
root = Tk()
for x in range(2 * var + 1):
Grid.columnconfigure(root, x, weight=1, minsize=2)
Grid.rowconfigure(root, x, weight=1, minsize=2)
red = Button(root, bg="RED", bd=0, relief=FLAT, command=lambda: colour("RED"))
red.grid(row=0, column=0, rowspan=var, columnspan=var, sticky=N+W+E+S)
blue = Button(root, bg="BLUE", bd=0, relief=FLAT, command=lambda: colour("BLUE"))
blue.grid(row=0, column=var+1, rowspan=var, columnspan=var, sticky=N+W+E+S)
green = Button(root, bg="GREEN", bd=0, relief=FLAT, command=lambda: colour("GREEN"))
green.grid(row=var+1, column=0, rowspan=var, columnspan=var, sticky=N+W+E+S)
yellow = Button(root, bg="YELLOW", bd=0, relief=FLAT, command=lambda: colour("YELLOW"))
yellow.grid(row=var+1, column=var+1, rowspan=var, columnspan=var, sticky=N+W+E+S)
border_vertical = Label(root, bg="BLACK")
border_vertical.grid(row=0, column=var, rowspan=2*var+1, columnspan=1, sticky=N+W+E+S)
border_horizontal = Label(root, bg="BLACK")
border_horizontal.grid(row=var, column=0, rowspan=1, columnspan=2*var+1, sticky=N+W+E+S)
# It seems that the horizontal line is starting and expanding thicker than the vertical line.
# The reason for this is unknown, as they are identical in code.
root.mainloop()
在代码中,我使用了一个变量var
,但这只是为了让我知道我希望窗口有多大。
我尝试过填充,这似乎使垂直边框更像是水平边框,但是我更喜欢水平边框更像是属性中的垂直边框,因为垂直边框的缩放比例类似于按钮,而水平边框似乎单独缩放。
感谢所有帮助!
答案 0 :(得分:2)
使用Label
作为分隔符意味着它的高度足以容纳文本(即使其中没有任何文本)。如果使用Frame
则可以达到想要的效果。
答案 1 :(得分:0)
border_vertical = Frame(root, bg="BLACK")
border_vertical.grid(row=0, column=var, rowspan=2*var+1, columnspan=1, sticky=N+W+E+S)
border_horizontal = Frame(root, bg = 'BLACK')
border_horizontal.grid(row=var, column=0, rowspan=1, columnspan=2*var+1, sticky=N+W+E+S)