Skip to content

Instantly share code, notes, and snippets.

@Sanket758
Last active February 8, 2020 15:44
Show Gist options
  • Save Sanket758/8bb354ec9ee6456fa8a8b9239ff33b93 to your computer and use it in GitHub Desktop.
Save Sanket758/8bb354ec9ee6456fa8a8b9239ff33b93 to your computer and use it in GitHub Desktop.
Getting started with PyQt. Here we have created a single page single layout Qt5 app where it displays a label on the window.
'''
First you will need to install qt5 on your system,
for that you will need to do this in you terminal:
sudo apt-get qt5-default
and then,
pip install sip PyQt5
'''
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
# Create a HomePage for our Qtapp
class Home(QWidget):
def __init__(self, parent=None):
super(Home, self).__init__(parent)
# Lets create a label to display in our app
label = QLabel('Sanket Gadge')
# Lets create a layout for our app
layout = QVBoxLayout()
# Add our label to this layout
layout.addWidget(label)
# Once we have everything set up, we have all our widgets and layouts created its time to set the layout for our app.
self.setLayout(layout)
# Then we give name to a window to show our widgets and layout
self.setWindowTitle("My QT app")
# Once we have our class created its time to call it
if __name__ == '__main__':
import sys
# Define a name for our app
app = QApplication(sys.argv)
# Call the class object we have created earlier
window = Home()
# This line is so important cuz if you dont show() it wont show a thing.
window.show()
# When user wants to exit, it will raise a exit call for our app, this will run when you press the close mark(beside minimize button)in the app window
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment