gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:32:58 24 4
gpt4 key购买 nike

默认情况下,在创建一个 tkinter 按钮后,它会自动将下一个放在另一行
我该如何阻止这种情况发生?
我想做这样的事情:
enter image description here

最佳答案

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

这里有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 大小时,它的使用有时会很复杂。

关于python - 如何使用 tkinter 在一行中并排放置多个小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631105/

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