如何让按钮到左侧?

时间:2018-05-10 09:46:01

标签: python tkinter

我正在尝试使用tkinter创建按钮。

这是我的代码

import tkinter as tk

def pressed():
    print("button pressed!")

def create_layout(frame):

    frame.pack(fill=None,expand=True)
    mycolor = '#%02x%02x%02x' % (250, 250, 210)
    root.configure(bg=mycolor)
    bottomframe = tk.Frame(root)
    bottomframe.pack( side = tk.LEFT )

    button1 = tk.Button(frame, text="Button1", fg="black",command=pressed,anchor="w")
    button1.pack( side = tk.LEFT)

    button2 =tk.Button(frame, text="Button2", fg="black",command=pressed,anchor="w")
    button2.pack( side = tk.LEFT )



root = tk.Tk()
root.geometry("250x150")
frame = tk.Frame(root)
create_layout(frame)
root.mainloop()

我已经指定了锚=" w"和side =" LEFT",但它似乎没有用。

代码输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

frame.pack(fill=None,expand=True)更改为frame.pack(fill=None,expand=True, anchor="w")会将按钮对齐到左侧。