python

PyQt5设计一个页面demo

2020-12-12  本文已影响0人  yichen_china

PyQt5手册1
PyQt5手册2

1.使用 qt designer 设计出了个窗口,
pyuic编译的命令是把ui转成py文件:

pyuic5 -o test2.py test.ui

2.test2.py内容大致如下格式:

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(590, 490, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(120, 170, 221, 31))
        self.textEdit.setObjectName("textEdit")
        self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit_2.setGeometry(QtCore.QRect(380, 170, 301, 31))
        self.textEdit_2.setObjectName("textEdit_2")
        self.textEdit_3 = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit_3.setGeometry(QtCore.QRect(120, 260, 571, 161))
        self.textEdit_3.setObjectName("textEdit_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        self.menuHeader = QtWidgets.QMenu(self.menubar)
        self.menuHeader.setObjectName("menuHeader")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuHeader.menuAction())

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(lambda : print("点击了按钮"))
        # self.pushButton.click()
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        MainWindow.setTabOrder(self.textEdit, self.textEdit_2)
        MainWindow.setTabOrder(self.textEdit_2, self.textEdit_3)
        MainWindow.setTabOrder(self.textEdit_3, self.pushButton)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "ok"))
        self.textEdit.setWhatsThis(_translate("MainWindow", "<html><head/><body><p>dd</p></body></html>"))
        self.menuHeader.setTitle(_translate("MainWindow", "header"))

3.再写一个类去继承 生成的类

import sys
from static.test2 import Ui_MainWindow
# from PyQt5.Qt import *
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QWidget


class Window(QMainWindow,Ui_MainWindow):
    # i = 3
    def __init__(self):
        super().__init__()
        print("初始化")
        self.setupUi(self)
        # print(self.i)
    def setup_ui(self):
        pass

    def binding(self):
        # self.textEdit.mouseDoubleClickEvent()
        pass

4.然后就可以在 这个子类去绑定事件

运行

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    # ui = Ui_MainWindow()
    # ui.setupUi(window)  # 传入一个 window 对象
    window.show()
    sys.exit(app.exec_())
上一篇下一篇

猜你喜欢

热点阅读