我有一个使用UDP流显示视频的Pyqt5图形用户界面
有一些按钮可以激活某些按钮(SD1或SD2或SD3将激活TP1和TP2),源1将激活TP5和TP6,依此类推
单击SD1后,TP1和TP2将处于活动状态,如果我们单击TP1,它将在右侧显示某个UDP流,并显示该程序的名称。
但当我尝试单击另一个SD或信号源时,我喜欢将按钮的旧状态重置为重置,将视频停止并重置所有按钮,因此我不会同时单击和启用许多按钮。
代码如下:
####################################### Important Libraries ##################################
import vlc
import os
import sys
import threading
from QLed import QLed
from functools import partial
from PyQt5.QtGui import QColor, QFont
from PyQt5.QtGui import QPainter, QPen
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QDir, Qt, QUrl, QSize, QPoint, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QMainWindow
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QGridLayout, QLCDNumber
class MainProg(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainProg, self).__init__(parent)
self.setObjectName("MainWindow")
self.setFixedSize(1280, 768)
self.setStyleSheet("")
self.setWindowTitle("video player ")
self.instance = vlc.Instance()
self.mediaplayer = self.instance.media_player_new()
################# The Test Points LEDs ######################
self.TPL1 = QLed(self, onColour=QLed.Orange, shape=QLed.Round)
self.TPL1.setGeometry(QtCore.QRect(190, 500, 35, 25))
self.TPL3 = QLed(self, onColour=QLed.Orange, shape=QLed.Round)
self.TPL3.setGeometry(QtCore.QRect(440, 525, 35, 25))
######################## Test Points Geo ##############################################
self.Testpunk1 = QtWidgets.QPushButton(self)
self.Testpunk1.setGeometry(QtCore.QRect(190, 460, 31, 31))
self.Testpunk1.setObjectName("TP1")
self.Testpunk2 = QtWidgets.QPushButton(self)
self.Testpunk2.setGeometry(QtCore.QRect(190, 530, 31, 31))
self.Testpunk2.setObjectName("TP2")
self.Testpunk5 = QtWidgets.QPushButton(self)
self.Testpunk5.setGeometry(QtCore.QRect(440, 490, 31, 31))
self.Testpunk5.setObjectName("TP5")
self.Testpunk6 = QtWidgets.QPushButton(self)
self.Testpunk6.setGeometry(QtCore.QRect(440, 550, 31, 31))
self.Testpunk6.setObjectName("TP6")
##########set text
self.Testpunk1.setText(("TP1"))
self.Testpunk2.setText(("TP2"))
self.Testpunk5.setText(("TP5"))
self.Testpunk6.setText(("TP6"))
############################ The Video and frame #################################
self.videoframe = QtWidgets.QFrame(self)
self.videoframe.setGeometry(QtCore.QRect(850, 40, 391, 291))
self.videoframe.setFrameShape(QtWidgets.QFrame.Box)
self.videoframe.setFrameShadow(QtWidgets.QFrame.Raised)
self.vboxlayout = QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.mediaplayer.set_hwnd(int(self.videoframe.winId()))
########################################################################################
########################## SD and HD buttons############################################
self.EspSD = QtWidgets.QToolButton(self, text="SD1", checkable=True)
self.EspSD.setGeometry(QtCore.QRect(140, 170, 31, 32))
self.EspSD.setObjectName("SD")
self.DeuSD = QtWidgets.QToolButton(self, text="SD2", checkable=True)
self.DeuSD.setGeometry(QtCore.QRect(140, 220, 31, 32))
self.DeuSD.setObjectName("DeSD")
self.DeupSD = QtWidgets.QToolButton(self, text="SD2", checkable=True)
self.DeupSD.setGeometry(QtCore.QRect(140, 270, 31, 32))
self.DeupSD.setObjectName("DpSD")
##########################source buttons############################################
self.IntS = QtWidgets.QToolButton(self, text="Source5", checkable=True)
self.IntS.setGeometry(QtCore.QRect(240, 170, 65, 32))
self.IntS.setObjectName("5")
self.AstD = QtWidgets.QToolButton(self, text="Source6", checkable=True)
self.AstD.setGeometry(QtCore.QRect(240, 220, 65, 32))
self.AstD.setObjectName("6")
self.IntSdep = QtWidgets.QToolButton(self, text="Source7", checkable=True)
self.IntSdep.setGeometry(QtCore.QRect(240, 270, 65, 32))
self.IntSdep.setObjectName("7")
################################### The Main Window Labels ############################
self.CurrentPro = QtWidgets.QLabel(self)
self.CurrentPro.setFont(QFont('Arial', 10))
self.CurrentPro.setGeometry(QtCore.QRect(580, 55, 140, 35))
self.CurrentPro.setText("Current Program :")
self.CurrentProL = QtWidgets.QLabel(self)
self.CurrentProL.setFont(QFont('Arial', 10))
self.CurrentProL.setGeometry(QtCore.QRect(690, 55, 155, 35))
#################################### The Main Programm Functions ######################
self.Testpunk1.clicked.connect(self.on_tp_clicked_Enc)
self.Testpunk2.clicked.connect(self.on_tp_clicked_Enc)
self.Testpunk5.clicked.connect(self.on_tp_clicked_Mux)
self.Testpunk6.clicked.connect(self.on_tp_clicked_Mux)
self.current_button = None
#self.process = QtCore.QProcess(self)
def on_tp_clicked_Enc(self):
self.TPL1.setValue(True)
if self.EspSD.isChecked():
urls_map = {
self.Testpunk1: "udp://@239.168.1.6:1113",
self.Testpunk2: "udp://@239.168.1.7:1117",
}
url = urls_map.get(self.sender(), "")
if url:
self.play(url)
self.CurrentProL.setText("Program4")
if self.DeuSD.isChecked():
urls_map = {
self.Testpunk1: "udp://@239.168.1.6:1123",
self.Testpunk2: "udp://@239.168.1.7:1127",
}
url = urls_map.get(self.sender(), "")
if url:
self.play(url)
#self.current_button = self.sender()
#self.current_button.setEnabled(False)
self.CurrentProL.setText("Program5")
if self.DeupSD.isChecked():
urls_map = {
self.Testpunk1: "udp://@239.168.1.6:1121",
self.Testpunk2: "udp://@239.168.1.7:1125",
}
url = urls_map.get(self.sender(), "")
if url:
self.play(url)
self.CurrentProL.setText("Program6")
@QtCore.pyqtSlot()
def on_tp_clicked_Mux(self):
self.TPL3.setValue(True) # the LED ON code
if self.IntS.isChecked():
urls_map = {
self.Testpunk5: "udp://@239.168.2.6:2113",
self.Testpunk6: "udp://@239.168.2.7:2123",
}
url = urls_map.get(self.sender(), "")
if url:
self.play(url)
self.CurrentProL.setText("Program3 Source3")
if self.AstD.isChecked():
urls_map = {
self.Testpunk5: "udp://@239.168.2.6:2117",
self.Testpunk6: "udp://@239.168.2.7:2127",
}
url = urls_map.get(self.sender(), "")
if url:
self.play(url)
self.CurrentProL.setText("Program4 Source4")
if self.IntSdep.isChecked():
urls_map = {
self.Testpunk5: "udp://@239.168.2.6:2113",
self.Testpunk6: "udp://@239.168.2.7:2123",
}
url = urls_map.get(self.sender(), "")
if url:
self.play(url)
self.CurrentProL.setText("Program4 Source5")
def play(self, url):
print('the url arrived')
self.media = self.instance.media_new(url)
self.media.get_mrl()
self.mediaplayer.set_media(self.media)
print('last step before video viewing')
self.mediaplayer.play()
#self.TPL1.setValue(False) # the LED ON code
#self.TPL2.setValue(False)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainProg()
w.show()
sys.exit(app.exec_())
############################## End of the Code #########################

发布于 2020-03-01 05:34:55
如果您希望一次只选中一个按钮,则可以使用QButtonGroup来管理每个按钮的状态。默认情况下,按钮组是独占的,因此您只需添加按钮即可。
def __init__(self, parent=None):
. . .
group = QtWidgets.QButtonGroup(self)
for btn in [self.EspSD, self.DeuSD, self.DeupSD, self.IntS, self.AstD, self.IntSdep]:
group.addButton(btn)https://stackoverflow.com/questions/60450537
复制相似问题