简体   繁体   English

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

[英]PyQt5 : Call function from One class to another Class?

I am new to Python and PyQt5.我是 Python 和 PyQt5 的新手。

Create two python files.创建两个 python 文件。 First One contains a data for screen Geometry.第一个包含屏幕几何的数据。 And the next file I create a simple window with the data of first file.下一个文件我用第一个文件的数据创建了一个简单的 window。 Works Fine .工作正常 But I am not satisfied, because I think this is not a proper way or Pythonic way.但我并不满意,因为我认为这不是正确的方式或 Pythonic 方式。 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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM