Luca
Luca

Reputation: 170

Align different labels in a Tkinter frame

I have x labels, these are created using

rand = random.randint(1,100000)
local()[f'self.m{rand}'] = Label(self.frame, text='test')
local()[f'self.m{rand}'].pack()

Furthermore I have a frame, I want to set a label next to another one following this scheme

Label 1 Label 2

Label 3 Label 4

Label 5 Label 6

Label **y** Label **x**

Does anyone have any ideas on how I could proceed?

Upvotes: 1

Views: 724

Answers (1)

furas
furas

Reputation: 142651

Use grid() instead of pack() to create columns and rows.

If you use pack() then create Frame using pack() and then inside Frame you can use grid().

import tkinter as tk

root = tk.Tk()

frame = tk.Frame(root)
frame.pack()

labels = {}

for row_number in range(5):
    for col_number in range(2):
        value = 1 + row_number*2 + col_number
        labels[value] = tk.Label(frame, text='Label ' + str(value))
        labels[value].grid(row=row_number, column=col_number)

root.mainloop() 

enter image description here

Upvotes: 2

Related Questions