Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Setting the position of TKinter labels
Next article icon

Tkinter is a built-in Python module used for building desktop GUI applications. It's simple, powerful, and doesn’t require any external installation. Tkinter provides many methods, one of them is thegeometry() method and it is used to control the size and position of theGUIwindow.

The geometry() method sets:

Examples of Geometry Method in Python Tkinter

Example 1: Set Window Size with geometry()

Usegeometry() to fix the window’s width and height.

Python
fromtkinterimportTkfromtkinter.ttkimportButtonw=Tk()w.geometry('200x150')btn=Button(w,text='Geeks')btn.pack(pady=5)w.mainloop()

Output: 

After running the application, you'll see that the size of the Tkinter window has changed, but the position on the screen is the same. 

geometry method to set window dimensions

Explanation:

  • 200x150sets the window width to 200px and height to 150px.
  • The position stays default (top-left corner).
  • Window now has a fixed size regardless of widgets inside.

Example 2: Set Size and Position Together

We can also set both size and on-screen position using offsets.

Python
fromtkinterimportTkfromtkinter.ttkimportButtonw=Tk()w.geometry('200x150+400+300')btn=Button(w,text='Geeks')btn.pack(pady=5)w.mainloop()

Output: 

When you run the application, you'll observe that the position and size both are changed. Now the Tkinter window is appearing at a different position (400 shifted on X-axis and 300 shifted on Y-axis).

geometry method to set the dimensions and position of the Tkinter window

Explanation:

  • 200x150: window size (width × height)
  • +400+300: 400px from left, 300px from top of the screen
  • The window now opens at a different position with a fixed size

Tkinter Window Without Using geometry() 

By default, the window appears in the top-left (northwest) corner with an automatic size.

Python
fromtkinterimportTkfromtkinter.ttkimportButtonw=Tk()btn=Button(w,text='Geeks')btn.pack(pady=5)w.mainloop()

Output: 

As soon as we run the application, we'll see the position of the Tkinter window is at the northwest position of the screen and the size of the window is also small as shown in the output below. 

Tkinter window without geometry method

Explanation:

  • No size or position is set manually.
  • The window appears in the default top-left screen corner.
  • Window size adjusts automatically based on widget size.

Note: We can also pass a variable argument in the geometry method, but it should be in the form(variable1) x (variable2); otherwise, it will raise an error.

Related articles:


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