Python 在tkinter中添加文本时防止调整按钮大小

Python 在tkinter中添加文本时防止调整按钮大小,python,tkinter,Python,Tkinter,我在tkinter做了一个井字游戏 代码如下: from tkinter import * index = 0 def play_again(): print("TO DO IMPLEMENT IT!") def update(button_tekst): global index if index % 2 == 0 and button_tekst.get() == "": button_tekst.set("X") index

我在tkinter做了一个井字游戏

代码如下:

from tkinter import *

index = 0

def play_again():
    print("TO DO IMPLEMENT IT!")


def update(button_tekst):
    global index
    if index % 2 == 0 and button_tekst.get() == "":
        button_tekst.set("X")
        index += 1
    if index % 2 and button_tekst.get() == "":
        button_tekst.set("O")
        index += 1




def main():

    window = Tk()

    window.title("Tic tac toe")
    window.geometry("400x400")

    window.grid_columnconfigure((0,1,2), weight=1)
    window.grid_rowconfigure((1, 2, 3), weight=2)
    window.grid_rowconfigure(0, weight=1)

    again = Button(window, text="Play again", bg="lightskyblue", fg='white', font=30, command=play_again)
    again.grid(row=0, columnspan=3, sticky="ewns")

    coordinaten = [[1, 0], [1, 1], [1, 2], [2, 0],
                   [2, 1], [2, 2], [3, 0], [3, 1], [3, 2]]

    texten = [StringVar(), StringVar(), StringVar(), StringVar(), StringVar(), StringVar(), StringVar(), StringVar(), StringVar()]

    for i in range(len(coordinaten)):
        button = Button(window, textvariable=texten[i], font=("Helvetica", "30"),  command=lambda current_index = i:update(texten[current_index]))
        button.grid(row=coordinaten[i][0], column=coordinaten[i][1], sticky="ewns")

    window.mainloop()


main()

但是,当我单击按钮时,会显示文本,但按钮会自动调整大小。 我怎样才能防止这种情况?
此外,是否需要使用全局索引(或布尔值)变量和7个Stringvar变量?

对于比例字体,字符串的总宽度取决于其内容。当geometry manager根据每个小部件内容的当前大小调整其大小时,您可能会观察到您所谈论的烦恼类型。幸运的是,解决方案很简单:只需为按钮小部件指定一个恒定的“宽度”属性:

    button = Button(window, width=3, textvariable=texten[i], ...)
编辑:关于您其他问题:

  • 需要全局索引变量:是
  • 需要9个Stringvar的列表:否
您可以直接操纵按钮小部件的“文本”属性。此外,通过将9个按钮存储到按单元坐标(i,j)索引的字典中, 更新功能变得更加简单:

from tkinter import *

def play_again():
    print("TO DO IMPLEMENT IT!")

def update(grid, i, j):
    global index
    grid[i,j]['text'] = 'XO'[index] # select either 'X' or 'O'
    index = 1 - index

def main():
    global index
    font = ("Helvetica", "30")
    index = 0

    window = Tk()
    window.title("Tic tac toe")
    window.geometry("400x400")

    window.grid_columnconfigure((0,1,2), weight=1)
    window.grid_rowconfigure((1, 2, 3), weight=2)
    window.grid_rowconfigure(0, weight=1)

    again = Button(window, text="Play again", bg="lightskyblue", fg='white',
                   font=font, command=play_again)
    again.grid(row=0, columnspan=3, sticky="ewns")

    grid = {} # store Button widgets into a dictionary indexed by cell coords
    for i in range(3):
      for j in range(3):
        grid[i,j] = Button(window, width=3, text='', font=font,
                           command=lambda i=i,j=j: update(grid, i, j))
        grid[i,j].grid(row=i+1, column=j, sticky="ewns")

    window.mainloop()

main()
阅读