In this PyQt5 post i want to show you Creating of WHATISTHIS Class Widget . so first of all what is WHATISTHIS class in PyQt5 ? WHATISTHIS class provides a description of the purpose of any widget, it is similar to QTooltip.
What is QWhatsThis ?
QWhatsThis is a class in PyQt5 that provides context sensitive help for widgets. when the user clicks the “What’s This?” button in a widget’s context menu, a help message is displayed in a pop-up window. The message is typically a short description of the widget’s purpose or instructions for how to use it.
Join PyQt5 Full Course for Free
PyQt5 Course
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel, QPushButton, QWhatsThis import sys |
After that we are going to create our main Window class that extends from QWidget. and in the constructor of the class we need to add some requirements of the window like set window title, window icon and window geometry.
1 2 3 4 5 6 7 8 9 10 11 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Whatisthis" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.setWindowTitle(self.title) self.setWindowIcon(QtGui.QIcon("icon.png")) self.setGeometry(self.left, self.top, self.width, self.height) |
In here we are going to create a QHBoxLayout object with QLabel. also we have added the label to the hbox layout.
1 2 3 |
hbox = QHBoxLayout() label = QLabel("Focus And Press SHIFT + F1") hbox.addWidget(label) |
So now we have created a QPushButton and we have used setWhatsThis() method for the button.
1 2 3 |
button = QPushButton("Click Me", self) button.setWhatsThis("This is a button that you can click on this button") hbox.addWidget(button) |
In here we need to set the layout for our main window and show the window.
1 2 |
self.setLayout(hbox) self.show() |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Complete source code PyQt5 Creating WHATISTHIS Class Widget
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel, QPushButton, QWhatsThis import sys class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Whatisthis" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.setWindowTitle(self.title) self.setWindowIcon(QtGui.QIcon("icon.png")) self.setGeometry(self.left, self.top, self.width, self.height) hbox = QHBoxLayout() label = QLabel("Focus And Press SHIFT + F1") hbox.addWidget(label) button = QPushButton("Click Me", self) button.setWhatsThis("This is a button that you can click on this button") hbox.addWidget(button) self.setLayout(hbox) self.show() if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
Run the complete code and this will be the result.
Also you can watch the complete video for this article.
Subscribe and Get Free Video Courses & Articles in your Email