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
    following:

    root.geometry('WxH+X+Y')

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

  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
    #2
    1. Advertising

  3. Pat

    Pat Guest

    Thanks.
    S
    "Lonnie Princehouse" <> wrote in message
    news:...
    > 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))
    >
    Pat, Apr 7, 2006
    #3
  4. "Pat" <> wrote:

    > 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 following:
    >
    > root.geometry('WxH+X+Y')
    >
    > It appears the values for X and Y need to be integers and not a variable
    > like width/2-40


    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
    basics:

    http://docs.python.org/tut/node9.html

    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

    http://effbot.org/tkinterbook/wm.htm#Tkinter.Wm.geometry-method

    which includes code that parses a geometry string.

    </F>
    Fredrik Lundh, Apr 8, 2006
    #4
  5. Lonnie Princehouse wrote:

    > Tkinter takes strings as its arguments; it's TCL's legacy.


    geometry strings are an X windows thing...

    > 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))


    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".

    </F>
    Fredrik Lundh, Apr 8, 2006
    #5
  6. Pat

    Pat Guest

    Thanks a lot for you response.
    S
    "Fredrik Lundh" <> wrote in message
    news:...
    > "Pat" <> wrote:
    >
    >> 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 following:
    >>
    >> root.geometry('WxH+X+Y')
    >>
    >> It appears the values for X and Y need to be integers and not a variable
    >> like width/2-40

    >
    > 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
    > basics:
    >
    > http://docs.python.org/tut/node9.html
    >
    > 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
    >
    > http://effbot.org/tkinterbook/wm.htm#Tkinter.Wm.geometry-method
    >
    > which includes code that parses a geometry string.
    >
    > </F>
    >
    >
    >
    Pat, Apr 8, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew FPGA
    Replies:
    0
    Views:
    944
    Andrew FPGA
    Sep 26, 2005
  2. Jenny

    Screen resolution

    Jenny, Aug 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    352
    Ken Schaefer
    Aug 26, 2003
  3. Screen resolution

    , Nov 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    332
    Teemu Keiski
    Nov 3, 2003
  4. Guadala Harry

    Discover User's Screen Resolution

    Guadala Harry, Jul 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    396
    bruce barker
    Jul 1, 2004
  5. Replies:
    2
    Views:
    98
Loading...

Share This Page