PyQt5 – QCommandLinkButton类
QCommandLinkButton 是一个由Windows Vista引入的控制小部件。它的用途类似于一个单选按钮,因为它被用来在一组互斥的选项中进行选择。它的外观一般类似于一个扁平的按钮,但除了正常的按钮文本外,它还允许描述性文本。默认情况下,它还会带有一个箭头图标,表示按下该控件将打开另一个窗口或页面或做一些事情。下面是命令链接按钮的样子
例子:
我们将创建一个有标签和命令链接按钮的窗口,当命令链接按钮被按下时,标签中的计数器将递增。
以下是实现的过程
# 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, 500, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for components
def UiComponents(self):
# counter value
self.n = 0
# creating label
label = QLabel("Counter", self)
# setting label geometry
label.setGeometry(100, 100, 100, 40)
# creating a command link button
cl_button = QCommandLinkButton("Next", self)
# setting geometry
cl_button.setGeometry(200, 100, 200, 40)
# adding action to the button
cl_button.clicked.connect(lambda: increment(self.n))
# method for incrementing the counter
def increment(n):
# increment
self.n = n + 1
# setting text to the label
label.setText(str(self.n))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())