Screen placement based on screen resolution

P

Pat

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
 
L

Lonnie Princehouse

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

Fredrik Lundh

Pat said:
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>
 
F

Fredrik Lundh

Lonnie said:
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>
 
P

Pat

Thanks a lot for you response.
S
Fredrik Lundh said:
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>
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top