import tkinter as tk
def pressed():
print("Button Pressed!")
def create_layout(frame):
"""
Add two buttons to the frame.
Both buttons should have the callback (command) pressed, and they should
have the labels "Button1" and "Button2".
Args:
frame (tk.Frame): The frame to create the two buttons in.
"""
button1 = tk.Button(frame, text="Button1", command=pressed)
button1.pack(side=tk.TOP, pady=20)
button2 = tk.Button(frame, text="Button2", command=pressed)
button2.pack(side=tk.TOP, ipadx=20)
上面有我的代码,按钮2按钮2按预期包装,但它们放在窗口的中间,我需要将它们放在左边,同时保持它们在彼此的顶部。我该怎么做?这只是我的教程中的一个练习,我没有达到,正如你们可以看到我尝试过的那样。提前谢谢。
答案 0 :(得分:2)
pack方法有几个选项,其中一个选项控制小部件在其空间中的对齐方式。
在您的情况下,您需要anchor
选项。您希望将小部件锚定到容器的西侧(左侧):
button1.pack(..., anchor="w")
button2.pack(..., anchor="w")
有关更多信息,请参阅https://docs.python.org/2/library/tkinter.html#packer-options