若要在其他窗口中使用主窗口的类变量,请使用PyQt5

2024-06-07 13:15:37 发布

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

我有两个QWidget窗口,如图所示。第一张图片是我获取变量输入的图片,另一张图片是我处理变量以显示用户的图片。但是,我没有在另一个类(QWidget)中使用该变量。总结一下; enter image description here

这是我输入数据的窗口

enter image description here

这是我想要处理和显示结果的第二个窗口和类。我需要使用第一个类中定义的变量。我在第二节课上有一个计算函数,它可以做2数学之类的计算。piMain。直径然后我需要调用这个函数来再次显示结果

您可以在下面找到所有代码

from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import sys
import math
from PyQt5.QtGui import QPixmap,QFont
import sqlite3

class Main(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Calculation")
        self.setGeometry(450,100,1250,600)
        self.UI()
        self.show()
    def UI(self):
        self.mainDesign()
        self.layouts()

    def mainDesign(self):
        self.setStyleSheet('background-color:white')
        #CUSTOMER INFORMATION BUTTONS AND TEXT###
        #### CALCULATE BUTTONS###
        self.buttonCalcBillet = QPushButton("Calculate")
        self.buttonCalcBillet.setStyleSheet(
            'background-color: orange;'
            'color: black;'
        )
        ### CALCULATE BUTTONS CLICKED ###
        self.buttonCalcBillet.clicked.connect(self.billetCalculationResults)
        ######
        self.Title = QLabel("Some Maths")
        self.Title.setAlignment(QtCore.Qt.AlignHCenter)
        self.Title.setStyleSheet('font-size: 18pt; font-family: arial,sans-serif')

        self.DiameterLabel = QLabel("Diameter")
        self.DiameterLabel.setStyleSheet('font-size: 12pt; font-family: arial,sans-serif')
        self.DiameterQline = QLineEdit()
        self.DiameterQline.setPlaceholderText("Please Enter Diameter in mm")
        self.DiameterQline.setStyleSheet(
            'font-family:Hack,monospace;'
            'font:12px;'
            'mind-width:20em;'
        )
    def layouts(self):
        #####LAYOUTS#########
        self.mainLayout = QHBoxLayout()
        self.billetLayout = QFormLayout()

        ###ADDING CHILD LAYOUTS TO MAIN LAYOUTS######
        self.mainLayout.addLayout(self.billetLayout,350)
        ###CALCULATION BUTTON WIDGETS###
        self.billetLayout.addRow(self.Title)
        self.billetLayout.addRow(self.DiameterLabel, self.DiameterQline)
        self.billetLayout.addRow(self.buttonCalcBillet)

        ####SETTING MAIN LAYOUT###
        self.setLayout(self.mainLayout)

    def billetCalculationResults(self):
        self.billetCalculation = BilletCalculationResults()
        self.GetValues()
        self.close()


    def GetValues(self):

        self.Diameter = float(self.DiameterQline.text())

class BilletCalculationResults(QWidget):
      def __init__(self):
          super().__init__()
          self.setWindowTitle("Calculation Results")
          self.setGeometry(450,150,350,600)
          ####CONSTRUCTION OF THE FIRST BILLET CLASS ###
          self.UI()
          self.show()
      def UI(self):
          self.billetCalculationPageDesign()
          self.billetCalculationLayouts()
      def billetCalculationPageDesign(self):
          ### BILLET RESULTS OF CALCULATION PAGE DESIGN ###
          self.billetCalSurfaceAreaLabel = QLabel("Surface Area : ")
          self.billetCalSurfaceAreaLabel.setStyleSheet('font-size: 12pt; font-family: arial,sans-serif')
          self.billetCalSurfaceAreaLabelResult = QLabel(" : ")
          self.billetCalSurfaceAreaLabelResult.setStyleSheet('font-size: 12pt; font-family: arial,sans-serif')

      def billetCalculationLayouts(self):
          ## BILLET RESULTS OF CALCULATION PAGE DESIGN ###
          self.billetMainLayout = QFormLayout()
          self.billetMainLayout.addRow(self.billetCalSurfaceAreaLabel,self.billetCalSurfaceAreaLabelResult)
          self.setLayout(self.billetMainLayout)

     ##def calculation():
     ## Something like : return Main.Diameter * 2 * math.pi
def main():
    APP = QApplication(sys.argv)
    window = Main()
    sys.exit(APP.exec())
if __name__== '__main__':
    main()

Tags: importselfuititleinitdef图片family
1条回答
网友
1楼 · 发布于 2024-06-07 13:15:37

要将参数传递给另一个类,可以按如下方式传递:

self.billetCalculation = BilletCalculationResults(self.GetValues())

在类BilletCalculationResultinit方法中使用它,如下所示:

def __init__(self, diameter):
    self.diameter = diameter

完整代码如下:

from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import sys
import math
from PyQt5.QtGui import QPixmap, QFont
import sqlite3


class Main(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Calculation")
        self.setGeometry(450, 100, 1250, 600)
        self.UI()
        self.show()

    def UI(self):
        self.mainDesign()
        self.layouts()

    def mainDesign(self):
        self.setStyleSheet('background-color:white')
        # CUSTOMER INFORMATION BUTTONS AND TEXT###
        #### CALCULATE BUTTONS###
        self.buttonCalcBillet = QPushButton("Calculate")
        self.buttonCalcBillet.setStyleSheet(
            'background-color: orange;'
            'color: black;'
        )
        ### CALCULATE BUTTONS CLICKED ###
        self.buttonCalcBillet.clicked.connect(self.billetCalculationResults)
        ######
        self.Title = QLabel("Some Maths")
        self.Title.setAlignment(QtCore.Qt.AlignHCenter)
        self.Title.setStyleSheet('font-size: 18pt; font-family: arial,sans-serif')

        self.DiameterLabel = QLabel("Diameter")
        self.DiameterLabel.setStyleSheet('font-size: 12pt; font-family: arial,sans-serif')
        self.DiameterQline = QLineEdit()
        self.DiameterQline.setPlaceholderText("Please Enter Diameter in mm")
        self.DiameterQline.setStyleSheet(
            'font-family:Hack,monospace;'
            'font:12px;'
            'mind-width:20em;'
        )

    def layouts(self):
        #####LAYOUTS#########
        self.mainLayout = QHBoxLayout()
        self.billetLayout = QFormLayout()

        ###ADDING CHILD LAYOUTS TO MAIN LAYOUTS######
        self.mainLayout.addLayout(self.billetLayout, 350)
        ###CALCULATION BUTTON WIDGETS###
        self.billetLayout.addRow(self.Title)
        self.billetLayout.addRow(self.DiameterLabel, self.DiameterQline)
        self.billetLayout.addRow(self.buttonCalcBillet)

        ####SETTING MAIN LAYOUT###
        self.setLayout(self.mainLayout)

    def billetCalculationResults(self):
        self.billetCalculation = BilletCalculationResults(self.GetValues())
        self.close()

    def GetValues(self):
        return float(self.DiameterQline.text())


class BilletCalculationResults(QWidget):
    def __init__(self, diameter):
        self.diameter = diameter
        super().__init__()
        self.setWindowTitle("Calculation Results")
        self.setGeometry(450, 150, 350, 600)
        ####CONSTRUCTION OF THE FIRST BILLET CLASS ###
        self.UI()
        self.show()

    def UI(self):
        self.billetCalculationPageDesign()
        self.billetCalculationLayouts()

    def billetCalculationPageDesign(self):
        ### BILLET RESULTS OF CALCULATION PAGE DESIGN ###
        print(self.calculation())
        self.billetCalSurfaceAreaLabel = QLabel("Surface Area : ")
        self.billetCalSurfaceAreaLabel.setStyleSheet('font-size: 12pt; font-family: arial,sans-serif')
        self.billetCalSurfaceAreaLabelResult = QLabel(f"{str(self.calculation())}")
        self.billetCalSurfaceAreaLabelResult.setStyleSheet('font-size: 12pt; font-family: arial,sans-serif')

    def billetCalculationLayouts(self):
        ## BILLET RESULTS OF CALCULATION PAGE DESIGN ###
        self.billetMainLayout = QFormLayout()
        self.billetMainLayout.addRow(self.billetCalSurfaceAreaLabel, self.billetCalSurfaceAreaLabelResult)
        self.setLayout(self.billetMainLayout)

    def calculation(self):
        return self.diameter * 2 * math.pi


def main():
    APP = QApplication(sys.argv)
    window = Main()
    sys.exit(APP.exec())


if __name__ == '__main__':
    main()

相关问题 更多 >

    热门问题