Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a calculator app using PyQt5, and when I'm assigning an action to my button to update the label text that resides in a different class from the button class. It seems to be doing something, but instead of what I'm expecting to happen. It seems to remove all of my calculator buttons, and show the label without even updating its text.


Python
<pre>import PyQt5.QtWidgets as qtwidget
import PyQt5.QtGui as qtgui
import PyQt5.QtCore as qtcore


app = qtwidget.QApplication([])

class Button(qtwidget.QPushButton):
    def __init__(self, text, parent=None):
        super().__init__(text, parent=parent)
        self.text = text
        button_policy = self.sizePolicy()
        button_policy.setVerticalPolicy(button_policy.Policy.Minimum)
        self.setSizePolicy(button_policy)
        
        # Set text font style & size
        self.setFont(qtgui.QFont('Helvetica', 20))
        
        # assign click action to button_click function
        self.clicked.connect(self.button_click)
    
    # The problem seeems to be here
    def button_click(self):
        # declare MainWindow class
        label_obj = MainWindow()
        
        # access label attribute of MainWindow class
        label_text = label_obj.label
        
        # Set text of label to button text
        label_text.setText(self.text)
        
btn_percent = Button('%')
btn_ce = Button('CE')
btn_c = Button('C')
btn_back = Button('Back')
btn_sign = Button('+/-')
btn1 = Button('1')
btn2 = Button('2')
btn3 = Button('3')
btn4 = Button('4')
btn5 = Button('5')
btn6 = Button('6')
btn7 = Button('7')
btn8 = Button('8')
btn9 = Button('9')
btn0 = Button('0')
btn_division = Button('/')
btn_multiply = Button('x')
btn_minus = Button('-')
btn_plus = Button('+')
btn_dot = Button('.')
btn_equal = Button('=')

class MainWindow(qtwidget.QWidget):
    def __init__(self):
        super().__init__()
            # Add a title
        self.setWindowIcon(qtgui.QIcon('.\icons\calculator_icon.ico'))
        self.setWindowTitle('Calculator Copy')
        
        # Create a label
        self.label = qtwidget.QLabel("0")
        # Set background color
        self.label.setStyleSheet("background-color: #e3e1da;\
                                border: 1px solid black;\
                                padding-top: 10px;\
                                padding-bottom: 10px")
        
        # Set label maximum height
        self.label.setMaximumHeight(100)
        
        # Align label text
        self.label.setAlignment(qtcore.Qt.AlignmentFlag.AlignVCenter | qtcore.Qt.AlignmentFlag.AlignRight)        
        
        # Set label text font and size
        self.label.setFont(qtgui.QFont('Helvetica', 40))
        
        # Add grid layout for calculator buttons
        main_gridlayout = qtwidget.QGridLayout()
        
        # Add label to main widget
        main_gridlayout.addWidget(self.label, 0, 0, 1, 4)
        
        # Add buttons to main widget
        main_gridlayout.addWidget(btn_percent, 1, 0)
        main_gridlayout.addWidget(btn_ce, 1, 1)
        main_gridlayout.addWidget(btn_back, 1, 2)
        main_gridlayout.addWidget(btn_division, 1, 3)
        main_gridlayout.addWidget(btn7, 2, 0)
        main_gridlayout.addWidget(btn8, 2, 1)
        main_gridlayout.addWidget(btn9, 2, 2)
        main_gridlayout.addWidget(btn_multiply, 2, 3)
        main_gridlayout.addWidget(btn4, 3, 0)
        main_gridlayout.addWidget(btn5, 3, 1)
        main_gridlayout.addWidget(btn6, 3, 2)
        main_gridlayout.addWidget(btn_minus, 3, 3)
        main_gridlayout.addWidget(btn1, 4, 0)
        main_gridlayout.addWidget(btn2, 4, 1)
        main_gridlayout.addWidget(btn3, 4, 2)
        main_gridlayout.addWidget(btn_plus, 4, 3)
        main_gridlayout.addWidget(btn_sign, 5, 0)
        main_gridlayout.addWidget(btn0, 5, 1)
        main_gridlayout.addWidget(btn_dot, 5, 2)
        main_gridlayout.addWidget(btn_equal, 5, 3)
        
        self.setLayout(main_gridlayout)
        self.show()

mw = MainWindow()

app.exec_()


What I have tried:

everything I could think of...
Posted
Updated 4-Aug-22 22:14pm
v2
Comments
Richard MacCutchan 7-Jul-22 4:26am    
You need to explain where the problem is in the above code. Also what is the text that is being replaced?

