程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[interface] learning pyqt5 in Python

編輯:Python

PyQt5 Learning from

Reference link :https://blog.csdn.net/AzureMouse/article/details/90338961

install PyQt5

  1. Use pip download PyQt5
    pip install PyQt5
    pip install pyqt5-tools
    
  2. win+s, Search for designer
  3. Verify that the installation was successful
     The input terminal pyuic5
    Output “Error: one input ui-file must be specified”, Successful installation
    

PyQt Based on learning
1.main window Create main window
2.Widget Box Free drag components
3. Based on learning

  1. To add text Label

  2. Add a button PushButton

  3. Modify the window title windowTitle

  4. preview Form > Preview / Preview in

  5. preservation

  6. Generate python Code

     Use cmd Cut the directory to D Disk and execute the following command . Please send the following command to name Replace with file name
    1.cd /d D:\AI\PyMyWork\UI
    2.pyuic5 -o name.py name.ui
    
  7. function Python Code

     You need to re create a main.py The program is run by .ui Converted .py file
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
    import gui_file_name
    if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = gui_file_name.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    

    Function is introduced
    1. The use of buttons PushButton

    1. Get button's ID
      ObjectName:PushButton

    2. Set trigger

       Directly in “main.py” in “MainWindow.show()” Add the following line of code after
      

      ui.pushButton.clicked.connect(click_success)

      # pushButton It is the button just obtained id
      # clicked It's a signal , Because it's clicking , So we use clicked
      # click_success It corresponds to the slot to be called , Note that the function here is not written as click_success()
      
    3. Set function
      stay main.py Define a click_success, Used to trigger buttons

    4. function
      The console will display the results

  8. Generate executable files (.exe)
    pip install pyinstaller
    Open... After installation CMD then cd To the folder where the program is located , Input
    pyinstaller -F -w main.py

    # Find... In the folder where the code is located dist Folder , In this folder exe Software , Double click to open it .
    # In command -w It means : Direct release exe Application with command line debugging window , Add... To the instruction -w Commands can be masked
    # In command -F It means : Use -F Instructions can package an application into a separate exe file , Otherwise it's one with all kinds of dll And folders that depend on files
    

4. Case study 1:GUI Exchange rate converter

  1. Design ui
  2. The ginseng
    One is to use lambda, One is to use functool.partial
    partial The usage of is as follows :
    partial(function, arg1, arg2, ......)
    To be in the program (main.py) Add the following line to the head of the
    from functools import partial
    The line of code that triggers the button is modified as shown below :
    ui.pushButton.clicked.connect(partial(convert, ui))
  3. To write convert function
    def convert(ui):
    input = ui.lineEdit.text()
    result = float(input) * 6.7
    ui.lineEdit_2.setText(str(result))
    

5. Related learning Links

  1. https://www.jianshu.com/p/4993f37b43e6
  2. https://zhuanlan.zhihu.com/p/95624918
  3. https://www.cnblogs.com/wojianxin/p/12629085.html

6. Use it directly pycharm convert to python Program
https://www.py.cn/jishu/gaoji/18503.html


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved