【发布时间】:2016-09-22 13:32:41
【问题描述】:
我正在尝试完成一个井字游戏。游戏中的每个图块都是一个带有字母“x”或“o”的按钮。但我似乎无法让字母填满框架。当我更改字体大小时,按钮会膨胀。如果我在设置字体大小后尝试设置图块高度/重量,它似乎没有任何效果。
有没有办法简单地将“x”展开以填充它所在的按钮?
class TicTacToeBoard(Frame):
def __init__(self, parent=None, game=None):
Frame.__init__(self, parent)
# We are controlling the gui through an external game class.
self.game = game
self.symbol = game.sym()
mainframe = ttk.Frame(root, padding=(12, 12, 12, 12))
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
# This is how we change the tile a player plays on.
self.strVars = [StringVar() for x in range(9)]
# Setup the button tiles
self.buttons = []
for i, s in enumerate(self.strVars):
func = (lambda i=i: self.btn_press(i))
b = Button(mainframe, textvariable=s, font='size, 35', command=func)
b.config(height=5, width=10)
self.buttons.append(b)
# Add each tile to the grid
for x in range(3):
for y in range(3):
self.buttons.pop(0).grid(row=x, column=y)
【问题讨论】:
-
一些备注:
root在哪里定义?我想应该是self。self.buttons到底是空的,为什么要绑定到对象上呢?对我来说,这似乎是一个简单的局部变量。strVars不是个好名字。名称应该告诉读者值的含义,而不是它们的类型。 -
root 是在一个单独的运行函数中定义的,因为我之前有一个对话框提示他们要使用什么字符。