Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
Hello everyone. I am doing a calculator by using Qt library in Python and i want to create a slot for multiple buttons.The problem is clickNumberButton function does not work. Please could you review my code. Where is my a mistake. Thank you


If i use connection self.ui.pB0.clicked.connect(partial(self.clickNumberButton,'pB0')) like this it works.


What I have tried:

Python
<pre>from CalcUI import Ui_Form
from PyQt5 import Qt
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import  QApplication, QWidget, QPushButton, QLineEdit
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
from functools import partial
import sys

class Calc(QObject):
     def __init__(self):
         self.app = QtWidgets.QApplication(sys.argv)
         self.widget = QtWidgets.QWidget()
         self.digit = 90
         self.ui = Ui_Form()
         self.ui.setupUi(self.widget)
         self.widget.setWindowTitle("Calculator")

         self.initConnection()
         self.widget.show()
         self.app.exec_()
    
     def initConnection(self):
         self.ui.pB0.clicked.connect(self.clickNumberButton)
         self.ui.pB1.clicked.connect(self.clickNumberButton)
         #self.ui.pB0.clicked.connect(partial(self.clickNumberButton,'pB0'))
         #self.ui.pB1.clicked.connect(partial(self.clickNumberButton,'pB1'))
         
     def clickNumberButton(self):
         print('click')
         
calcWidget = Calc()
Posted
Updated 14-Jul-19 13:34pm
v4
Comments
Richard MacCutchan 14-Jul-19 3:06am    
What is the problem?
Akmal Bukhariev 14-Jul-19 4:06am    
The problem is clickNumberButton function does not work.
If i use connection >>self.ui.pB0.clicked.connect(partial(self.clickNumberButton,'pB0'))<< like this it works
Richard MacCutchan 14-Jul-19 7:31am    
What does "does not work" mean?
Please explain exactly what happens or does not happen; we cannot guess.
Akmal Bukhariev 14-Jul-19 19:36pm    
when i click ui.pB0 or ui.pB1 it does not print 'click' it means
ui.pB0.clicked.connect does not work.
If i use connection self.ui.pB0.clicked.connect(partial(self.clickNumberButton,'pB0')) like this it works
Richard MacCutchan 15-Jul-19 2:53am    
I m sorry but "does not work", and "it works", mean nothing.

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