[英]To Use Class Variable of the Main Window in Other Window, PyQt5
如圖所示,我有兩個 QWidget windows。 我獲得變量輸入的第一張圖片,另一張我處理變量以顯示用戶的圖片。 但是,我沒有設法在其他 class (QWidget) 中使用該變量。 總結一下;
這是我輸入數據的 window。
這是我要處理並顯示結果的第二個 window 和 class 。 我需要使用第一個 class 中定義的變量。 我在第二個 class 中有一個計算 function ,它的計算類似於 2數學。 pi主要。 直徑。 然后我需要調用這個 function 來再次顯示結果。
您可以在下面找到所有代碼。
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()
要將參數傳遞給另一個 class 您可以像這樣傳遞它:
self.billetCalculation = BilletCalculationResults(self.GetValues())
並在 class BilletCalculationResult
init 方法中使用它,如下所示:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.