Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | pack_forget() and grid_forget()) method in Tkinter
Next article icon

The Pack geometry manager packs widgets relative to the earlier widget. Tkinter literally packs all the widgets one after the other in a window.  We can use options likefill,expand, andside to control this geometry manager.
Compared to thegrid manager, thepack manager is somewhat limited, but it’s much easier to use in a few, but quite common situations:


Code #1: Putting a widget inside frame and filling entire frame. We can do this with the help ofexpand andfill options.
 

Python3
# Importing tkinter modulefromtkinterimport*fromtkinter.ttkimport*# creating Tk windowmaster=Tk()# creating a Fra, e which can expand according# to the size of the windowpane=Frame(master)pane.pack(fill=BOTH,expand=True)# button widgets which can also expand and fill# in the parent widget entirely# Button 1b1=Button(pane,text="Click me !")b1.pack(fill=BOTH,expand=True)# Button 2b2=Button(pane,text="Click me too")b2.pack(fill=BOTH,expand=True)# Execute Tkintermaster.mainloop()

Output: 
 


Code #2: Placing widgets on top of each other and side by side. We can do this by side option. 
 

Python3
# Importing tkinter modulefromtkinterimport*# from tkinter.ttk import *# creating Tk windowmaster=Tk()# creating a Fra, e which can expand according# to the size of the windowpane=Frame(master)pane.pack(fill=BOTH,expand=True)# button widgets which can also expand and fill# in the parent widget entirely# Button 1b1=Button(pane,text="Click me !",background="red",fg="white")b1.pack(side=TOP,expand=True,fill=BOTH)# Button 2b2=Button(pane,text="Click me too",background="blue",fg="white")b2.pack(side=TOP,expand=True,fill=BOTH)# Button 3b3=Button(pane,text="I'm also button",background="green",fg="white")b3.pack(side=TOP,expand=True,fill=BOTH)# Execute Tkintermaster.mainloop()

Output: 
 


Code #3: 
 

Python3
# Importing tkinter modulefromtkinterimport*# from tkinter.ttk import *# creating Tk windowmaster=Tk()# creating a Fra, e which can expand according# to the size of the windowpane=Frame(master)pane.pack(fill=BOTH,expand=True)# button widgets which can also expand and fill# in the parent widget entirely# Button 1b1=Button(pane,text="Click me !",background="red",fg="white")b1.pack(side=LEFT,expand=True,fill=BOTH)# Button 2b2=Button(pane,text="Click me too",background="blue",fg="white")b2.pack(side=LEFT,expand=True,fill=BOTH)# Button 3b3=Button(pane,text="I'm also button",background="green",fg="white")b3.pack(side=LEFT,expand=True,fill=BOTH)# Execute Tkintermaster.mainloop()

Output: 
 


 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp