PyQt5 - Скрыть кнопку при нажатии

Опубликовано: 8 Апреля, 2022

In this article we will see how to hide the push button. When we design a GUI (Graphical User Interface) we create push button in it which do some task when they are pressed. But some time their is a need to hide the button if task is completed, in order to hide the button we will use hide method.

Syntax : button.hide()

Argument : It takes no argument.

Acton performed : It hides the push button

Код:

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # creating a button
        self.button = QPushButton("CLICK", self)
  
        # setting up the geometry
        self.button.setGeometry(200, 150, 200, 40)
  
        # connecting method when button get clicked
        self.button.clicked.connect(self.clickme)
  
        # showing all the widgets
        self.show()
  
    # action method
    def clickme(self):
  
        # hiding the button
        self.button.hide()
        # printing pressed
        print("pressed")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Выход :

Когда кнопка нажата, вывод будет сгенерирован, и кнопка будет скрыта.

 прижатый

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.