如何使用 tkinter 在一行中并排放置多个小部件?

新手上路,请多包涵

默认情况下,制作一个 tkinter 按钮后, 它会自动将下一个放在另一行

我该如何阻止这种情况发生?

我想做这样的事情:

在此处输入图像描述

原文由 Remigiusz Schoida 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

您必须为此使用其中一个几何管理器:

这里有 grid

 import tkinter as tk

root = tk.Tk()

b1 = tk.Button(root, text='b1')
b2 = tk.Button(root, text='b2')
b1.grid(column=0, row=0)   # grid dynamically divides the space in a grid
b2.grid(column=1, row=0)   # and arranges widgets accordingly
root.mainloop()

那里使用 pack

 import tkinter as tk

root = tk.Tk()

b1 = tk.Button(root, text='b1')
b2 = tk.Button(root, text='b2')
b1.pack(side=tk.LEFT)      # pack starts packing widgets on the left
b2.pack(side=tk.LEFT)      # and keeps packing them to the next place available on the left
root.mainloop()

剩下的几何管理器是 place ,但在调整 GUI 大小时,它的使用有时会很复杂。

原文由 Reblochon Masque 发布,翻译遵循 CC BY-SA 4.0 许可协议

只需使用它来使 y 坐标相同并更改 x 坐标:

 from tkinter import *
root = Tk()

Button(root, text='Submit', width=10, bg='blue', fg='white',
command=database).place(x=70, y=130)

对于第二个按钮:

 buttonSignIn = Button(root, text="Sign in", width=10, bg='black',
fg='white', command=new_winF).place(x=30, y=130)

原文由 bugfreerammohan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题