首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在pyqt5中使用另一个按钮激活按钮,我应该使用if语句吗?

在pyqt5中使用另一个按钮激活按钮,我应该使用if语句吗?
EN

Stack Overflow用户
提问于 2019-11-20 22:56:32
回答 1查看 70关注 0票数 0

事情是这样发展的.

我想要按下SD底部,这将激活四个测试点,并为它们分配以下IP地址,因此当我按下TP1时,它将采用TP1地址,依此类推。

我正试着用if函数来做,但它不起作用!有没有更好的方法呢?

代码:

代码语言:javascript
复制
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication,
                             QGridLayout, QLCDNumber)
from PyQt5 import QtCore, QtGui, QtWidgets
from QLed import QLed

class MainProg(QtWidgets.QMainWindow):

    def __init__(self):


        super(MainProg, self).__init__()


        self.setObjectName("MainWindow")

        self.setFixedSize(1366, 768)

        self.AASD = QtWidgets.QToolButton(self)
        self.AASD.setGeometry(QtCore.QRect(140, 70, 31, 32))
        self.AASD.clicked.connect(self.ASD)

        self.AAHD = QtWidgets.QToolButton(self)
        self.AAHD.setGeometry(QtCore.QRect(180, 70, 31, 32))
        #self.AASD.clicked.connect(self.AHD)


        self.AASD.setText("SD")
        self.AAHD.setText("HD")


        ##################################################3
        self.Testpunk1 = QtWidgets.QToolButton(self)
        self.Testpunk1.setGeometry(QtCore.QRect(150, 460, 31, 32))

        self.Testpunk2 = QtWidgets.QToolButton(self)
        self.Testpunk2.setGeometry(QtCore.QRect(150, 510, 31, 32))

        self.Testpunk3 = QtWidgets.QToolButton(self)
        self.Testpunk3.setGeometry(QtCore.QRect(150, 595, 31, 32))
        self.Testpunk4 = QtWidgets.QToolButton(self)
        self.Testpunk4.setGeometry(QtCore.QRect(150, 650, 31, 32))

        self.Testpunk5 = QtWidgets.QToolButton(self)
        self.Testpunk5.setGeometry(QtCore.QRect(710, 465, 31, 32))
        self.Testpunk6 = QtWidgets.QToolButton(self)
        self.Testpunk6.setGeometry(QtCore.QRect(710, 515, 31, 32))

        self.Testpunk7 = QtWidgets.QToolButton(self)
        self.Testpunk7.setGeometry(QtCore.QRect(710, 600, 31, 32))
        self.Testpunk8 = QtWidgets.QToolButton(self)
        self.Testpunk8.setGeometry(QtCore.QRect(710, 650, 31, 32))
        self.Testpunk1.setText("TP1")
        self.Testpunk2.setText("TP2")
        self.Testpunk3.setText("TP3")
        self.Testpunk4.setText("TP4")

        self.Testpunk5.setText("TP5")
        self.Testpunk6.setText("TP6")
        self.Testpunk7.setText("TP7")
        self.Testpunk8.setText("TP8")



        ###################the LEDs####################

        self.Tp1 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp1.setGeometry(QtCore.QRect(185, 465, 25, 25))
        self.Tp1.value = False

        self.Tp2 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp2.setGeometry(QtCore.QRect(185, 600, 25, 25))
        self.Tp2.value = False

        self.Tp3 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp3.setGeometry(QtCore.QRect(745, 470, 25, 25))
        self.Tp3.value = False

        self.Tp4 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp4.setGeometry(QtCore.QRect(745, 605, 25, 25))
        self.Tp4.value = False

    def TPLed(self):

        self.Tp1.value = True  # the LED ON code
        self.Tp3.value = True  # the LED ON code
        self.Tp2.value = True  # the LED ON code
        self.Tp4.value = True


    def ASD(self):


        self.TPLed()
        TP1 = "239.168.1.6:1112"
        TP2 = "239.168.1.7:1116"
        TP3 = "239.168.1.6:1132"
        TP4 = "239.168.1.6:1136"
        TP5 = "239.168.2.6:2113"
        TP6 = "239.168.2.7:2122"
        TP7 = "239.168.2.8:2132"
        TP8 = "239.168.2.9:2142"


        if  self.Testpunk1.click():
            myurl = TP1
            print("TP1 is playing")
            pass
        if self.Testpunk2.click():
            myurl = TP2
            print("TP2 is playing")
            pass
        if self.Testpunk3.click():
            myurl = TP3
            print("TP3 is playing")
            pass
        if self.Testpunk4.click():
            myurl = TP4
            print("TP4 is playing")
            pass
        if self.Testpunk5.click():
            myurl = TP5
            print("TP5 is playing")
            pass
        if self.Testpunk6.click():
            myurl = TP6
            print("TP6 is playing")
            pass
        if self.Testpunk7.click():
            myurl = TP7
            print("TP7 is playing")
            pass

        if self.Testpunk8.click():
            myurl = TP8
            print("TP8 is playing")
            pass

    def AADH(self):
        #the same with different IP addresses
        pass




if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    player = MainProg()
    player.show()
    sys.exit(app.exec_())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-22 02:37:39

我不太明白你到底想做什么,但答案来了。

代码语言:javascript
复制
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication,
                             QGridLayout, QLCDNumber)
from PyQt5 import QtCore, QtGui, QtWidgets
from QLed import QLed

class MainProg(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainProg, self).__init__()

        self.setObjectName("MainWindow")

        self.setFixedSize(1366, 768)

        self.AASD = QtWidgets.QToolButton(self)
        self.AASD.setGeometry(QtCore.QRect(140, 70, 31, 32))
        self.AASD.clicked.connect(self.ASD)

        self.AAHD = QtWidgets.QToolButton(self)
        self.AAHD.setGeometry(QtCore.QRect(180, 70, 31, 32))
        #self.AASD.clicked.connect(self.AHD)

        self.AASD.setText("SD")
        self.AAHD.setText("HD")

        ##################################################3
        self.Testpunk1 = QtWidgets.QToolButton(self)
        self.Testpunk1.setGeometry(QtCore.QRect(150, 460, 31, 32))

        self.Testpunk2 = QtWidgets.QToolButton(self)
        self.Testpunk2.setGeometry(QtCore.QRect(150, 510, 31, 32))

        self.Testpunk3 = QtWidgets.QToolButton(self)
        self.Testpunk3.setGeometry(QtCore.QRect(150, 595, 31, 32))
        self.Testpunk4 = QtWidgets.QToolButton(self)
        self.Testpunk4.setGeometry(QtCore.QRect(150, 650, 31, 32))

        self.Testpunk5 = QtWidgets.QToolButton(self)
        self.Testpunk5.setGeometry(QtCore.QRect(710, 465, 31, 32))
        self.Testpunk6 = QtWidgets.QToolButton(self)
        self.Testpunk6.setGeometry(QtCore.QRect(710, 515, 31, 32))

        self.Testpunk7 = QtWidgets.QToolButton(self)
        self.Testpunk7.setGeometry(QtCore.QRect(710, 600, 31, 32))
        self.Testpunk8 = QtWidgets.QToolButton(self)
        self.Testpunk8.setGeometry(QtCore.QRect(710, 650, 31, 32))
        self.Testpunk1.setText("TP1")
        self.Testpunk2.setText("TP2")
        self.Testpunk3.setText("TP3")
        self.Testpunk4.setText("TP4")

        self.Testpunk5.setText("TP5")
        self.Testpunk6.setText("TP6")
        self.Testpunk7.setText("TP7")
        self.Testpunk8.setText("TP8")

        # You do need to set objectName to access from self.sender
        self.Testpunk1.setObjectName("TP1")
        self.Testpunk2.setObjectName("TP2")
        self.Testpunk3.setObjectName("TP3")
        self.Testpunk4.setObjectName("TP4")

        self.Testpunk5.setObjectName("TP5")
        self.Testpunk6.setObjectName("TP6")
        self.Testpunk7.setObjectName("TP7")
        self.Testpunk8.setObjectName("TP8")

        self.Testpunk1.clicked.connect(self.tpButtonClicked)
        self.Testpunk2.clicked.connect(self.tpButtonClicked)
        self.Testpunk3.clicked.connect(self.tpButtonClicked)
        self.Testpunk4.clicked.connect(self.tpButtonClicked)
        self.Testpunk5.clicked.connect(self.tpButtonClicked)
        self.Testpunk6.clicked.connect(self.tpButtonClicked)
        self.Testpunk7.clicked.connect(self.tpButtonClicked)
        self.Testpunk8.clicked.connect(self.tpButtonClicked)

        # ##################the LEDs####################
        self.Tp1 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp1.setGeometry(QtCore.QRect(185, 465, 25, 25))
        self.Tp1.value = False

        self.Tp2 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp2.setGeometry(QtCore.QRect(185, 600, 25, 25))
        self.Tp2.value = False

        self.Tp3 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp3.setGeometry(QtCore.QRect(745, 470, 25, 25))
        self.Tp3.value = False

        self.Tp4 = QLed(self, onColour=QLed.Orange, shape=QLed.Circle)
        self.Tp4.setGeometry(QtCore.QRect(745, 605, 25, 25))
        self.Tp4.value = False

    def TPLed(self):
        self.Tp1.setValue(True)  # the LED ON code
        self.Tp3.setValue(True)  # the LED ON code
        self.Tp2.setValue(True)  # the LED ON code
        self.Tp4.setValue(True)

    def tpButtonClicked(self):
        TP1 = "239.168.1.6:1112"
        TP2 = "239.168.1.7:1116"
        TP3 = "239.168.1.6:1132"
        TP4 = "239.168.1.6:1136"
        TP5 = "239.168.2.6:2113"
        TP6 = "239.168.2.7:2122"
        TP7 = "239.168.2.8:2132"
        TP8 = "239.168.2.9:2142"

        if self.sender().objectName() == "TP1":
            myurl = TP1
            print("TP1 is playing")
            return
        if self.sender().objectName() == "TP2":
            myurl = TP2
            print("TP2 is playing")
            return
        if self.sender().objectName() == "TP3":
            myurl = TP3
            print("TP3 is playing")
            return
        if self.sender().objectName() == "TP4":
            myurl = TP4
            print("TP4 is playing")
            return
        if self.sender().objectName() == "TP5":
            myurl = TP5
            print("TP5 is playing")
            return
        if self.sender().objectName() == "TP6":
            myurl = TP6
            print("TP6 is playing")
            return
        if self.sender().objectName() == "TP7":
            myurl = TP7
            print("TP7 is playing")
            return
        if self.sender().objectName() == "TP8":
            myurl = TP8
            print("TP8 is playing")
            return

    def ASD(self):
        QtCore.QTimer.singleShot(500, self.TPLed) # Using timer as QLed uses it in its tests

    def AADH(self):
        #the same with different IP addresses
        pass


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    player = MainProg()
    player.show()
    sys.exit(app.exec_())

发生的情况是,您正在尝试检查从另一个按钮触发的插槽中的单击事件。你的条件语句永远不会是真的。

我只需将这些按钮连接到一个插槽上,由于您可以访问self.sender,因此我以这种方式设置了一个objectName,您可以稍后检查它。我希望它能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58957265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档