📜  PyQt5 QSpinBox – 获取调用动作的对象

📅  最后修改于: 2022-05-13 01:55:39.814000             🧑  作者: Mango

PyQt5 QSpinBox – 获取调用动作的对象

在本文中,我们将了解如何获取调用动作的旋转框对象。假设当超过 1 个旋转框调用相同的操作(方法)时,我们不知道调用了哪个旋转框操作。为了获得调用动作的旋转框,我们必须跟踪旋转框发出的信号,因为在发出信号时调用动作。

为了做到这一点,我们将sender方法与旋转框对象一起使用。

下面是实现

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
        # method for widgets
    def UiComponents(self):
        # creating spin box
        self.spin = QSpinBox(self)
  
        # setting geometry to spin box
        self.spin.setGeometry(100, 100, 250, 40)
  
        # setting range to the spin box
        self.spin.setRange(1, 999999)
  
        # setting prefix to spin
        self.spin.setPrefix("PREFIX ")
  
        # setting suffix to spin
        self.spin.setSuffix(" SUFFIX")
  
        # adding action to the spin box
        self.spin.valueChanged.connect(self.do_something)
  
        # creating another spin box
        self.spin2 = QSpinBox(self)
  
        # setting geometry
        self.spin2.setGeometry(150, 50, 100, 40)
  
        # adding same action to it
        self.spin2.valueChanged.connect(self.do_something)
  
        # creating a label
        self.label = QLabel(self)
  
        # making label multi line
        self.label.setWordWrap(True)
  
        # setting label geometry
        self.label.setGeometry(100, 200, 250, 60)
  
  
    def do_something(self):
  
        # getting the sender
        sender = self.spin.sender()
  
        # setting text to the spin box
        self.label.setText(str(sender))
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :