使tkinter按钮大小相同

时间:2017-12-17 22:36:30

标签: python-3.x button tkinter

我想让所有tkinter按钮的大小都相同,无论文本如何。是否可以拉伸其他按钮以相互匹配或设置特定大小?因为我很难在文档中找到如何做到这一点。目前,按钮根据文本的大小进行拉伸。 Example of what I mean。是否有可能使它们大小相同?

2 个答案:

答案 0 :(得分:4)

使用几何管理器(packplacegrid)时,通常会执行此操作。

使用网格:

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()