2 回答
TA贡献1816条经验 获得超4个赞
要使按钮为正方形,您只需设置 GridLayout 单元格的高度和宽度,并且您正在尝试使用 size_hint 来实现。尝试这个:
from kivy.core.window import Window
class ChessBoard(GridLayout):
def __init__(self, **kwargs):
super(ChessBoard, self).__init__(**kwargs)
self.cols = 8
winsize = Window.size
sizedict = {}
# to set width and height of GridLayout cells, you should make a dict, where the key is col's/row's number and the value is size
for i in range(self.cols):
sizedict[i] = winsize[0]/8 #or you can divide it by 10 for example to have some black filling on the sides
# and then simply do this
self.cols_minimum = sizedict
self.rows_minimum = sizedict
TA贡献1789条经验 获得超8个赞
这段代码生成的按钮对我来说看起来相当方正。如果您打算为您的棋子使用图像,则按钮将符合这些图像的大小。
from tkinter import Tk, Button
window = Tk ()
squares = []
index = 0
for x in range (8) :
for y in range (8) :
squares.append (Button (window, width = 7, height = 4))
squares [index].grid (row = x, column = y)
index += 1
window.mainloop ()
添加回答
举报