1 solution

You should not create a new instance of MainWindow() class inside button_click() method. Use the same mw you created, when accessing the label attribute and setting the text.

I did it for you.

import PyQt5.QtWidgets as qtwidget
import PyQt5.QtGui as qtgui
import PyQt5.QtCore as qtcore


app = qtwidget.QApplication([])

class Button(qtwidget.QPushButton):
    def __init__(self, text, parent=None):
        super().__init__(text, parent=parent)
        self.text = text
        button_policy = self.sizePolicy()
        button_policy.setVerticalPolicy(button_policy.Policy.Minimum)
        self.setSizePolicy(button_policy)
        
        # Set text font style & size
        self.setFont(qtgui.QFont('Helvetica', 20))
        
        # assign click action to button_click function
        self.clicked.connect(self.button_click)
    
    # NOW THERE IS NO PROBLEM, KEEP DEVELOPING THE APP :)
    def button_click(self):
        # declare MainWindow class
        #label_obj = MainWindow()
        main_window = mw
        
        # access label attribute of MainWindow class
        #label_text = label_obj.label
        label_mw = mw.label
        
        # Set text of label to button text
        #label_text.setText('f***')
        label_mw.setText(self.text)
        
btn_percent = Button('%')
btn_ce = Button('CE')
btn_c = Button('C')
btn_back = Button('Back')
btn_sign = Button('+/-')
btn1 = Button('1')
btn2 = Button('2')
btn3 = Button('3')
btn4 = Button('4')
btn5 = Button('5')
btn6 = Button('6')
btn7 = Button('7')
btn8 = Button('8')
btn9 = Button('9')
btn0 = Button('0')
btn_division = Button('/')
btn_multiply = Button('x')
btn_minus = Button('-')
btn_plus = Button('+')
btn_dot = Button('.')
btn_equal = Button('=')

class MainWindow(qtwidget.QWidget):
    def __init__(self):
        super().__init__()
            # Add a title
        self.setWindowIcon(qtgui.QIcon('.\icons\calculator_icon.ico'))
        self.setWindowTitle('Calculator Copy')
        
        # Create a label
        self.label = qtwidget.QLabel("0")
        # Set background color
        self.label.setStyleSheet("background-color: #e3e1da;\
                                border: 1px solid black;\
                                padding-top: 10px;\
                                padding-bottom: 10px")
        
        # Set label maximum height
        self.label.setMaximumHeight(100)
        
        # Align label text
        self.label.setAlignment(qtcore.Qt.AlignmentFlag.AlignVCenter | qtcore.Qt.AlignmentFlag.AlignRight)        
        
        # Set label text font and size
        self.label.setFont(qtgui.QFont('Helvetica', 40))
        
        # Add grid layout for calculator buttons
        main_gridlayout = qtwidget.QGridLayout()
        
        # Add label to main widget
        main_gridlayout.addWidget(self.label, 0, 0, 1, 4)
        
        # Add buttons to main widget
        main_gridlayout.addWidget(btn_percent, 1, 0)
        main_gridlayout.addWidget(btn_ce, 1, 1)
        main_gridlayout.addWidget(btn_back, 1, 2)
        main_gridlayout.addWidget(btn_division, 1, 3)
        main_gridlayout.addWidget(btn7, 2, 0)
        main_gridlayout.addWidget(btn8, 2, 1)
        main_gridlayout.addWidget(btn9, 2, 2)
        main_gridlayout.addWidget(btn_multiply, 2, 3)
        main_gridlayout.addWidget(btn4, 3, 0)
        main_gridlayout.addWidget(btn5, 3, 1)
        main_gridlayout.addWidget(btn6, 3, 2)
        main_gridlayout.addWidget(btn_minus, 3, 3)
        main_gridlayout.addWidget(btn1, 4, 0)
        main_gridlayout.addWidget(btn2, 4, 1)
        main_gridlayout.addWidget(btn3, 4, 2)
        main_gridlayout.addWidget(btn_plus, 4, 3)
        main_gridlayout.addWidget(btn_sign, 5, 0)
        main_gridlayout.addWidget(btn0, 5, 1)
        main_gridlayout.addWidget(btn_dot, 5, 2)
        main_gridlayout.addWidget(btn_equal, 5, 3)
        
        self.setLayout(main_gridlayout)
        self.show()

mw = MainWindow()

app.exec_()
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900