如何在ttk中的框架上居中按钮?

时间:2017-02-15 13:04:57

标签: python tkinter ttk

我无法使用ttk获取以框架为中心的按钮: enter image description here

以下是上述示例的代码:

from tkinter import *
from tkinter.ttk import *

root = Tk()
root.title("button on a frame")

l = Label(root, text="why wont my button")
l2 = Label(root, text="on a frame center?")
l.grid(row=0, column=0)
l2.grid(row=1, column=1)

myStyle = Style()
myStyle.configure("green.TFrame", background="green")

bottomFrame = Frame(root, style="green.TFrame")
bottomFrame.grid(row=11, columnspan=2, sticky=E+W, pady=5)

runButton = Button(bottomFrame, text="Run")
runButton.grid(pady=5)

root.mainloop()

如果我添加一个没有框架的按钮,它就会居中。

1 个答案:

答案 0 :(得分:1)

您需要为weight的行和列设置Frame

bottomFrame = Frame(root, style="green.TFrame")
bottomFrame.grid(row=11, columnspan=2, sticky=E+W+N+S, pady=5)
bottomFrame.grid_columnconfigure(0, weight=1)
bottomFrame.grid_rowconfigure(0, weight=1)

runButton = Button(bottomFrame, text="Run")
runButton.grid(pady=5)

weight (column)

  

用于在列之间分配额外空间的相对权重。   重量为2的色谱柱的增长速度是色谱柱的两倍   权重1.默认值为0,表示该列不会增长   一点都不。

weight (row)

  

用于在行之间分配额外空间的相对权重。一个   具有权重2的行将比具有权重1的行快两倍。   默认值为0,表示该行根本不会增长。

相关问题