Python - .pack() .place() .grid() mit Beispielen

Опубликовано: 22 Март 2026
на канале: ANDREEs
194
1

Hier eine kurze Erklärung mit Beispielen zu den 3 Platzierungs-Methoden pack, place und grid...
Hier der Code und die Dokumentation:

import tkinter as tk
root = tk.Tk()
root.geometry("800x600")
root.configure(bg='#48483D')
text_1 = tk.Label(root, text= "Willkommen", foreground="blue", background="white").grid(row=0, column=0)
button_1 = tk.Button(root, text= "login").grid(row=1, column=1, padx=20)
root.mainloop()

"""
///// Platzieren mit pack() /////

Beispiel: button_1.pack(side = "left")
button_2.pack(side = "bottom", pady=3)
button_3.pack(side = "top", expand = True, fill = "both"")

expand − When set to true, widget expands to fill any space not otherwise used in widget's parent.

fill − Determines whether widget fills any extra space allocated to it by the packer, or keeps its own minimal dimensions: none (default), x (fill only horizontally), y (fill only vertically), or both (fill both horizontally and vertically).

side − Determines which side of the parent widget packs against: top (default), bottom, left, or right

##############################################################

///// Platzieren mit place() /////

Beispiele: button_1.place(bordermode="outside", height=100, width=100)
button_2.place(relx = 0.5, rely = 0.3))
button_3.place(relx = 0.5, rely = 0.5, anchor = "center"")

anchor − The exact spot of widget other options refer to: may be n, e, s, w, ne, nw, se, or sw, compass directions indicating the corners and sides of widget; default is nw (the upper left corner of widget)

bordermode − INSIDE (the default) to indicate that other options refer to the parent's inside (ignoring the parent's border); outside otherwise.

height, width − Height and width in pixels.

relheight, relwidth − Height and width as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.

relx, rely − Horizontal and vertical offset as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.

x, y − Horizontal and vertical offset in pixels.



##############################################################

///// Platzieren mit grid() /////

Beispiele: button_1.grid(row = 0, column = 0, sticky = W, pady = 2)
button_2.grid(row = 1, column = 0, sticky = W, pady = 2)

column = number - use cell identified with given column (starting with 0)

columnspan = number - this widget will span several columns

in = master - use master to contain this widget

in_ = master - see 'in' option description

ipadx = amount - add internal padding in x direction

ipady = amount - add internal padding in y direction

padx = amount - add padding in x direction

pady = amount - add padding in y direction

row = number - use cell identified with given row (starting with 0)

rowspan = number - this widget will span several rows

sticky = NSEW - if cell is larger on which sides will this widget stick to the cell boundary
"""