PyQt5 simple routines and set PyQt5 in Pycharm

Source: Internet
Author: User

This article describes a simple routine written by Python using PYQT5 for interface programming.

Reference Blog http://blog.csdn.net/u013401853/article/details/54581512, a reference to the blog to write more detailed.

The following problems are easy to appear:

Resolve Pycharm cannot import package issues unresolved reference

Software Environment Preparation

PYQT5 can only be used in Python3.5 and above, and the installation method is described in the blog for the Python library installation. In addition, the QT development environment needs to be installed, using the Tools--qtcreator or designer. The main role of PYQT5 is to enable Python to invoke the UI interface designed to use QT design, so that the QT interface design is convenient and powerful advantages of Python combined.

Add: (optional, but recommended configuration, more convenient to use)

In order to facilitate the easier use of PyQt5 in Pycharm, PYQT5 are configured in Pycharm, including the designer of Qt, and PyQt5-only pyuic5 tools.

Configuration process See:

Design of QT interface for pyqt5+python3+pycharm development environment configuration

In the development of QT interface, the design interface can use QT Creator or designer, essentially the same, using designer in this article.

Create a new main window project and save it as Testpyqt5.ui (the widget project is set up in the reference blog)

Add a pushbutton to the interface named BTN and add a testedit named TextEdit


Add a slot function Btn_click () to Pushbutton, which is the signal and slot editor, as shown in the above illustration.

After entering the signal slot edit mode, the left button presses the pushbutton and then drags outward to a place to loosen, will appear in the following picture

The left side of the image above is the pushbutton signal named Btn, and the clicked () signal is selected, and the right side is the slot function bound to the signal.

Since the signal is temporarily not connected to any slot function, the Slot function box on the right is blank. Now you need to create a slot function of your own. Click Edit, will pop up an interface, click the slot corresponding to the box below, will generate a function, the name modified to Btn_click ()


Then confirm. In the returned interface, select the Btn_click () function that you just defined in the column of the slot function

Confirm and save. Then put the Testpyqt5.ui file in your own project directory.

qt ui file into Python file

PYQT5 provides a tool to convert the UI file to a Python file--pyuic5, under the Scripts folder in the Python installation directory, remember to add the path where PYUIC5 is located to the system environment variable.

Enter the project directory (Testpyqt5.ui is placed under the project directory in the previous step)

Input:

   PYUIC5 Testpyqt5.ui-o testpyqt5.py
A file named testpyqt5.py is generated in the current directory.

If you have previously configured PYQT5 from Pycharm, you can directly mail testpyqt5.ui,external Tools->pyuic and automatically generate a. py file. using QT design interface in Python

The code is as follows:

From TESTPYQT5 import Ui_mainwindow  #导入uitestPyQt5. Ui converts to classes in uitestpyqt5.py from

PyQt5 import qtwidgets
Import SYS

class Mywindow (Qtwidgets.qmainwindow,ui_mainwindow):  
#建立的是Main window project, so the import is Qmainwindow
#参考博客中建立的是Widget项目, so where to import is Qwidget
    def __init__ (self):
        super (Mywindow, self). __init__ ()
        Self.setupui (self)

    def Btn_click (self):     #定义槽函数btn_click (), can also be understood as a slot function in an overloaded class Ui_mainwindow Btn_click ()
        Self.textEdit.setText ("hi,pyqt5~")

app = Qtwidgets.qapplication (sys.argv)
window = Mywindow ()
Window.show ()
sys.exit (App.exec_ ())

The effect of the following figure:


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.