pyqt5圖形界面模版


pyqt5圖形界面模版,初學pyqt5,經常會用到的代碼片段。

 

# -*- coding: utf-8 -*-
#
# Form implementation generated from reading ui file 'FormUI.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# 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(300, 150)
        # 文本框
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(100, 20, 100, 28))  # x坐標,y坐標,寬,高
        self.lineEdit.setObjectName("lineEdit")
        # 按鈕
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(100, 60, 100, 28))
        self.pushButton.setObjectName("pushButton")
        # 標簽
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(100, 100, 100, 28))
        self.label.setObjectName("label")

        self.retranslateUi(Form)

    def retranslateUi(self, Form):  # 設置各組件的文本
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "這是一個測試窗口"))
        self.pushButton.setText(_translate("Form", "測試按鈕"))
        self.label.setText(_translate("Form", "原文字"))

# 以上是圖形界面代碼,不用手寫。可以用Qt Designer定制,然后用pyuic生成。
#
# ui轉py命令:  python -m PyQt5.uic.pyuic xxx.ui -o xxx_ui.py
#
##################################################################




# from PyQt5 import QtWidgets                       # 上面ui部分已經調用過了,這里就先注釋掉
# from myUi import Ui_Form                          # 界面類、邏輯類分離的話,myUi的名字就是ui文件名

class MyWindow(QtWidgets.QWidget,Ui_Form):          # 括號中的Ui_Form要跟ui.py文件里的class同名
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)                          # 生成界面
        QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create('Fusion'))   # 界面風格

        self.pushButton.clicked.connect(self.btn_clicked)   # 按鈕信號槽      ################ 按需修改

    def btn_clicked(self):    # 槽函數       ################ 按需修改
        input_words = self.lineEdit.text() 
        self.label.setText(input_words)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    myshow = MyWindow()
    myshow.show()
    sys.exit(app.exec_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM