In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Pyqt5 in python how to use Qlabel tags for video playback", the content is detailed, the steps are clear, the details are handled properly, I hope that this article "Pyqt5 in python how to use Qlabel tags for video playback" article can help you solve your doubts, following the editor's ideas slowly in-depth, let's learn new knowledge.
I. brief introduction
QLabel is a label class in the interface, inherited from the QFrame class, provides the display of text and images, and is a display control.
QLabel objects can display non-editable text or pictures, place a GIF animation, and can be used as prompts to mark as other controls.
Plain text, links, or rich text can also be displayed on tags.
Basic usage 2.1 QLabel control
SetAlignment (): aligns text with a fixed value, with the following alignment:
Qt.AlignLeft (horizontal left alignment), Qt.AlignRight (horizontal right alignment), Qt.AlignCenter (horizontal alignment), Qt.AlignJustify (horizontal alignment of both ends of the spacing), Qt.AlignTop (vertical top alignment), Qt.AlignBottom (vertical bottom alignment), Qt.AlignVCenter (vertical center alignment)
SetIndent (): sets text indentation
SetPixmap (): sets QLabel to a Pixmap image
Text (): get the text content of QLabel
SetText (): sets the text content of QLabel
SelectedText (): returns the selected character
SetBuddy (): set up a partnership
SetWordWrap (): sets whether line wrapping is allowed
2.2 signals (events) commonly used in QLabel
1.linkHovered: when the mouse pointer slides over the hyperlink embedded in the tag, it needs to be bound to this signal with a slot function.
2.linkActivated: when you click the hyperlink embedded in the tab and want to open the hyperlink in a new window, the setOpenExternalLinks property must be set to true
3. QLabel plays videos
The key point of using QLabel to play video files is * timer QTimer
Timers are used when the time needs to be displayed in the program or when an operation needs to be performed periodically in the program.
3.1 QTimer
Import the QTimer module:
From PyQt5.QtCore import QTimer
Initialize:
Self.timer_camera = QTimer ()
Time and start:
Self.timer_camera.start (1000) # 1000ms = = 1sself.timer_camera.timeout.connect (self.openFrame) # connection slot function openFrame
Note: when the parent of a QTimer is destroyed, it is also automatically destroyed.
3.2 Code
UI interface:
Python program:
From PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.uic import loadUiTypeimport cv2import sysvedio_ui, _ = loadUiType ('. / UI/vedio.ui') class VedioGui (QMainWindow Vedio_ui): # define construction method def _ _ init__ (self): QMainWindow.__init__ (self) self.setupUi (self) self.timer_camera = QTimer () self.handle_buttons () self.open_vedio () # all Button messages communicate with slots def handle_buttons (self): self.btn_Start .clicked.connect (self.Btn_Start) self.btn_Stop.clicked.connect (self.Btn_Stop) def Btn_Start (self): # timer start From time to time Read a frame of self.timer_camera.start (100th) self.timer_camera.timeout.connect (self.OpenFrame) def Btn_Stop (self): # self.cap.release () self.timer_camera.stop () def open_vedio (self): "" Select a video file "# here take mp4 and avi video playback as an example, openfile_name = QFileDialog.getOpenFileName (self) 'chose files','' 'Image files (* .mp4 * .avi)') # Open the file selection box and select the file self.file_name = openfile_name [0] # get the picture name # modify the file suffix suffix = self.file_name.split ("/") [- 1] [self.file_name.split ("/") [- 1] .index (" . ") + 1:] # print (self.file_name Suffix) if self.file_name =': pass elif suffix = = "mp4" or suffix = = "avi": self.cap = cv2.VideoCapture (self.file_name) def OpenFrame (self): ret, image = self.cap.read () if ret: if len (image.shape) = = 3: image = cv2.cvtColor (image) Cv2.COLOR_BGR2RGB) vedio_img = QImage (image.data, image.shape [1], image.shape [0], QImage.Format_RGB888) elif len (image.shape) = = 1: vedio_img = QImage (image.data, image.shape [1], image.shape [0], QImage.Format_Indexed8) else: vedio_img = QImage (image.data, image.shape [1]) Image.shape [0], QImage.Format_RGB888) self.vedio_label.setPixmap (QPixmap (vedio_img)) self.vedio_label.setScaledContents (True) # Adaptive window else: self.cap.release () self.timer_camera.stop () # Interface shutdown event Ask the user if they want to close def closeEvent (self, event): reply = QMessageBox.question (self, 'exit', "do you want to exit the interface?" , QMessageBox.Yes | QMessageBox.No QMessageBox.No) if reply = = QMessageBox.Yes: self.close () event.accept () else: event.ignore () if _ _ name__ = = "_ _ main__": app = QApplication (sys.argv) window = VedioGui () window.show () sys.exit (app.exec_ ())
Successful video playback shows:
Note: there is no sound during video playback.
After reading this, the article "how Pyqt5 in python uses Qlabel tags for video playback" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un