tkinter.Label.pack


Definition
The method create the Label.

Syntax
Label.pack()

Options
  • Label.pack(
  • after=widget - pack it after you have packed widget
  • anchor=NSEW (or subset) - position widget according to given direction
  • before=widget - pack it before you will pack widget
  • expand=bool - expand widget if parent size grows
  • fill=NONE or X or Y or BOTH - fill widget if widget grows
  • 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
  • side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
  • )
Examples
The frame "<ButtonRelease>" trigger the function(buttonrelease) which displays the Label(welcome).
from tkinter import *

master = Tk()

def buttonrelease(click):
    label=Label(frame, text="Welcome", fg="Blue")
    label.pack(side=TOP)

frame = Frame(master, background="blue", width=200, height=200)
frame.bind("<ButtonRelease>", buttonrelease)
frame.pack()

master.mainloop()









The button command function is used to invoke the message box to display the random value.
from tkinter import*

master = Tk()
master.title("Random Widget")

def random_value():
    import random
    random1=random.random()
    messagebox.showinfo("Random Value is", random1)

labelframe=LabelFrame(master, fg="red", font=14, text="Random value generator")
labelframe.pack(side=BOTTOM)

button = Button(labelframe, text = "Click here", fg="blue", command = random_value)
button.pack(side=BOTTOM)

label = Label(labelframe, text = "Click on button to generate random value")
label.pack(side=BOTTOM)

master.mainloop()






Comments