基於Python+Pyqt5開發一個應用程序

介紹你的那個她/他

1. UI —MainWindow設計界面及代碼

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

# Form implementation generated from reading ui file '/media/sf_data/onemore_tjj/Again_tjj/MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# 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(788, 634)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(40, 270, 691, 321))
        self.groupBox_2.setObjectName("groupBox_2")
        self.txtResume = QtWidgets.QTextEdit(self.groupBox_2)
        self.txtResume.setGeometry(QtCore.QRect(10, 30, 671, 281))
        self.txtResume.setObjectName("txtResume")
        self.butGenerate = QtWidgets.QPushButton(self.centralwidget)
        self.butGenerate.setGeometry(QtCore.QRect(390, 230, 101, 51))
        self.butGenerate.setObjectName("butGenerate")
        self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_3.setGeometry(QtCore.QRect(500, 10, 231, 271))
        self.groupBox_3.setObjectName("groupBox_3")
        self.label_6 = QtWidgets.QLabel(self.groupBox_3)
        self.label_6.setGeometry(QtCore.QRect(6, 30, 221, 241))
        self.label_6.setMinimumSize(QtCore.QSize(221, 0))
        self.label_6.setMaximumSize(QtCore.QSize(221, 16777215))
        self.label_6.setText("")
        self.label_6.setPixmap(QtGui.QPixmap(":/tjj.jpg"))
        self.label_6.setObjectName("label_6")
        self.label_6.setScaledContents(True)    #圖片自適應
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(30, 20, 231, 201))
        self.groupBox.setObjectName("groupBox")
        self.txtName = QtWidgets.QLineEdit(self.groupBox)
        self.txtName.setGeometry(QtCore.QRect(50, 30, 151, 25))
        self.txtName.setObjectName("txtName")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(10, 32, 67, 17))
        self.label.setObjectName("label")
        self.txtRelationship = QtWidgets.QLineEdit(self.groupBox)
        self.txtRelationship.setGeometry(QtCore.QRect(50, 60, 151, 25))
        self.txtRelationship.setObjectName("txtRelationship")
        self.label_2 = QtWidgets.QLabel(self.groupBox)
        self.label_2.setGeometry(QtCore.QRect(6, 90, 67, 17))
        self.label_2.setObjectName("label_2")
        self.txtHobby = QtWidgets.QLineEdit(self.groupBox)
        self.txtHobby.setGeometry(QtCore.QRect(48, 120, 151, 25))
        self.txtHobby.setObjectName("txtHobby")
        self.txtCharacter = QtWidgets.QLineEdit(self.groupBox)
        self.txtCharacter.setGeometry(QtCore.QRect(48, 150, 151, 25))
        self.txtCharacter.setObjectName("txtCharacter")
        self.label_4 = QtWidgets.QLabel(self.groupBox)
        self.label_4.setGeometry(QtCore.QRect(6, 122, 67, 17))
        self.label_4.setObjectName("label_4")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(6, 60, 67, 17))
        self.label_3.setObjectName("label_3")
        self.label_5 = QtWidgets.QLabel(self.groupBox)
        self.label_5.setGeometry(QtCore.QRect(6, 153, 67, 17))
        self.label_5.setObjectName("label_5")
        self.txtSpecialty = QtWidgets.QLineEdit(self.groupBox)
        self.txtSpecialty.setGeometry(QtCore.QRect(49, 90, 151, 25))
        self.txtSpecialty.setObjectName("txtSpecialty")
        self.groupBox_5 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_5.setGeometry(QtCore.QRect(270, 20, 221, 201))
        self.groupBox_5.setObjectName("groupBox_5")
        self.label_7 = QtWidgets.QLabel(self.groupBox_5)
        self.label_7.setGeometry(QtCore.QRect(10, 40, 67, 17))
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(self.groupBox_5)
        self.label_8.setGeometry(QtCore.QRect(10, 70, 67, 17))
        self.label_8.setObjectName("label_8")
        self.label_9 = QtWidgets.QLabel(self.groupBox_5)
        self.label_9.setGeometry(QtCore.QRect(10, 100, 67, 17))
        self.label_9.setObjectName("label_9")
        self.label_10 = QtWidgets.QLabel(self.groupBox_5)
        self.label_10.setGeometry(QtCore.QRect(10, 130, 67, 17))
        self.label_10.setObjectName("label_10")
        self.label_11 = QtWidgets.QLabel(self.groupBox_5)
        self.label_11.setGeometry(QtCore.QRect(10, 160, 67, 17))
        self.label_11.setObjectName("label_11")
        self.txtHair = QtWidgets.QLineEdit(self.groupBox_5)
        self.txtHair.setGeometry(QtCore.QRect(60, 40, 113, 25))
        self.txtHair.setObjectName("txtHair")
        self.txtHeight = QtWidgets.QLineEdit(self.groupBox_5)
        self.txtHeight.setGeometry(QtCore.QRect(60, 70, 113, 25))
        self.txtHeight.setObjectName("txtHeight")
        self.txtWeight = QtWidgets.QLineEdit(self.groupBox_5)
        self.txtWeight.setGeometry(QtCore.QRect(60, 100, 113, 25))
        self.txtWeight.setObjectName("txtWeight")
        self.txtOffice = QtWidgets.QLineEdit(self.groupBox_5)
        self.txtOffice.setGeometry(QtCore.QRect(60, 130, 113, 25))
        self.txtOffice.setObjectName("txtOffice")
        self.txtAddress = QtWidgets.QLineEdit(self.groupBox_5)
        self.txtAddress.setGeometry(QtCore.QRect(60, 160, 113, 25))
        self.txtAddress.setObjectName("txtAddress")
        self.label_14 = QtWidgets.QLabel(self.groupBox_5)
        self.label_14.setGeometry(QtCore.QRect(175, 73, 67, 17))
        self.label_14.setObjectName("label_14")
        self.label_15 = QtWidgets.QLabel(self.groupBox_5)
        self.label_15.setGeometry(QtCore.QRect(175, 101, 67, 17))
        self.label_15.setObjectName("label_15")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.butGenerate.clicked.connect(MainWindow.Generate)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "介紹你青春裡的那個她"))
        self.groupBox_2.setTitle(_translate("MainWindow", "簡介:"))
        self.butGenerate.setText(_translate("MainWindow", "生成"))
        self.groupBox_3.setTitle(_translate("MainWindow", "照片"))
        self.groupBox.setTitle(_translate("MainWindow", "我的朋友:"))
        self.label.setText(_translate("MainWindow", "姓名:"))
        self.label_2.setText(_translate("MainWindow", "特長:"))
        self.label_4.setText(_translate("MainWindow", "愛好:"))
        self.label_3.setText(_translate("MainWindow", "關系:"))
        self.label_5.setText(_translate("MainWindow", "性格:"))
        self.groupBox_5.setTitle(_translate("MainWindow", "簡介:"))
        self.label_7.setText(_translate("MainWindow", "發型:"))
        self.label_8.setText(_translate("MainWindow", "身高:"))
        self.label_9.setText(_translate("MainWindow", "體重:"))
        self.label_10.setText(_translate("MainWindow", "職務:"))
        self.label_11.setText(_translate("MainWindow", "住址:"))
        self.label_14.setText(_translate("MainWindow", "m"))
        self.label_15.setText(_translate("MainWindow", "kg"))
