Search code examples
pythonpyqtpyqt5python-module

PyQt5 : Call function from One class to another Class?


I am new to Python and PyQt5.

Create two python files. First One contains a data for screen Geometry. And the next file I create a simple window with the data of first file. Works Fine. But I am not satisfied, because I think this is not a proper way or Pythonic way. So I seek your attention, to improve it.

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

Solution

  • Try this,

    I am also new. So somebody suggest, this way is correct or wrong :

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