Determining window size in Python

Can you determine the x and y size of a window in script?

Sure, just get a handle to the window like this:

win = fpmi.gui.getWindow('MyWindow')

and then get the size params like this:

x = win.location.x y = win.location.y width = win.size.width height = win.size.height

The code:

x = win.location.x
y = win.location.y

bombs out for me but the rest of it works.

I have found itā€™s easier to work with window sizing by selecting the window then use CTRL-P to see and adjust size.

I think nathan was talking about accessing the window size at runtime. How exactly did it ā€œbomb outā€? Did you include the snippet of code from the first code section too?

Iā€™ll preface this reply by saying I donā€™t have much clue what Iā€™m doing here, but checking in the Java documentation shows that location should be followed by brackets. The following code seems to work:

win = fpmi.gui.getWindow('MyWindow') x = win.location().x y = win.location().y width = win.size.width height = win.size.height print x,y print width,height
I also noticed that ā€˜location()ā€™ is in the deprecated list and has been replaced by ā€˜getLocation()ā€™ - does this matter here, Carl?

Al

So, whats going on here is that the location() function is interfering with Pythonā€™s automatic JavaBean property access support. Here is an example: If I have a Java class with functions like getFoo() and setFoo(String foo), then in Python I can say:

objectInstance.foo, referencing ā€œfooā€ as if it were a variable, not a pair of getter/setter functions. This is part of the JavaBean spec.

So, normally when you access the variable ā€œwin.locationā€ Python is really going to call the function win.getLocation(). But, the presence of a ā€œlocation()ā€ function on Window is throwing off the JavaBean stuff, and so you are accessing that function directly, which is why you need the parens to make it work.

More than you wanted to know, eh?

:open_mouth:

I stand informed. Iā€™m assuming then that you should really be directly calling the getLocation() function here.

Oh ya, that works now, thanks.

Where do you to look at Java documentation?

Hi harryting,

You could look at http://java.sun.com/javase/reference/api.jsp under Core API Docs version 6, but I warn you, there is a ton of stuff in there! (and if you know as little about Java as I do it may only confuse things more!)

Al

Somewhat related, do I have access to the Move Forward, Move Backward, Move to Front, Move to Back properties programatically? I found that it could be useful while using the paint component.

Step7,

Iā€™m in so far over my head here that I canā€™t see the light anymore, but for what itā€™s worth, Iā€™ve been able to read the z-order of components on the screen using code like

root = event.source.parent z1 = root.getComponentZOrder(event.source.parent.getComponent('Button')) z2 = root.getComponentZOrder(event.source.parent.getComponent('Text Field')) print "Button z-order = %d" % (z1) print "Text Field z-order = %d" % (z2)
I then superimposed the 2 objects and tried to swap their z-order using the following code:

root.setComponentZOrder(event.source.parent.getComponent('Button'),z2) root.setComponentZOrder(event.source.parent.getComponent('Text Field'),z1)
However, the controls only refreshed when I moved the mouse over them.

Thatā€™s as far as Iā€™ve got!

Al

Hey, thanks Al. Iā€™ll be able to get that working somehow. Thatā€™s just what I was looking for.

You probably just need to call .repaint() on the container after changing its childrenā€™s Z-order.

1 Like

Carl,

Addingroot.repaint()at the end of the previous code did the trick :thumb_left: