Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tkinter | grid_location() and grid_size() method
Next article icon

TheGrid geometry manager puts the widgets in a 2-dimensional table. The master widget is split into a number of rows and columns, and each “cell” in the resulting table can hold a widget. Thegrid manager is the most flexible of the geometry managers inTkinter. If you don’t want to learn how and when to use all three managers, you should at least make sure to learn this one. Consider the following example - 

Creating this layout using thepack manager is possible, but it takes a number of extra frame widgets, and a lot of work to make things look good. If you use the grid manager instead, you only need one call per widget to get everything laid out properly. Using thegrid manager is easy. Just create the widgets, and use thegrid method to tell the manager in which row and column to place them. You don’t have to specify the size of the grid beforehand; the manager automatically determines that from the widgets in it. 

Code #1: 

Python
# import tkinter modulefromtkinterimport*fromtkinter.ttkimport*# creating main tkinter window/toplevelmaster=Tk()# this will create a label widgetl1=Label(master,text="First:")l2=Label(master,text="Second:")# grid method to arrange labels in respective# rows and columns as specifiedl1.grid(row=0,column=0,sticky=W,pady=2)l2.grid(row=1,column=0,sticky=W,pady=2)# entry widgets, used to take entry from usere1=Entry(master)e2=Entry(master)# this will arrange entry widgetse1.grid(row=0,column=1,pady=2)e2.grid(row=1,column=1,pady=2)# infinite loop which can be terminated by keyboard# or mouse interruptmainloop()

Output:

    

Code #2: Creating the layout which is shown above. 

Python
# import tkinter modulefromtkinterimport*fromtkinter.ttkimport*# creating main tkinter window/toplevelmaster=Tk()# this will create a label widgetl1=Label(master,text="Height")l2=Label(master,text="Width")# grid method to arrange labels in respective# rows and columns as specifiedl1.grid(row=0,column=0,sticky=W,pady=2)l2.grid(row=1,column=0,sticky=W,pady=2)# entry widgets, used to take entry from usere1=Entry(master)e2=Entry(master)# this will arrange entry widgetse1.grid(row=0,column=1,pady=2)e2.grid(row=1,column=1,pady=2)# checkbutton widgetc1=Checkbutton(master,text="Preserve")c1.grid(row=2,column=0,sticky=W,columnspan=2)# adding image (remember image should be PNG and not JPG)img=PhotoImage(file=r"C:\Users\Admin\Pictures\capture1.png")img1=img.subsample(2,2)# setting image with the help of labelLabel(master,image=img1).grid(row=0,column=2,columnspan=2,rowspan=2,padx=5,pady=5)# button widgetb1=Button(master,text="Zoomin")b2=Button(master,text="Zoomout")# arranging button widgetsb1.grid(row=2,column=2,sticky=E)b2.grid(row=2,column=3,sticky=E)# infinite loop which can be terminated# by keyboard or mouse interruptmainloop()

Output:

  

Warning: Never mixgrid() andpack() in the same master window.


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