一、QPushButton控件
QAbstractButton是所有按鈕空間的父類,提供了一系列按鈕共用方法。
1.按鈕的可選中狀態
QPushButton雖然是一個普通按鈕,但也可以由checkbox那樣的選中狀態(按下和彈起)。
def initUI(self): button1 = QPushButton("按下/彈起") button1.setCheckable(True) # 讓普通按鈕支持check功能 button1.toggle() # 默認按下按鈕 vbox = QVBoxLayout() vbox.addWidget(button1) self.setLayout(vbox)
2.點擊事件綁定槽函數時傳入參數
def initUI4(self): self.button1 = QPushButton("按下/彈起") # 將button1的點擊事件綁定到槽函數onClickButton1上,但是由於要傳遞參數,所以使用一個匿名函數再包裝哦一層 self.button1.clicked.connect(lambda: self.onClickButton1(self.button1)) vbox = QVBoxLayout() vbox.addWidget(self.button1) self.setLayout(vbox) def onClickButton1(self, pushed_btn): print("你點擊的按鈕是:", pushed_btn.text())
點擊button1后控制台輸出信息: 你點擊的按鈕是: 按下/彈起
3.在按鈕上顯示圖標
self.button1 = QPushButton("圖標按鈕") self.button1.setIcon(QIcon('./images/icon.ico')) # 設置ico格式的圖標 self.button2 = QPushButton("圖標按鈕2") self.button2.setIcon(QIcon(QPixmap('./images/ailusha.png'))) # 將普通圖片轉換為圖標
效果:
4.按鈕其他操作
1)默認按鈕
一個頁面只能有一個默認按鈕:
button.setDefault(True)
2)可用/不可用
button.setEnabled(False) # 不可用,顯示為灰色
3)熱鍵
button = QPushButton("&Mybutton") # 熱鍵是Alt+M,按熱鍵就相當於點擊
二、QRadioButton控件
QRadioButton是單元控件。
def initUI5(self): self.radioButton1 = QRadioButton("單選按鈕1") self.radioButton1.toggled.connect(self.toggleButton) self.radioButton2 = QRadioButton("單選按鈕2") self.radioButton2.toggled.connect(self.toggleButton) vbox = QVBoxLayout() vbox.addWidget(self.radioButton1) vbox.addWidget(self.radioButton2) self.setLayout(vbox) def toggleButton(self): btn = self.sender() if btn.isChecked() == True: print('<' + btn.text() + '> 被選中') else: print('<' + btn.text() + '> 被取消選中')
效果:
三、QCheckBox控件
QCheckBox是一個復選控件。
QCheckBox有三種選中狀態:
1)未選中:0
2)半選中:1
3)選中:2
def initUI6(self): self.checkBox1 = QCheckBox('復選框1') self.checkBox1.setChecked(True) self.checkBox1.stateChanged.connect(lambda: self.checkboxState(self.checkBox1)) self.checkBox2 = QCheckBox('復選框2') self.checkBox2.stateChanged.connect(lambda: self.checkboxState(self.checkBox2)) self.checkBox3 = QCheckBox('半選中復選框3') self.checkBox3.stateChanged.connect(lambda: self.checkboxState(self.checkBox3)) self.checkBox3.setTristate() # 讓其支持半選中狀態 self.checkBox3.setCheckState(Qt.PartiallyChecked) # 默認為半選中 vbox = QVBoxLayout() vbox.addWidget(self.checkBox1) vbox.addWidget(self.checkBox2) vbox.addWidget(self.checkBox3) self.setLayout(vbox) def checkboxState(self, cb): checkbox1Status = self.checkBox1.text() + ', isChecked=' + str( self.checkBox1.isChecked()) + ', checkState=' + str(self.checkBox1.checkState()) + '\n' checkbox2Status = self.checkBox2.text() + ', isChecked=' + str( self.checkBox2.isChecked()) + ', checkState=' + str(self.checkBox2.checkState()) + '\n' checkbox3Status = self.checkBox3.text() + ', isChecked=' + str( self.checkBox3.isChecked()) + ', checkState=' + str(self.checkBox3.checkState()) + '\n' print(checkbox1Status + checkbox2Status + checkbox3Status)
效果:
四、QComboBox控件
QComboBox就是我們常用的下拉選擇框。
def initUI7(self): self.label = QLabel("請選擇一種編程語言:") self.comboBox = QComboBox() self.comboBox.addItem("Python") # 想列表中添加一個item self.comboBox.addItem("C語言") self.comboBox.addItems(["C++", "JAVA", "Ruby"]) # 可以使用列表形式添加 self.comboBox.currentIndexChanged.connect(self.selectionChanged) # 當前選中的index變化時觸發槽函數 self.outputLabel = QLabel("") # 用於顯示選中的項 hbox = QHBoxLayout() hbox.addWidget(self.label) hbox.addWidget(self.comboBox) vbox = QVBoxLayout() vbox.addLayout(hbox) vbox.addWidget(self.outputLabel) self.setLayout(vbox) def selectionChanged(self): currentSelectedText = self.comboBox.currentText() currentSelectedIdx = self.comboBox.currentIndex() self.outputLabel.setText(currentSelectedText + ' index是' + str(currentSelectedIdx)) self.outputLabel.adjustSize()
效果:
===