How do I make text(one letter) fill out a tkinter button?
<p>I have a tic tac toe game I'm trying to put the finishing touches on. Each tile in the game is a button with the letter 'x' or 'o'. But I can't seem to get the letters to fill out the frame. When I change the font size, the buttons bloat out. If I try to set the tile height/weight after setting the font size it doesn't seem to have any effect. </p>

<p>Is there a way to simply make an 'x' expand out to fill the button it's in?</p>

<pre><code>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)
</code></pre>

<p>I would say there is no way to make characters automatically fill the entire button's space.  Consider <code>Canvas</code> objects instead of <code>Button</code>s.  There you can easily draw an X (two lines) and an O (circle) with full control over the size and the space between the edge of the canvas and the sign.  Maybe it makes even sense to make a field in the UI an own class.</p>

<pre><code>from functools import partial
from itertools import cycle
import tkinter as tk


class FieldUI(tk.Canvas):

    def __init__(self, parent, size, variable):
        tk.Canvas.__init__(self, parent, width=size, height=size)
        self.size = size
        self.variable = variable
        self.variable.trace('w', self.update_display)
        self.update_display()

    def update_display(self, *_args):
        self.delete(tk.ALL)
        value = self.variable.get()
        if value == 'x':
            self.create_line(5, 5, self.size - 5, self.size - 5, width=4)
            self.create_line(self.size - 5, 5, 5, self.size - 5, width=4)
        elif value == 'o':
            self.create_oval(5, 5, self.size - 5, self.size - 5, width=4)
        elif value != '':
            raise ValueError("only 'x', 'o', and '' allowed")


class GameUI(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent, background='black')
        self.symbols = cycle('xo')
        self.field_vars = [tk.StringVar() for _ in range(9)]
        for i, field_var in enumerate(self.field_vars):
            field_ui = FieldUI(self, 50, field_var)
            field_ui.bind('&lt;Button-1&gt;', partial(self.on_click, i))
            column, row = divmod(i, 3)
            field_ui.grid(row=row, column=column, padx=1, pady=1)

    def on_click(self, field_index, _event=None):
        field_var = self.field_vars[field_index]
        if not field_var.get():
            field_var.set(next(self.symbols))


def main():
    root = tk.Tk()
    game_ui = GameUI(root)
    game_ui.pack()
    root.mainloop()


if __name__ == '__main__':
    main()
</code></pre>

python button tkinter