Jun-02-2022, 09:00 PM
Just an idea
import sys from PyQt5.QtWidgets import (QApplication, QPushButton, QWidget, QLabel, QVBoxLayout,QSpinBox, QMainWindow) from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self, parent = None): super(MainWindow, self).__init__(parent) self.ex = None self.checkout_value = 0 cw = QWidget() vbox = QVBoxLayout() self.btn = QPushButton("Click", clicked = self.btn_clicked) self.lbl = QLabel(alignment = Qt.AlignCenter) vbox.addWidget(self.btn) vbox.addWidget(self.lbl) cw.setLayout(vbox) self.setCentralWidget(cw) self.setGeometry(100, 100, 400, 400) def btn_clicked(self): if not self.ex: self.ex = spindemo(parent = self) self.ex.show() def update_label(self): self.lbl.setText(f'CheckoutQuantity: {self.checkout_value}') print((f'CheckoutQuantity: {self.checkout_value}')) self.ex = None class spindemo(QWidget): def __init__(self, parent = None): super(spindemo, self).__init__(parent) self.setStyleSheet("QSpinBox, QPushButton {background: #3465a4; color: #eeeeec;}") layout = QVBoxLayout() self.messageText = QLabel("How much quantity are you taking?") self.messageText.setAlignment(Qt.AlignCenter) layout.addWidget(self.messageText) self.CheckoutSpinBox = QSpinBox() self.ConfirmButton = QPushButton() self.ConfirmButton.setText("Confirm") layout.addWidget(self.CheckoutSpinBox) layout.addWidget(self.ConfirmButton) self.CheckoutSpinBox.valueChanged.connect(self.currentValue) self.setLayout(layout) self.setWindowTitle("Checkout") #------------------------------------------ #Confim Button #------------------------------------------ #When the Confirm button is clicked -> Confirm Function ConfirmButton = self.ConfirmButton ConfirmButton.clicked.connect(self.ConfirmClicked) #------------------------------------------ self.setGeometry(self.parent().x(), self.parent().y(), round(self.parent().width() / 2), round(self.parent().height() / 4)) def currentValue(self): #Show the current value of the SpinBox in real time self.messageText.setText("You are taking: "+str(self.CheckoutSpinBox.value())) def ConfirmClicked(self): CheckoutQuantity = self.CheckoutSpinBox.value() print("Quantity you are taking: ", CheckoutQuantity) self.parent().checkout_value = CheckoutQuantity #self.parent().lbl.setText(f'CheckoutQuantity: {checkout_value}') self.parent().update_label() self.close() def main(): app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_()) if __name__ == '__main__': main()