PyQt5
1. 通过UIC转换成python代码后需在文件中直接添加即可运行。
方便使用代码补全 但每次改动ui会生成在原文件中,不小心会发生覆盖,每次都需要将之前的代码重新补充到新生成的文件中。
from PyQt5 import QtCore, QtGui, QtWidgets
if __name__ == "__main__" :
import sys
my_app = QtWidgets. QApplication( sys. argv)
MainWindow = QtWidgets. QMainWindow( )
ui = Ui_Form( )
ui. setupUi( MainWindow)
MainWindow. show( )
sys. exit( my_app. exec_( ) )
2. 不转换成python代码,直接加载ui文件
代码无法自动补全 每次对ui的修改直接保存即可,不需要转换文件,非常方便
from PyQt5 import uic
from PyQt5. QtWidgets import QApplication
class Stats :
def __init__ ( self) :
self. ui = uic. loadUi( "main.ui" )
app = QApplication( [ ] )
stats = Stats( )
stats. ui. show( )
app. exec_( )
3. 转化后进行类的继承
代码可以自动补全 每次需转换一下文件,但不需要对生成的文件修改 推荐
from PyQt5. QtWidgets import QApplication, QMainWindow
from XXX( 转化后的文件) import Ui_Form
class Stats ( QMainWindow) :
def __init__ ( self) :
super ( ) . __init__( )
self. ui = Ui_Form( )
self. ui. setupUi( self)
app = QApplication( [ ] )
mainw = Stats( )
mainw. show( )
app. exec_( )
Pyside2
不转换成python代码
from PySide2. QtWidgets import QApplication, QMessageBox
from PySide2. QtUiTools import QUiLoader
class Stats :
def __init__ ( self) :
self. ui = QUiLoader( ) . load( 'main.ui' )
显示图片
选择Label控件,并打对勾,可以进行尺寸自动缩放
self. ui. video_label. setPixmap( QtGui. QPixmap( "test.jpg" ) )
显示视频
import cv2
import threading
from PyQt5. QtGui import QImage, QPixmap
def __init__ ( self) :
self. ui. Open. clicked. connect( self. video_show)
self. ui. Close. clicked. connect( self. close)
self. stopEvent = threading. Event( )
self. stopEvent. clear( )
def video_show ( self) :
self. cap = cv2. VideoCapture( "test.mp4" )
self. frameRate = self. cap. get( cv2. CAP_PROP_FPS)
th = threading. Thread( target= self. Display)
th. start( )
def Close ( self) :
self. stopEvent. set ( )
def Display ( self) :
self. ui. Open. setEnabled( False )
self. ui. Close. setEnabled( True )
while self. cap. isOpened( ) :
success, frame = self. cap. read( )
frame = cv2. cvtColor( frame, cv2. COLOR_RGB2BGR)
img = QImage( frame. data, frame. shape[ 1 ] , frame. shape[ 0 ] , QImage. Format_RGB888)
self. ui. video_label. setPixmap( QPixmap. fromImage( img) )
cv2. waitKey( int ( 1000 / self. frameRate) )
if True == self. stopEvent. is_set( ) :
self. stopEvent. clear( )
self. ui. video_label. clear( )
self. ui. Close. setEnabled( False )
self. ui. Open. setEnabled( True )
break