📜  PyQt5 |如何制作胶囊形按钮?

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

PyQt5 |如何制作胶囊形按钮?

胶囊形状基本上是由一个矩形和两个连接到两端的半圆形组成的形状,在本教程中我们将看到如何创建这些形状的按钮。

下面是普通按钮与胶囊形按钮。

注意:如果按钮不是矩形,它将变成圆形。

代码 :

# importing libraries
from PyQt5.QtWidgets import * 
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 a push button
        button = QPushButton("CLICK", self)
  
        # setting geometry of button
        # rectangular shape i.e width > height
        button.setGeometry(200, 150, 150, 40)
  
        # adding action to a button
        button.clicked.connect(self.clickme)
  
        # setting border and radius
        # radius = half of height
        button.setStyleSheet("border : 2px solid black; 
                              border-radius : 20px;")
  
  
    # action method
    def clickme(self):
  
        # printing pressed
        print("pressed")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :