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