Python PyQt5 QComboBox 下拉式選單用法與範例

本篇 ShengYu 介紹 Python PyQt5 QComboBox 下拉式選單用法與範例,ComboBox 下拉式選單通常適用於讓使用者從多個選項中選擇一個的情境。

以下的 Python PyQt5 QComboBox 用法與範例將分為這幾部分,

  • PyQt5 建立 QComboBox
  • PyQt5 設定 QComboBox 預設的選項
  • PyQt5 取得目前 QComboBox 的選項
  • PyQt5 QComboBox 綁定事件

PyQt5 建立 QComboBox

PyQt5 建立 QComboBox 的用法如下,建立 QComboBox() 後,再用 addItems() 將選項的數值設定進去,這邊我們以水果的選項作為示範,addItems() 接受 list 作為引數,所以這邊我們一次把所有水果的選項一次傳進去。

python-pyqt-qcombobox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QComboBox)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

self.mycombobox = QComboBox(self)
self.mycombobox.addItems(['apple', 'banana', 'orange', 'lemon', 'tomato'])
self.mycombobox.move(60, 50)

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

QComboBox 的 addItems() 是用來加入多個選項,如果要加入單一選項的話可以使用 addItem(),所以上述範例你也可以這樣寫,

1
2
3
4
5
self.mycombobox.addItem('apple')
self.mycombobox.addItem('banana')
self.mycombobox.addItem('orange')
self.mycombobox.addItem('lemon')
self.mycombobox.addItem('tomato')

PyQt5 設定 QComboBox 預設的選項

PyQt5 要設定 QComboBox 預設選項的話,可以透過 QComboBox.setCurrentIndex() 函式來設定,索引值從 0 開始,第一個選項的索引值為 0,如果設定的索引值超出範圍則會顯示空選項,以下示範設定索引值為 1,另外也可以使用 QComboBox.setCurrentText() 來設定預設的選項文字,如果設定的文字不在選項內的話則會顯示第一個選項。

python-pyqt-qcombobox2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QComboBox)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

self.mycombobox = QComboBox(self)
self.mycombobox.addItems(['apple', 'banana', 'orange', 'lemon', 'tomato'])
self.mycombobox.setCurrentIndex(1)
#self.mycombobox.setCurrentText('banana')
self.mycombobox.move(60, 50)

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

PyQt5 取得目前 QComboBox 的選項

這邊示範按下按鈕顯示目前選取的 QComboBox 選項,QComboBox.currentIndex() 可以取得目前 QComboBox 的選項索引值,而 QComboBox.currentText() 則是取得目前 QComboBox 的選項文字,首先使用 QPushButton.clicked.connect() 先設定按鈕事件的對應處理函式,如下例中的 onButtonClick(),關於 QPushButton 詳細的用法與按鈕事件可以參考這篇,當按下按鈕時在 onButtonClick() 把 mybutton 的按鈕文字設定成當前的 mycombobox 選項索引值跟選項文字。

python-pyqt-qcombobox3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QComboBox, QPushButton)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mycombobox = QComboBox(self)
self.mycombobox.addItems(['apple', 'banana', 'orange', 'lemon', 'tomato'])
gridlayout.addWidget(self.mycombobox, 0, 0)

self.mybutton = QPushButton('button', self)
self.mybutton.clicked.connect(self.onButtonClick)
gridlayout.addWidget(self.mybutton, 1, 0)

def onButtonClick(self):
self.mybutton.setText('idx: ' + str(self.mycombobox.currentIndex()) +
', ' + self.mycombobox.currentText())

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

PyQt5 QComboBox 綁定事件

有時我們會希望當 Combobox 有變動時可以處理這個事件的邏輯,例如下列例子中,當 Combobox 發生改變時就讓 label 顯示對應的選項,這其中就要用到 QComboBox.currentIndexChanged.connect 來連結這個事件對應的處理函式,如下例中的 onComboBoxChanged(),當 Combobox 有改動時就在 onComboBoxChanged() 函式裡讓 mylabel 顯示文字設定成目前 Combobox 的選項文字,關於 QLabel 詳細的用法可以參考這篇

python-pyqt-qcombobox4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QComboBox, QLabel)

class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)

gridlayout = QGridLayout()
self.setLayout(gridlayout)

self.mycombobox = QComboBox(self)
self.mycombobox.addItems(['apple', 'banana', 'orange', 'lemon', 'tomato'])
self.mycombobox.currentIndexChanged.connect(self.onComboBoxChanged)
gridlayout.addWidget(self.mycombobox, 0, 0)

self.mylabel = QLabel('label', self)
gridlayout.addWidget(self.mylabel, 1, 0)

def onComboBoxChanged(self):
self.mylabel.setText(self.mycombobox.currentText())

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())

結果圖如下,

以上就是 Python PyQt5 QComboBox 下拉式選單用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!
下一篇將會介紹 PyQt5 QRadioButton 單選框用法與範例

其它相關文章推薦
Python 新手入門教學懶人包
Python PyQt5 新手入門教學