我想让所有tkinter按钮的大小都相同,无论文本如何。是否可以拉伸其他按钮以相互匹配或设置特定大小?因为我很难在文档中找到如何做到这一点。目前,按钮根据文本的大小进行拉伸。 Example of what I mean。是否有可能使它们大小相同?
答案 0 :(得分:4)
使用几何管理器(pack
,place
或grid
)时,通常会执行此操作。
使用网格:
import tkinter as tk
root = tk.Tk()
for row, text in enumerate((
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger")):
button = tk.Button(root, text=text)
button.grid(row=row, column=0, sticky="ew")
root.mainloop()
使用pack:
import tkinter as tk
root = tk.Tk()
for text in (
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger"):
button = tk.Button(root, text=text)
button.pack(side="top", fill="x")
root.mainloop()
答案 1 :(得分:1)
您还可以在定义按钮时使用width
选项,
from tkinter import *
root = Tk()
button = Button(root, text = "Test", width = 5)
button.grid()
root.mainloop()