PyQt5 series tutorials (II) use QtDesigner to design the UI _ PHP Tutorial

Source: Internet
Author: User
PyQt5 series tutorials (II) use QtDesigner to design the UI. PyQt5 series tutorials (II) design UI using QtDesigner hardware and software environment OSXEICapitanPython3.5.1PyQt5.5.1PyCharm5.0.1 preface the first blog. csdn. n PyQt5 series tutorials (II) design UI using QtDesigner
Software and hardware environment
  • OS X EI Capitan
  • Python 3.5.1
  • PyQt 5.5.1
  • PyCharm 5.0.1
Preface

The first ghost GUI program in the PyQt5 series shows you the charm of Qt development and the general process of Qt GUI development.

There are two methods to create a program UI, which are written using the UI creation tool and pure code, like Android and iOS in mobile development. in PyQt5, we also have two methods.

QtDesigner

QtDesigner is a tool used to create the UI of the Qt program. it is very simple to use. you can drag and drop the UI and click it to complete the complex interface design. you can also preview and view the UI at any time.

Among them, Area 1 is UI creation orientation, QtDesigner provides us with some common modules, which is very convenient; Area 2 is the UI control list; area 3 is the control property list; area 4 is the Action Editor editing List, area 5 is the editing column list of signals and slots, and Area 6 is the resource processing window.

The first PyQt5 program

If you have said so much, it is better to practice it.

This is the first interface of the PyQt5 project. when you click the button on the page, a prompt box is displayed, showing a string of text in the prompt box.

All right, set the target, and immediately start to implement it.

QtDesigner UI design

Create a UI file based on the Main Window module named firstPyQt5.ui. Find (or) the Push Button from the Widget Box, drag it to the workspace, adjust the position, enter the text on the Button, and adjust the font and size, these operations can be performed in the Property Editor. Clicking the button in the pop-up box will lead to a very important pair of concepts in Qt, namely signal and slot. I plan to open another blog post on this. Now you only need to know that slot is a function. if a signal is bound to a slot, the signal is triggered and the slot is executed.

So the question is, how can I bind signal and slot in QtDesigner? Open Edit-> Edit Signals/Slots in the menu bar, move the cursor to the button, and click drag. an Edit box is displayed.

Because it is a click, signal selects clicked (). If the slot function is not available yet, click Edit to create a new one, which is firstPyQt5_button_click ()

To demonstrate the usage of resource files, import two images here, one for the main window and the other for the Action in the Help menu.

Before using QtDesigner to reference a resource file, you must prepare a qrc file, which is similar to an xml file and used to specify the path of the resource file.

     
          
   
    qt.png
           
   
    penguin.jpg
       
  
  

Next, you can add the qrc file to the Resource Browser of QtDesigner so that the resources described in the qrc file can be used.

Convert firstPyQt5.ui to python code

It is very easy to implement through the command line tool pyuic5 provided by Qt

pyuic5 -o firstPyQt5.py firstPyQt5.ui 
Create a PyCharm project

Create a new python file main. py. the code is as follows:

#-*-Coding: UTF-8-*-_ author _ = 'djstava @ gmail.com 'import sysfrom PyQt5.QtWidgets import QApplication, QMainWindowfrom firstPyQt5 import * if _ name _ = '_ main _': '''main function ''' app = QApplication (sys. argv) mainWindow = QMainWindow () ui = Ui_mainWindow () ui. setupUi (mainWindow) mainWindow. show () sys.exit(app.exe c _())

Next, modify the firstPyQt5.py file to implement the slot function. because it was not implemented in QtDesigner before, let it pop up a message box.

Def firtPyQt5_button_click (self): QtWidgets. QMessageBox. information (self. pushButton, "title", "This is the first PyQt5 GUI program ")

Finally run the project

Generally, the ui file is saved as a separate file to facilitate updates.

Source code download

Http://download.csdn.net/detail/djstavav/9351205

Using QtDesigner to design the UI software and hardware environment OS X EI Capitan Python 3.5.1 PyQt 5.5.1 PyCharm 5.0.1 preface the first http://blog.csdn.n in the PyQt5 series tutorials...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.