python中按钮位置设定_tkinter Python中的按钮位置(网格)

这里有一个快速简单的方法让你的按钮坐在你的程序底部。在

首先将enter_button和{}移到根窗口。这里不需要框架。在

然后添加root.rowconfigure(),并对按钮所在的行应用权重1。使用权重配置行可以使该行与其所在的框架或窗口展开和收缩。在本例中,我们将按钮放在root中,以便配置root。你可以对一个框架做同样的事情,但这只是添加了另一个层,不需要这样简单的事情。在

这里有一段代码你可以在你的程序中替换,结果应该是你要找的。注意,我把按钮并排放置,但是你可以用同样的方法实现从上到下的操作。在

编辑:

我忘了加columnspan部分。您还需要添加其他框架的一列,这样就可以在并排时将按钮放在居中位置。columnspan用于告诉程序小部件将占用多少列。行也可以这样做。在top_frame.grid(row = 0, column = 0, columnspan = 2)

bottom_frame.grid(row = 1, column = 0, columnspan = 2)

root.rowconfigure(2, weight = 1)

#enter_button

enter_button = Button(root, text="Enter", bg='blue', fg='white')

enter_button.config(height = 2, width = 15)

enter_button.grid(row = 2, column=0, sticky = 'se')

#quit_button

quit_button = Button(root, text="Quit", bg="blue", fg="white")

quit_button.config(height = 2, width = 15)

quit_button.grid(row = 2, column=1, sticky = 'sw')

如果您想让enter按钮位于quit按钮的顶部,那么可以执行以下操作。在

^{pr2}$

如果您只是想在输入字段和按钮之间留出一点空间,您可以使用间隔标签。像这样:#empty row spacers

spacer1 = Label(bottom_frame, text = "")

spacer1.grid(row = 3)

spacer2= Label(bottom_frame, text = "")

spacer2.grid(row = 4)

#enter_button

enter_button = Button(bottom_frame, text="Enter", bg='blue', fg='white')

enter_button.config(height = 2, width = 15)

enter_button.grid(row = 5, column=1)

#quit_button

quit_button = Button(bottom_frame, text="Quit", bg="blue", fg="white")

quit_button.config(height = 2, width = 15)

quit_button.grid(row = 6, column=1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值