将窗口名作为参数传递给类

2024-04-19 22:56:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个类,其中包含一个方法,用于将窗口定位在屏幕上的任何位置。我使用PyQt4进行GUI编程。我写了以下课程:

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)

这个代码需要更正。任何导入这个类的文件都将向这个类传递三个参数:desired_X_Positiondesired_Y_position和它自己的名称。方法AdjustWindow应该接受这三个参数,并将调用窗口定位到所需的坐标。在

在上面的代码中,虽然我已经传递了参数,但不遵循如何修改AdjustWindow方法。在


Tags: 方法定位self参数sizedefwidthscreen
1条回答
网友
1楼 · 发布于 2024-04-19 22:56:20

不完全清楚您要问什么,但是,您访问方法中的值的方式与在构造函数中设置值的方式相同。在

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)

编辑: 我找到了this page,它似乎就是您从中获取代码的地方。 您的类不是从QtGui.QWidget继承的,因此对geometry()和{}的调用将失败。一旦你这样做了,看起来代码应该是:

^{pr2}$

但是,您仍然需要弄清楚如何让您的类成为使用windowName控制窗口的类。似乎不是为windows制作的gui。我可能错了,因为我读的书只够做这个答案。在

相关问题 更多 >