import picture_rc

2. 應用程序代碼

# 系統包
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# 項目包
from Ui_MainWindow import Ui_MainWindow
class Cjianjie:
    hair = ""
    height = ""
    weight = ""
    office = ""
    address = ""
    bmi=0.0
    def OutputHair(self):
        strOutput = "她有一頭烏黑亮麗的黑" + self.hair
        return strOutput
    def OutputHeight(self):
        strOutput = "她長的不算很高,"+ self.height + "米左右"
        return strOutput
    def OutputWeight(self):
        strOutput = "她身材不胖,才" + self.weight + "公斤"
        return strOutput

    def OutputOffice(self):
        strOutput = "她在班裡是" + self.office + "也是因為她才記得瞭那首詩,曾經滄海難為水,除去巫山不是雲"
        return strOutput
    def OutputAddress(self):
        strOutput = "她傢住在"  + self.address + "K41旁的我和她不是順路,隻是在意每一次的路過"
        return strOutput
    def OutputResume(self):
        strOutput = "    " + self.OutputHair() + ","
        strOutput = strOutput + self.OutputHeight() + ","
        strOutput = strOutput + self.OutputWeight() + ","
        strOutput = strOutput + self.OutputOffice() + "。"
        strOutput = strOutput + self.OutputAddress() + "。"
        return strOutput
class CFriend(Cjianjie):
    name = ""
    restionship = ""
    speciatly = ""
    hobby = ""
    character = ""
    def OutputName(self):
        strOutput = "她就是笑容值得等待一千萬年的" + self.name
        return strOutput
    def OutputRestionship(self):
        strOutput = "她是我的"+ self.restionship + ";高中兩年同窗,忘不瞭早自習一起被趕出去補作業,忘不瞭背不過英語去後排罰站,忘不瞭她曾謝絕瞭我的巧克力" 
        return strOutput
    def OutputSpecialty(self):
        strOutput = "她擅長" + self.speciatly + ",她那明亮的眼睛中,蕩漾這一池清波,清波上蕩漾著聰明與智慧;簡單純粹,風華絕代"
        return strOutput
    def OutputHobby(self):
        strOutput = "她愛" + self.hobby + ",老看她發逛街的朋友圈,差點忘瞭她眼裡也有山河、星辰、大海和天空,哈哈,她和我都不曾缺失發現美的眼睛" 
        return strOutput
    def OutputCharacter(self):
        strOutput = "她性格" + self.character + ",尤記開學發書,傳給你的是書,她卻還給我一句謝謝和一個微笑,也曾開玩笑問她以後要不要嫁入豪門,她卻說:“靠自己!”" 
        return strOutput

    def OutputResume(self):
        strOutput= Cjianjie.OutputResume(self)
        strOutput = strOutput+ self.OutputName() + ","
        strOutput = strOutput + self.OutputRestionship() + "。"
        strOutput = strOutput + self.OutputSpecialty() + "。"
        strOutput = strOutput + self.OutputHobby() + "。"
        strOutput = strOutput + self.OutputCharacter() + "。"
        return strOutput

class CMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):
        super(CMainWindow, self).__init__(parent)
        self.setupUi(self)

        # 主窗體設置
        self.show()


    def Generate(self):
        f = CFriend()
        f.hair = self.txtHair.text()
        f.height = self.txtHeight.text()
        f.weight = self.txtWeight.text()
        f.office = self.txtOffice.text()
        f.address = self.txtAddress.text()
        f.name = self.txtName.text()
        f.restionship = self.txtRelationship.text()
        f.speciatly= self.txtSpecialty.text()
        f.character= self.txtCharacter.text()
        f.hobby = self.txtHobby.text()
       
        
        self.txtResume.setText(f.OutputResume())


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = CMainWindow()
    sys.exit(app.exec_())

3. 視頻播放程序圖片及代碼

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('video1.avi')
while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # show gray picture
    #gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    #cv.imshow('frame', gray)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('q'):
        break
cap.release()
cv.destroyAllWindows()

4. 呈現效果

到此這篇關於基於Python+Pyqt5開發一個應用程序的文章就介紹到這瞭,更多相關Python+Pyqt5應用程序內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: