PyQt5:按钮控件

生成按钮以及位置等功能可以较快的在Qt designer上实现

pushbutton

设置按钮文本信息:

self.pushButton.setText('第二个按钮')

设置按钮图像

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

设置按钮模式为check

self.pushButton_3.setCheckable(True)
self.pushButton_3.setChecked(False)#初始为未选中状态
self.pushButton_3.toggle()#这个可以转换初始状态

设置多个按钮与一个函数绑定:

self.pushButton_3.setText('第一个按钮')
self.pushButton_3.clicked.connect(lambda: self.pushbutton_click(self.pushButton_3))
self.pushButton_4.setText('第二个按钮')
self.pushButton_4.clicked.connect(lambda: self.pushbutton_click(self.pushButton_4))

def pushbutton_click(self, btn):
    print('被单机的按钮是' + btn.text())
    if btn.text()=="第一个按钮":
        webbrowser.open('https://blog.csdn.net/qq_54517101?spm=1000.2115.3001.5343')
    if btn.text()=="第二个按钮" and btn.isChecked():
        webbrowser.open('https://blog.csdn.net/qq_54517101/article/details/123188489')

说一下button.clicked.connect和button.toggled.connect的区别,前者是单击事件与函数联系,后者是按钮的状态改变了就与函数联系,一般用于commandLinkButton(单选按钮)

checkbox

checkbox按钮设置可以半选

self.checkBox_2.setTristate(True)

设置初始为半选

self.checkBox_2.setCheckState(Qt.PartiallyChecked)

如果要设置初始为完全选中

self.checkBox_2.setChecked(True)
#或者
self.checkBox_2.setCheckState(Qt.Checked)

将checkbox的状态变化与函数连接,不应该使用toggled,而是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添加下拉选项

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

相关操作

 self.comboBox.currentTextChanged.connect(self.selectchange)#状态切换连接

 def selectchange(self):
     self.label.setText(self.comboBox.currentText())#读取当前选择
     self.label.adjustSize()
     for num in range(self.comboBox.count()):#获取选择数量
         print('item' + str(num) + '=' + self.comboBox.itemText(num))#选取下拉选择的第num个


版权声明:本文为qq_54517101原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。