【问题标题】:Select button and turn on others and reset other buttons in pyqt5在pyqt5中选择按钮并打开其他按钮并重置其他按钮
【发布时间】:2020-03-01 05:34:54
【问题描述】:

我有一个使用 UDP 流显示视频的 Pyqt5 GUI

有一些按钮可以激活某些按钮 as(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  #########################

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    如果您希望一次只检查一个按钮,您可以使用 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)
    

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 2021-09-01
      • 1970-01-01
      • 2017-11-15
      • 2014-01-06
      • 2012-06-02
      • 2021-08-23
      • 2015-02-27
      • 1970-01-01
      相关资源
      最近更新 更多