Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:48:15+00:00 2026-06-11T15:48:15+00:00

I have a window whose content changes. Sometimes the content is larger than the

  • 0

I have a window whose content changes. Sometimes the content is larger than the window, so the window expands to fit it’s children. However, when I center a window using a call to “geometry”, the window no longer resizes. Below, you will find code that illustrates this.

If you comment out the delayed center() function call, you’ll notice that the window expands to fit its content. If you leave it as is, the window centers, but no longer expands to fit its content.

Is it possible to center a window AND have it continue to resize to fit its content?

from Tkinter import *
import ttk

def center(root):
    w = root.winfo_screenwidth()
    h = root.winfo_screenheight()
    rootsize = tuple(int(_) for _ in root.geometry().split('+')[0].split('x'))
    x = w/2 - rootsize[0]/2
    y = h/2 - rootsize[1]/2
    root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))

root = Tk()
var = StringVar()
var.set('Small Text')

label = ttk.Label(root, textvariable=var)
label.grid(column=0, row=0)

# Change the text label in a couple of seconds.
def changeit():
    var.set('BIG TXT - ' * 5)
root.after(2000, changeit)

# Comment out this center call and the label expands.
root.after(100, lambda: center(root))
root.mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:48:16+00:00Added an answer on June 11, 2026 at 3:48 pm

    When you call the geometry command, don’t provide a width and height — just supply the x/y values. When you give it an explicit width and height, you’re telling Tk “I want the window to be exactly this size” so it turns it’s auto-resize behavior off.

    root.geometry("+%d+%d" % (rootsize + (x, y)))
    

    Also, you can use winfo_width() and winfo_height() to get the actual size of the window, instead of parsing the output of the geometry method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Cocoa window, whose content view contains an NSScrollView that, in turns,
I have created a window whose handle is handle_parent. Then I created a child
I have a window I've created using UiBinder. I'm followings Sencha's HelloWorldUiBinder example and
I have a web app using master page and content pages (see the attached
I'm using Ajax to dynamically populate a DIV. I have each page content stored
I have a telerik window with multiple divs inside it whose visibility is set
I have a window whose size I need to change when the user clicks
I want to create an application whose content of the window will change based
I have a positioned div whose content can be too long so scrollbars appear
I have a page with iframe. Content inside iframe does window.parent.resizeTo calls (or something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.