PyQt5:从一个 class 调用函数到另一个 Class?

PyQt5 : Call function from One class to another Class?

我是 Python 和 PyQt5 的新手。

创建两个 python 文件。第一个包含屏幕几何数据。在下一个文件中,我使用第一个文件的数据创建了一个简单的 window。 工作正常。但我并不满意,因为我认为这不是一个正确的方式或 Pythonic 方式。所以我寻求你的关注,以改进它。

First File

import sys
from PyQt5.QtWidgets import *

class Mylayout(QWidget):
    def __init__(self):
        super().__init__()
        self.myscreen()

    def myscreen(self):
        global screen_width
        global screen_height
        global startpoint_x
        global startpoint_y

        screen_width  = 1000
        screen_height = 500

        resolution_width = QDesktopWidget().screenGeometry().width()
        resolution_height= QDesktopWidget().screenGeometry().height()

        if resolution_width > screen_width:
            startpoint_x = round((resolution_width - screen_width)/2)

        else:
            startpoint_x = 0


        if resolution_height > screen_height:
            startpoint_y = round((resolution_height - screen_height)/2)
        else:
            startpoint_y = 30

        return startpoint_x,startpoint_y,screen_width,screen_height

def main():
    myapp = QApplication(sys.argv)
    mywindow = Mylayout()
    mywindow.setGeometry(startpoint_x,startpoint_y,screen_width,screen_height)
    mywindow.show()
    sys.exit(myapp.exec_())

if __name__ =="__main__":
    main()

Second File

from firstfile import *

class example(QWidget):
    def __init__(self):
        super().__init__()

        x = Mylayout()
        y = x.myscreen()
        xpoint = (y[0])
        ypoint = (y[1])
        width  = (y[2])
        height = (y[3])

        self.setGeometry(xpoint, ypoint, width, height)

def main():
    myapp = QApplication(sys.argv)
    mywindow = example()
    mywindow.show()
    sys.exit(myapp.exec_())

if __name__ == "__main__":
    main()

试试这个,

我也是新来的。所以有人建议,这种方式是对还是错:

First File

import sys
from PyQt5.QtWidgets import *

class Mylayout(QWidget):
    def __init__(self,mywidget):
        self.mywindow = mywidget
        self.myscreen()

    def myscreen(self):
        screen_width  = 1000
        screen_height = 500

        resolution_width = QDesktopWidget().screenGeometry().width()
        resolution_height= QDesktopWidget().screenGeometry().height()

        if resolution_width > screen_width:
            startpoint_x = round((resolution_width - screen_width)/2)
        else:
            startpoint_x = 0

        if resolution_height > screen_height:
            startpoint_y = round((resolution_height - screen_height)/2)
        else:
            startpoint_y = 30

        return startpoint_x,startpoint_y,screen_width,screen_height

Second File

import sys
from PyQt5.QtWidgets import *
from firstfile import *

class example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.myframe = QMainWindow(self)
        self.getsize = Mylayout(self.myframe)
        xpoint,ypoint,width,height = self.getsize.myscreen()
        self.setGeometry(xpoint, ypoint, width, height)

def main():
    myapp = QApplication(sys.argv)
    mywindow = example()
    mywindow.show()
    sys.exit(myapp.exec_())

if __name__ == "__main__":
    main()