![]() Pressing the button will do nothing, we'll sort that next. Run it! You will now see your window again, but this time with the QPushButton widget in the middle. This is a QMainWindow specific function that allows you to set the widget that goes in the middle of the window. When creating the button you can pass in the text that you want the button to display.įinally, we call. This is one of the basic widgets available in Qt. ![]() Then we add our first widget - a QPushButton - to the middle of the window. setWindowTitle() to change the title of our main window. When you subclass a Qt class you must always call the super _init_ function to allow Qt to set up the object. We'll look at how to add multiple widgets to windows in the layouts tutorial. setCentralWidget to place a widget (here a QPushButton) in the QMainWindow - by default it takes the whole of the window. The core Qt widgets are always imported from the QtWidgets namespace, as are the QMainWindow and QApplication classes. # Subclass QMainWindow to customize your application's main windowįor this demo we're using a QPushButton. There is only one event loop running at any timeįrom PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton You application sits waiting in the event loop until an action is taken There is only one running event loop per application. The event handler deals with the event, then passes control back to the event loop to wait for more events. In the event loop, the queue is checked on each iteration and if a waiting event is found, the event and control is passed to the specific event handler for the event. This object holds the event loop of your application - the core loop which governs all user interaction with the GUI.Įach interaction with your application - whether a press of a key, click of a mouse, or mouse movement - generates an event which is placed on the event queue. Every application needs one - and only one - QApplication object to function. The core of every Qt Applications is the QApplication class. If you're already familiar with event loops you can safely skip to the next section. What's the event loop?īefore getting the window on the screen, there are a few key concepts to introduce about how applications are organized in the Qt world. This was a legacy feature avoid a clash with the exec reserved word in Python 2. Application will (by default) exit when last window is closedįinally, we call app.exec() to start up the event loop. Every application needs at least one (.but can have more) Holds the user-interface of your application ![]() show() and run the app, but you'll have no way to quit it! So, after creating the window object, we must always call. Widgets without a parent are invisible by default. This means you can technically create a window using any widget you like. In Qt all top level widgets are windows - that is, they don't have a parent and are not nested within another widget or layout. If you do mess up, Python will let you know what's wrong. Type it in verbatim, and be careful not to make mistakes. The source code for the application is shown below. We'll be editing within this file as we go along, and you may want to come back to earlier versions of your code, so remember to keep regular backups. app.py) and save it somewhere accessible. Let's create our first application! To start create a new Python file - you can call it whatever you like (e.g. These will be explored in more detail in the subsequent tutorials. Finally we'll look at Qt's QMainWindow which offers some useful common interface elements such as toolbars and menus. Then we'll take a brief look at the event loop and how it relates to GUI programming in Python. In this tutorial we'll learn how to use PyQt to create desktop applications with Python.įirst we'll create a series of simple windows on your desktop to ensure that PyQt is working and introduce some of the basic concepts.
0 Comments
Leave a Reply. |