简体   繁体   English

在其他 Window、Z8BDB75FF7B8D600ACD3FD789F4E2C7A3 中使用主变量 Window 的 Class 变量

[英]To Use Class Variable of the Main Window in Other Window, PyQt5

I have two QWidget windows as in the pictures.如图所示,我有两个 QWidget windows。 The first picture where I get the input of the variable, the other picture where I process the variable to show the user.我获得变量输入的第一张图片,另一张我处理变量以显示用户的图片。 However, I did not manage to use the variable in the other class (QWidget).但是,我没有设法在其他 class (QWidget) 中使用该变量。 To summarize it;总结一下; 在此处输入图像描述

This is the window I have entered my data.这是我输入数据的 window。

在此处输入图像描述

This is the second window and class where I want to process and show the result.这是我要处理并显示结果的第二个 window 和 class 。 I need to use the variable defined in the first class.我需要使用第一个 class 中定义的变量。 I have a calculation function in the second class that does the calculation like 2 math.我在第二个 class 中有一个计算 function ,它的计算类似于 2数学。 pi Main. pi主要。 Diameter.直径。 Then I need to recall this function to show the result again.然后我需要调用这个 function 来再次显示结果。

You can find the all code below.您可以在下面找到所有代码。

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

To pass and argument to another class you can pass it like this:要将参数传递给另一个 class 您可以像这样传递它:

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

And to use it in class BilletCalculationResult init method like this:并在 class BilletCalculationResult init 方法中使用它,如下所示:

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

Full code below:完整代码如下:

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

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

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