【问题标题】:Prevent resize buttons when adding text in tkinter在 tkinter 中添加文本时防止调整按钮大小
【发布时间】:2019-12-17 17:24:51
【问题描述】:

我在 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 变量?

【问题讨论】:

标签: python tkinter


【解决方案1】:

对于比例字体,字符串的总宽度取决于其内容。当几何管理器根据其内容的当前大小调整每个小部件的大小时,您可能会观察到您所说的那种烦恼。幸运的是,解决方案很简单:只需为您的 Button 小部件指定一个恒定的“宽度”属性:

    button = Button(window, width=3, textvariable=texten[i], ...)

编辑:关于你的其他问题:

  • 需要一个全局索引变量:是
  • 需要 9 个 Stringvar 的列表:否

您可以直接操作 Button 小部件的“文本”属性。此外,通过将 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多