QAbstractbutton button class is an abstract base class can not only be used to implement the functions possessed by his sub-category
A simple example
from PyQt5.Qt Import * Import SYS App = the QApplication (the sys.argv) window = the QWidget () window.setWindowTitle ( ' QAbstractButton ' ) window.resize ( 500, 500 ) class BTN (QAbstractButton): DEF the paintEvent (Self, EVT) : # 1. create a painter, painting somewhere painter = QPainter (Self) # 2 to a painter pen # 2.1 Creating a pen pEN = QPen (QColor (111, 200, 20), 5 ) # 2.2 to pen a painter painter.setPen (pen) # 3 the painter painter.drawText (25, 40 , self.text ()) painter.drawEllipse (0, 0, 100, 100 ) BTN = BTN (window) btn.setText ( ' XXX ' ) btn.resize ( 100 , 100 ) btn.pressed.connect ( the lambda : Print ( " clicked this button " )) window.show () sys.exit (app.exec_ ())
Example shows:
- Text Settings
- Picture Settings
- Shortcut keys
- Auto Repeat
- Button State
- Exclusive
- Set the effective area
from PyQt5.Qt Import * Import SYS App = the QApplication (the sys.argv) window = the QWidget () window.setWindowTitle ( ' button function test - abstract class ' ) window.move ( 200 is, 200 is ) window.resize ( 500, 500 ) # window.setFixedSize (500, 500) sets a fixed size # # ************* *************** text operation begins # BTN = the QPushButton (window) # btn.setText ( '. 1') is provided Tip # text # DEF PLUS_ONEday (): # Print ( '+. 1') # NUM = int (btn.text ()) +. 1 # Btn.setText (STR (NUM)) # # btn.pressed.connect (PLUS_ONEday) # ************* text manipulation ************** * end # ************* icon to start operating *************** # btn = QPushButton (window) # icon = QIcon ( 'dp.png ') Add image # # # btn.setIcon (icon) picture # button; a manner # btn.setIcon (QIcon (' dp.png ')) # # set button images; Second way # # size = QSize (50, 50) # # btn.setIconSize (size) # set the image size; a way # btn.setIconSize (QSize (50, 50)) # set the image size; Second way # # Print (btn.icon ()) # get the icon # print (btn.iconSize ()) # get the icon size #Icon Action *************** ************* end # ************* set shortcuts *************** start # btn = QPushButton (window) # btn.setText ( 'button') # btn.pressed.connect (the lambda: Print ( 'button is clicked') ) # btn.setShortcut ( 'a') to set the shortcut keys # # btn.setText ( 'a & B') have & # automatically set the shortcut keys # ************* * set shortcut end ************** # ************* *************** start automatically repeat # auto-repeat means hold the button is kept pressed, the function will always perform slot # BTN = the QPushButton (window) # btn.setText ( 'button') # btn.setAutoRepeat (True) set automatic repeat # # btn.setAutoRepeatDelay (2000) # set automatic repeat detection interval, in milliseconds #btn.setAutoRepeatInterval (1000) # Set delay time detection, ms # Print (btn.autoRepeat ()) # Print (btn.autoRepeatDelay ()) # Print (btn.autoRepeatInterval ()) # DEF CaO (): # Print (. 1 ) # btn.clicked.connect (CaO) # ************* *************** end automatic repeat # ******* ****** *************** start button status # push_button the QPushButton = (window) # push_button.setText ( 'which is the QPushButton') # push_button.move (100, 100 ) # # radio_button, QRadioButton = (window) # radio_button.setText ( 'which is a Radio') #radio_button.move (100, 150) # # CheckBox = QCheckBoxes (window) # checkbox.setText ( 'which is CheckBox') # checkbox.move (100, 200 is) # push_button.setStyleSheet ( 'the QPushButton: Color-pressed {background: Red;} ') # # # set #s pressed # # push_button.setDown (True) # # radio_button.setDown (True) # # checkbox.setDown (False) # # Print (push_button.isDown ()) # # Print (radio_button.isDown ()) # # Print (checkbox.isDown ()) # # if # # set button can be selected # # push_button.setCheckable (True) #Print # (push_button.isCheckable ()) # # push_button.setEnabled (False) # button does not set # ************* button states ************ *** end # ************* *************** exclusive setting start # # in the same exclusive control # for i in the Range ( 0,. 3): # BTN = QCheckBoxes (window) # btn.setText ( 'BTN' + STR (I)) # btn.move (I * 50, 50 * I) # # btn.setAutoExclusive (True) # ** *********** *************** end exclusive setting # ************* **** button to simulate a click *********** start # BTN = the QPushButton (window) # btn.setText ( 'this is the button') #btn.move (200, 200) # btn.pressed.connect (the lambda: Print ( "clicked this button")) # btn.animateClick (2000) # set to simulate a click # *********** ** click the button analog *************** end # ************* ************ set the effective area *** start # by rewriting hitButton (QPoint) method, a valid return Ture; invalid return False # sets a circular region # class BTN (the QPushButton): # DEF hitButton (Self, Point): # # by a given point coordinates, calculating the center distance # yuanxin_x self.width = () / 2 # yuanxin_y self.height = () / 2 # # hit_x point.x = () # hit_y point.y = () # # # # ((X1 - X2) squared + (y1 - y2) squared) open squares # Import Math # Distance = Math.sqrt (Math.pow (hit_x - yuanxin_x, 2) + Math.pow (hit_y - yuanxin_y, 2) ) # IF distance <self.width () / 2: # return True # # # If the distance <radius True # return # False # return False # # # set painter # DEF the paintEvent (Self, EVT): # Super (). the paintEvent (EVT) # Painter = QPainter (Self) # painter.setPen (QPen (a QColor (100, 150, 200 is),. 6)) # # Painter.drawEllipse (self.rect ()) # # btn btn = (window) # btn.move (100, 100) # btn.setText ( "click") # btn.resize (200, 200) # btn.clicked .connect (lambda value: print ( "button to select the state has changed", value)) # ************* ************* set the effective area ** end window.show () sys.exit (app.exec_ ())