Python tkinter pack()

Python tkinter pack()

Widget-ul pack() este utilizat pentru a organiza widgetul în bloc.  Widget-urile de poziție adăugate la aplicația python folosind metoda pack() pot fi controlate prin utilizarea diferitelor opțiuni specificate în apelul de metodă.

Cu toate acestea, comenzile sunt mai puține, iar widget-urile sunt, în general, adăugate într-un mod mai puțin organizat.

Sintaxa de utilizare a pack() este dată mai jos:

        widget.pack(options)

  • expand 
    Dacă extinderea este setată la adevărat, widgetul se extinde pentru a umple orice spațiu.
  • fill   În mod implicit, umplerea este setată la NONE. Cu toate acestea, îl putem seta la X sau Y pentru a determina dacă widget-ul conține spațiu suplimentar. NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically).
  • side  Reprezintă partea părintelui pe care urmează să fie plasat widgetul pe fereastră. TOP(default), BOTTOM, LEFT sau RIGHT. 
  •  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # FILE: root_window.py
    # RUN : python3 root_window.py
    # -------------------------------------------BEGIN file
    import tkinter as tk
    from tkinter import ttk
    from tkinter.messagebox import showinfo
    
    class App(tk.Tk):
        def __init__(self):
            super().__init__()
            # configurarea ferestrei principale
            self.title("Fereastra oop")
            self.geometry('300x100')
            # adaugarea widget-ului label
            self.label = ttk.Label(self, text="Salut, Tkinter!")
            self.label.pack()
            # adaugarea unui buton
            self.button = tk.Button(self, text="Apasa",fg="red")
            self.button['command'] = self.button_clicked
            self.button.pack()
    
            self.buttonDoi = tk.Button(self, text="DOI", fg="green")
            self.buttonDoi.pack(side=tk.LEFT)
    
            self.buttonTrei = tk.Button(self, text="Trei", fg="blue")
            self.buttonTrei.pack(side=tk.LEFT)
    
            self.buttonPatru = tk.Button(self, text="Patru", fg="yellow")
            self.buttonPatru.pack(side=tk.LEFT)
            
        def button_clicked(self):
            showinfo(title="Informare", message="Salut!")
            
    if __name__ == "__main__":
        app = App()
        app.mainloop()
    # -------------------------------------------END file
    

 

Blog:

Python Tkinter.ttk Frame 




Comentarii