PyQt5 – 点击时隐藏按钮

PyQt5 – 点击时隐藏按钮

在这篇文章中,我们将看到如何隐藏按钮。当我们设计一个GUI(图形用户界面)时,我们在其中创建了一些按钮,当它们被按下时,会执行一些任务。但是在某些时候,如果任务完成了,就需要隐藏按钮,为了隐藏按钮,我们将使用隐藏方法。

语法: button.hide()

参数: 它不需要参数。

执行的动作: 它隐藏了按钮。

代码。

# 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())

输出。

PyQt5 - 点击时隐藏按钮

当按下按钮时,输出将产生,按钮将被隐藏。

pressed 

PyQt5 - 点击时隐藏按钮

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程