tkinter fill = Y不适用于Button

时间:2017-08-07 09:46:00

标签: python user-interface tkinter

在macOS Sierra上,使用py2.7.13和py3.6,

我希望以下代码看起来像: enter image description here

import Tkinter as tk
root = tk.Tk()
tk.Button(root, text="A").pack(side=tk.LEFT, expand=tk.YES, fill=tk.Y)
tk.Button(root, text="B").pack(side=tk.TOP, expand=tk.YES, fill=tk.BOTH)
tk.Button(root, text="C").pack(side=tk.RIGHT, expand=tk.YES, fill=tk.NONE, anchor = tk.NE)
tk.Button(root, text="D").pack(side=tk.LEFT, expand=tk.NO, fill=tk.Y)
tk.Button(root, text="E").pack(side=tk.TOP, expand=tk.NO, fill=tk.BOTH)
tk.Button(root, text="F").pack(side=tk.RIGHT, expand=tk.NO, fill=tk.NONE)
tk.Button(root, text="G").pack(side=tk.BOTTOM, expand=tk.YES, fill=tk.Y)
tk.Button(root, text="H").pack(side=tk.TOP, expand=tk.NO, fill=tk.BOTH)
tk.Button(root, text="I").pack(side=tk.RIGHT, expand=tk.NO)
tk.Button(root, text="J").pack(anchor=tk.SE)
root.mainloop()
但是得到了 enter image description here

因此那些填充= Y的按钮不会扩展并垂直填充。

但是,标签的行为符合预期: enter image description here

代码:

import Tkinter as tk
root = tk.Tk()
tk.Label(root, text="A", bg='green').pack(side=tk.LEFT, expand=tk.YES, fill=tk.Y)
tk.Label(root, text="B", bg='red').pack(side=tk.TOP, expand=tk.YES, fill=tk.BOTH)
tk.Label(root, text="C", bg='blue').pack(side=tk.RIGHT, expand=tk.YES, fill=tk.NONE, anchor=tk.NE)
tk.Label(root, text="D", bg='yellow').pack(side=tk.LEFT, expand=tk.NO, fill=tk.Y)
tk.Label(root, text="E", bg='purple').pack(side=tk.TOP, expand=tk.NO, fill=tk.BOTH)
tk.Label(root, text="F", bg='pink').pack(side=tk.TOP, expand=tk.NO, fill=tk.NONE)
tk.Label(root, text="G", bg='green').pack(side=tk.BOTTOM, expand=tk.YES, fill=tk.Y)
tk.Label(root, text="H", bg='red').pack(side=tk.TOP, expand=tk.NO, fill=tk.BOTH)
tk.Label(root, text="I", bg='blue').pack(side=tk.RIGHT, expand=tk.NO)
tk.Label(root, text="J", bg='yellow').pack(anchor=tk.SE)
root.mainloop()

我在这里缺少什么?按设计,按钮的行为与标签不同吗?

1 个答案:

答案 0 :(得分:0)

我看到了这个问题: Why won't my Button widget expand vertically

接受的答案似乎有道理。太糟糕了,我现在仍然坚持使用macOS。

我的重复。