Reputation: 313
I have two QWidget windows as in the pictures. 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). To summarize it ;
This is the window I have entered my data.
This is the second window and class where I want to process and show the result. I need to use the variable defined in the first class. I have a calculation function in the second class that does the calculation like 2math. piMain. Diameter. Then I need to recall this function to show the result again.
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()
Upvotes: 0
Views: 823
Reputation: 166
To pass and argument to another class you can pass it like this:
self.billetCalculation = BilletCalculationResults(self.GetValues())
And to use it in class BilletCalculationResult
init method like this:
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()
Upvotes: 1