gpt4 book ai didi

python-3.x - python3.4 tkinter 如何将按钮放在左侧和顶部

转载 作者:行者123 更新时间:2023-12-02 01:33:30 25 4
gpt4 key购买 nike

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)

上面有我的代码,它按预期将 button1 打包在 button2 的顶部,但它们被放置在窗口的中间,我需要将它们放置在左侧,同时将它们保持在彼此的顶部。我该怎么做?这只是我的教程中的一个练习,我没有做过,你们可以看到我已经尝试过。提前致谢。

最佳答案

pack 方法有几个选项,其中之一控制小部件在其空间中的对齐方式。

在您的情况下,您需要 anchor选项。您想将您的小部件 anchor 定到容器的西(左)侧:

button1.pack(..., anchor="w")
button2.pack(..., anchor="w")

有关更多信息,请参阅 https://docs.python.org/2/library/tkinter.html#packer-options

关于python-3.x - python3.4 tkinter 如何将按钮放在左侧和顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com