如何与tkinter并排放置多个小部件?

时间:2018-08-01 10:31:30

标签: python python-3.x tkinter

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

2 个答案:

答案 0 :(得分:5)

您必须为此使用几何管理器之一:

此处带有grid

将tkinter导入为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大小时,其使用有时会很复杂。

答案 1 :(得分:2)

简单地用于放置y坐标,并更改x坐标

b in a 

from tkinter import * root = Tk() 对于第二个按钮

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