1. 程式人生 > >pyQt5 使用qtdesigner生成程式碼後的程式碼分析 和中文幫助文件程式碼不同之處

pyQt5 使用qtdesigner生成程式碼後的程式碼分析 和中文幫助文件程式碼不同之處

1,生成程式碼後加入幾行事件處理機制,點選按鈕 出現hello

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form") #不懂

        Form.resize(400, 300)


        self.Btn_show = QtWidgets.QPushButton(Form)
        self.Btn_show.setGeometry(QtCore.QRect(11, 11, 99, 27))
        self.Btn_show.setObjectName("Btn_show")
        self.Btn_show.clicked.connect(self.buttonClicked)

        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(116, 11, 146, 27))
        self.lineEdit.setObjectName("lineEdit")

#不懂
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
#這個函式不懂
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.Btn_show.setText(_translate("Form", "PushButton"))

    def buttonClicked(self):
        self.lineEdit.setText("Hello")

main函式程式碼

from untitled   import Ui_Form
import sys
from PyQt5 import QtWidgets

from PyQt5.QtGui import QIcon

app = QtWidgets.QApplication(sys.argv)


widget = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(widget)
widget.setWindowIcon(QIcon('web.png'))  # 增加icon圖示
widget.show()
sys.exit(app.exec_())

執行結果: