In this article we will see how to change the text of existing push button. We know when we create a button, we set the text to it but sometimes, conditions occur in which re-usability of push button occur i.e button should do its normal operation just change the text of it, so it look different.
In order to do this we use setText
method. This method will over-write the existing text of push button.
Syntax : button.setText(new_text)
Argument : It takes string as argument.
Action performed : It over-write the text of button.
Code :
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle( "Python " )
self .setGeometry( 100 , 100 , 600 , 400 )
self .UiComponents()
self .show()
def UiComponents( self ):
button = QPushButton( "CLICK" , self )
button.setGeometry( 200 , 150 , 100 , 30 )
button.clicked.connect( self .clickme)
button.setText( "Over-write" )
def clickme( self ):
print ( "pressed" )
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
