Examples of using PyQt QMainWindow

  • 2021-10-13 08:17:05
  • OfStack

Directory PyQt5 Handwritten QMainWindow Example
The interface drawn with designer
PyQt4 Handwritten Window Code

QMainWindow inherits from QWidget
QMainWindow is equivalent to the main interface of the program, with built-in menu and toolBar.
menu options can be easily added using Qt Designer.

For larger interfaces, Qt Designer is more convenient. The. ui file is like xml1 used in Android.
The drawn ui file can be converted into py file by PyUIC in PyQt. There is one class in the converted py file.
Create a new class inherited from QMainWindow to call the generated class.

When the main window closes, closeEvent (self, *args, **kwargs) is called, which overrides this method, plus a few closed operations.
Such as terminating child threads, closing database interfaces, releasing resources and so on.

PyQt5 Handwritten QMainWindow Example

Win7 PyCharm Python3.5.1 PyQt5

Handwriting 1 main window, mainly using menu bar, text edit box, toolbar and status bar


|-- main.py
|-- res
| `-- sword.png
`-- ui
 `-- app_main_window.py

main. py Master File


import sys

from PyQt5.QtWidgets import QApplication
from ui.app_main_window import AppMainWindow

if __name__ == '__main__':
 app = QApplication(sys.argv)
 window = AppMainWindow()
 window.show()
 sys.exit(app.exec_())

app_main_window. py window implementation file


from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QTextEdit


class AppMainWindow(QMainWindow):
 """
  Menu bar, text edit box, toolbar and status bar 
 """

 def __init__(self):
  super().__init__()
  self.init_ui()

 def init_ui(self):
  #  Menu bar 
  self.statusBar().showMessage('Main window is ready')
  self.setGeometry(500, 500, 450, 220)
  self.setMinimumSize(150, 120)
  self.setWindowTitle('MainWindow')

  #  Text edit box 
  text_edit = QTextEdit()
  self.setCentralWidget(text_edit) #  Fill in the remaining positions 

  #  Define exit actions 
  exit_action = QAction(QIcon('res/sword.png'), 'Exit', self)
  exit_action.setShortcut('Ctrl+Q')
  exit_action.setStatusTip('Exit App') #  Prompt in the window status bar when the mouse points to an option 
  # exit_action.triggered.connect(QCoreApplication.instance().quit)
  exit_action.triggered.connect(self.close) #  Shut down app

  #  Define the menu bar and add 1 Options 
  menu_bar = self.menuBar()
  file_menu = menu_bar.addMenu('&File')
  file_menu.addAction(exit_action)

  #  Define the toolbar, add 1 Exit actions 
  toolbar = self.addToolBar('&Exit')
  toolbar.addAction(exit_action)

Sometimes the code prompt given by PyCharm is incomplete. It is said on the Internet that PyCharm can bring a good experience when used together with vim plug-in.

In the generated interface, the toolbar can be dragged freely and placed in 4 places.

The same code can be easily ported to PyQt4.

The interface drawn with designer

Ubuntu

After drawing the interface with designer, the ui file is converted into py code.


import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from ui_main_window import Ui_UAppMainWindow


class RustMainWindow(QMainWindow):
 """ Main interface class """

 def __init__(self):
  super(RustMainWindow, self).__init__()
  self.ma = Ui_UAppMainWindow() # designer Drawing interface 
  self.ma.setupUi(self)


if __name__ == "__main__":
 app = QApplication(sys.argv)
 main_window = RustMainWindow()
 main_window.show()
 sys.exit(app.exec_())

Overriding the __init__ initialization method requires calling the parent class method

PyQt4 Handwritten Window Code

Similar to the above function.


import sys
from PyQt4.QtGui import QMainWindow, QTextEdit, QAction, QIcon, QApplication


class AppMainWindow(QMainWindow):
 def __init__(self):
  super(AppMainWindow, self).__init__()
  self.init_ui()

 def init_ui(self):
  self.statusBar().showMessage('Main window is ready')
  self.setGeometry(500, 500, 450, 220)
  self.setMinimumSize(150, 120)
  self.setWindowTitle('MainWindow')

  text_edit = QTextEdit()
  self.setCentralWidget(text_edit)

  exit_action = QAction(QIcon('res/ic_s1.png'), 'Exit', self)
  exit_action.setShortcut('Ctrl+Q')
  exit_action.setStatusTip('Exit App')
  exit_action.triggered.connect(self.close)

  menu_bar = self.menuBar()
  file_menu = menu_bar.addMenu('&File')
  file_menu.addAction(exit_action)

  toolbar = self.addToolBar('&Exit')
  toolbar.addAction(exit_action)


if __name__ == '__main__':
 app = QApplication(sys.argv)
 window = AppMainWindow()
 window.show()
 sys.exit(app.exec_())

As you can see, the code for PyQt4 and 5 is basically generic. There are different ways to override __init__.

The above is the PyQt QMainWindow usage example details, more information about the use of PyQt QMainWindow please pay attention to other related articles on this site!


Related articles: