基本上,用戶所做的是創(chuàng)建了第二個類button,他用來創(chuàng)建一個button。例如,代碼如下: class Button(QPushButton): def __init__(self, parent=None): super(Button, self).__init__(parent) # other initializations... def enterEvent(self, QEvent): print("enter") pass def leaveEvent(self, QEvent): print("leave") pass 現(xiàn)在,為了用這個類創(chuàng)建一個按鈕,我們需要做的就是這樣調(diào)用這個類: self.buttn = Button(self) 這將使用上一個類創(chuàng)建一個按鈕。因此,整個代碼就是這樣的: from PyQt5.QtWidgets import QApplication, QWidget, QPushButtonimport PyQt5.QtGuifrom PyQt5.QtCore import QEventimport sysclass Button(QPushButton): def __init__(self, parent=None): super(Button, self).__init__(parent) # other initializations