I want to get the name of the click button when the Python click button event occurs.

Asked 2 years ago, Updated 2 years ago, 14 views

I want to get the name of the button to know which button was pressed when the button was pressed. Press button -> After passing button name to TextReadBT -> TextReadBT wants to check which button is pressed by if door. I don't know how to approach it even if I googled, so I'm posting a question here.

        self.CPUBt.clicked.connect(self.TextReadBt)
        self.MainBordBt.clicked.connect(self.TextReadBt)
        self.RamBt.clicked.connect(self.TextReadBt)
        self.GPUBt.clicked.connect(self.TextReadBt)
        self.SSDBt.clicked.connect(self.TextReadBt)
        self.HDDBt.clicked.connect(self.TextReadBt)
        self.CaseBt.clicked.connect(self.TextReadBt)
        self.PowerBt.clicked.connect(self.TextReadBt)
 def TextReadBt(self):


        ua = UserAgent()
        headers = {"User-Agent":ua.random}
        response = requests.get(self.CPUPlaintText.toPlainText(),headers = headers)
        html = response.text
        soup = BeautifulSoup(html,"html.parser")

        for lowest in soup.select('em[class=prc_c]'):
            print (lowest.text)
            self.CPUAddText.setPlainText(lowest.text)
            break

python

2022-09-22 13:40

2 Answers

I don't know why it stops. Please refer to the code below for the event code.

Please separate the ui from the actual logic. This means that you should not create a job logic in the event handler. This makes it easier to recycle and easier to test ui.

import sys
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class MyForm(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUI(parent)

    def setupUI(self, parent):
        self.vbox = QVBoxLayout()
        self.but1 = QPushButton('button1')
        self.but2 = QPushButton('button2')
        self.but1.clicked.connect(self.clicked_func)
        self.but2.clicked.connect(self.clicked_func)
        self.vbox.addWidget(self.but1)
        self.vbox.addWidget(self.but2)
        self.setLayout(self.vbox)

    @pyqtSlot()
    def clicked_func(self):
        widget = self.sender()
        t = widget.text()
        QMessageBox.information(self, t, t)

class MyMain(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        form = MyForm(self)
        self.setCentralWidget(form)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mywindow = MyMain()
    mywindow.show()
    app.exec_()


2022-09-22 13:40

You can get it from the self of the TextReadBt function.

self.You can obtain widgets called by sender().

def TextReadBt(self):
    widget = self.sender()
    button_text = widget.text()


2022-09-22 13:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.