I have 5 buttons with 3 of them in the first row and 2 in the second row. How do I fill the white space that is left? I tried the following:
JavaScript
x
3
1
Date_play.grid(row=3,column=1,columnspan=2,sticky="w")
2
File_play.grid(row=3,column=2,columnspan=2,sticky="e")
3
Thanks in advance.
Advertisement
Answer
There are multiple ways to do this. One of the easier ones is to play around with the Grid Layout.
Try this:
JavaScript
1
24
24
1
import tkinter as tk
2
3
4
root = tk.Tk()
5
# create a grid of 2x6
6
root.rowconfigure(0, weight=1)
7
root.rowconfigure(1, weight=1)
8
for i in range(6):
9
root.columnconfigure(i, weight=1)
10
11
# by playing around with columnspan, you can get the layout that you need
12
button1 = tk.Button(root, text='1')
13
button1.grid(row=0, column=0, columnspan=2, sticky=tk.NSEW)
14
button2 = tk.Button(root, text='2')
15
button2.grid(row=0, column=2, columnspan=2, sticky=tk.NSEW)
16
button3 = tk.Button(root, text='3')
17
button3.grid(row=0, column=4, columnspan=2, sticky=tk.NSEW)
18
button4 = tk.Button(root, text='4')
19
button4.grid(row=1, column=0, columnspan=3, sticky=tk.NSEW)
20
button5 = tk.Button(root, text='5')
21
button5.grid(row=1, column=3, columnspan=3, sticky=tk.NSEW)
22
23
root.mainloop()
24