Screen placement based on screen resolution

Discussion in 'Python' started by Pat, Apr 7, 2006.

  1. Pat

    Pat Guest

    I am trying to place a dialog in the center of the screen based on a users
    screen resolution.
    I can get the width and height of the screen, but I can't seem to use the


    It appears the values for X and Y need to be integers and not a variable
    like width/2-40
    Pat, Apr 7, 2006
    1. Advertisements

  2. Tkinter takes strings as its arguments; it's TCL's legacy. You can use
    string formatting for this:

    x = width/2-40
    y = height/2-30

    root.geometry('%ldx%ld+%ld+%ld' % (width, height, x, y))
    Lonnie Princehouse, Apr 7, 2006
    1. Advertisements

  3. Pat

    Pat Guest

    Pat, Apr 7, 2006
  4. Python doesn't look in string literals for things that might look
    like expressions, but if you have the values, *creating* a string
    with the right contents is pretty easy. see the tutorial for the

    if you have all the values in variables, this expression sets the
    geometry in one step:

    root.geometry("%dx%d%+d%+d" % (width, height, xoffset, yoffset))

    also see

    which includes code that parses a geometry string.

    Fredrik Lundh, Apr 8, 2006
  5. geometry strings are an X windows thing...
    note that "+%ld" (why bother with the l, btw? Python's not C) will
    misbehave if you pass in a negative offset. better use "%+d".

    Fredrik Lundh, Apr 8, 2006
  6. Pat

    Pat Guest

    Thanks a lot for you response.
    Pat, Apr 8, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.