RKh
RKh

Reputation: 14159

Passing window name as parameter to a class

I created a class containing a method to position a window anywhere on the screen. I am using PyQt4 for GUI programming. I wrote following class:

from PyQt4 import QtGui

class setWindowPosition:
    def __init__(self, xCoord, yCoord, windowName, parent = None):
        self.x = xCoord
        self.y = yCoord
        self.wName = windowName;

    def AdjustWindow(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

This code needs correction. Any file that imports this class will pass three parameters: desired_X_Position, desired_Y_position and its own name to this class. The method AdjustWindow should accept these three parameters and position the calling window to the desired coordinates.

In the above code, though I have passed the parameters, but not following how to modify the AdjustWindow method.

Upvotes: 0

Views: 218

Answers (1)

unholysampler
unholysampler

Reputation: 17341

It is not entirely clear what you are trying to ask, But, you access the values in the method the same way you set them in the constructor.

from PyQt4 import QtGui

class setWindowPosition:
    def __init__(self, xCoord, yCoord, windowName, parent = None):
        self.x = xCoord
        self.y = yCoord
        self.wName = windowName;

    def AdjustWindow(self):
        print self.x, self.y, self.wName //See Here
        //now use them how you want
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

EDIT: I found this page which seems to be where you grabbed the code from. Your class is not inheriting from QtGui.QWidget so calls to geometry() and move() are going to fail. Once you do that, it looks like it the code would be:

def AdjustWindow(self):
  self.move(self.x, self.y)

However, you still need to figure out how to have your class as the one that controls the window with windowName. It seems like this package is for making GUIs and not controlling external windows. I could be wrong as I have only read enough to make this answer.

Upvotes: 1

Related Questions