当前位置:网站首页>Pyqt5: button control

Pyqt5: button control

2022-06-11 03:04:00 Solitary rest

Functions such as generating buttons and positions can be quickly displayed in Qt designer Implemented on

pushbutton

Set button text information :

self.pushButton.setText(' The second button ')

Set button image

self.pushButton.setIcon(QIcon(QPixmap('11.jpg')))

Set button mode to check

self.pushButton_3.setCheckable(True)
self.pushButton_3.setChecked(False)# The initial status is unselected 
self.pushButton_3.toggle()# This can change the initial state 

Set multiple buttons bound to a function :

self.pushButton_3.setText(' The first button ')
self.pushButton_3.clicked.connect(lambda: self.pushbutton_click(self.pushButton_3))
self.pushButton_4.setText(' The second button ')
self.pushButton_4.clicked.connect(lambda: self.pushbutton_click(self.pushButton_4))

def pushbutton_click(self, btn):
    print(' The button of the stand-alone is ' + btn.text())
    if btn.text()==" The first button ":
        webbrowser.open('https://blog.csdn.net/qq_54517101?spm=1000.2115.3001.5343')
    if btn.text()==" The second button " and btn.isChecked():
        webbrowser.open('https://blog.csdn.net/qq_54517101/article/details/123188489')

The way button.clicked.connect and button.toggled.connect The difference between , The former is the connection between click event and function , The latter is related to the function when the state of the button changes , Commonly used in commandLinkButton( Radio button )

checkbox

checkbox Button settings can be half selected

self.checkBox_2.setTristate(True)

The initial setting is half selected

self.checkBox_2.setCheckState(Qt.PartiallyChecked)

If you want to set the initial to fully selected

self.checkBox_2.setChecked(True)
# perhaps 
self.checkBox_2.setCheckState(Qt.Checked)

take checkbox The state change and function connection of , Should not be used toggled, It is stateChanged

self.checkBox_2.stateChanged.connect(self.q111)

def q111(self):
    webbrowser.open('https://blog.csdn.net/qq_54517101?spm=1000.2115.3001.5343')

combobox

combobox Add drop-down options

self.comboBox.addItems(['Java', 'python', 'C++'])
self.comboBox.addItem('abc')

The relevant operation

 self.comboBox.currentTextChanged.connect(self.selectchange)# State switch connection 

 def selectchange(self):
     self.label.setText(self.comboBox.currentText())# Read current selection 
     self.label.adjustSize()
     for num in range(self.comboBox.count()):# Get selection quantity 
         print('item' + str(num) + '=' + self.comboBox.itemText(num))# Select the... Of the drop-down selection num individual 

原网站

版权声明
本文为[Solitary rest]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020555199074.html