首页 > 解决方案 > 使用 Sticky 时无法调整 Tkinter 按钮的大小

问题描述

在此处输入图像描述

我正在尝试调整按钮,但不知何故,即使在改变width之后,它也不会改变大小。这是我的代码:

if __name__ == '__main__':
    # create application window
    app = Tk()

    # title
    app.title("Music Players")

    # geometry
    app.geometry('500x300')

    # background color
    app.configure(bg='orange')

    equation = StringVar()
    window_1 = Label(app, textvariable=equation)
    window_1.grid(columnspan=3, ipadx=100, ipady=10)
    equation.set('music player')

    window_2 = Entry(app, width=30)
    window_2.grid(columnspan=5, ipadx=100, ipady=10)

    # Create buttons and other accessories
    button1 = Button(app, text='PLAY', fg='yellow', bg='purple',
                     command=lambda: press('PLAY'), height=2, width=1)
    button1.grid(row=2, column=0, sticky="NSEW")

    button2 = Button(app, text='STOP', fg='yellow', bg='purple',
                     command=lambda: press('STOP'), height=2, width=2)
    button2.grid(row=2, column=1, sticky="NSEW")

    button3 = Button(app, text='NEXT', fg='yellow', bg='purple',
                     command=lambda: press('NEXT'), height=2, width=2)
    button3.grid(row=2, column=2, sticky="NSEW")

    button4 = Button(app, text='PREVIOUS', fg='yellow', bg='purple',
                     command=lambda: press('PREVIOUS'), height=2, width=2)
    button4.grid(row=2, column=3, sticky="NSEW")

我假设这是因为sticky="NSEW",因为当我删除它时,按钮会改变大小,但是按钮之间有很大的空间,而且即使columns=0columns=1也很间隔. 在此处输入图像描述

我怎样才能改变按钮的大小而不摆脱粘滞?或者,没有粘性,但按钮彼此相邻?

我的预期输出将是这样的: 在此处输入图像描述

先感谢您!

标签: pythontkinter

解决方案


推荐阅读