pyqt显示可选择项
时间: 2023-11-24 22:47:00 浏览: 31
您可以使用QComboBox控件来显示可选择项。以下是一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QComboBox, QWidget, QVBoxLayout
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
combo_box = QComboBox()
combo_box.addItems(['Option 1', 'Option 2', 'Option 3'])
layout.addWidget(combo_box)
window.setLayout(layout)
window.show()
app.exec_()
```
这将创建一个窗口,其中包含一个下拉列表框,其中包含三个选项:“Option 1”,“Option 2”和“Option 3”。
相关问题
PyQt 创建多选下拉框 选中可在选框中显示内容 点击按钮获取选中值 并弹窗显示选中值
下面是一个简单的示例代码,可以创建一个多选下拉框,选中的内容将显示在选框中,点击按钮可以获取选中的值并弹窗显示。
```python
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox, QLabel, QPushButton, QMessageBox
class MultiSelectComboBox(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
self.layout = QVBoxLayout()
self.combobox = QComboBox(self)
self.combobox.setEditable(True)
self.combobox.setInsertPolicy(QComboBox.NoInsert)
self.combobox.view().pressed.connect(self.handle_item_pressed)
self.label = QLabel(self)
self.label.setText("Selected items:")
self.layout.addWidget(self.combobox)
self.layout.addWidget(self.label)
self.setLayout(self.layout)
def handle_item_pressed(self, index):
item = self.combobox.itemText(index)
if item:
if index.row() == -1:
self.combobox.addItem(item)
else:
self.combobox.removeItem(index.row())
def getSelectedItems(self):
selected_items = []
for index in range(self.combobox.count()):
item = self.combobox.itemText(index)
selected_items.append(item)
return selected_items
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.layout = QVBoxLayout()
self.combobox = MultiSelectComboBox(self)
self.layout.addWidget(self.combobox)
self.button = QPushButton("Get Selected Items", self)
self.button.clicked.connect(self.show_selected_items)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
def show_selected_items(self):
selected_items = self.combobox.getSelectedItems()
if selected_items:
message = "Selected Items:\n\n" + "\n".join(selected_items)
else:
message = "No Items Selected!"
QMessageBox.information(self, "Selected Items", message)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
在上面的代码中,我们定义了一个名为 `MultiSelectComboBox` 的自定义小部件,它继承自 `QWidget`。该小部件包含一个 `QComboBox` 和一个 `QLabel`,用于显示选中的项目。我们还定义了一个名为 `handle_item_pressed` 的方法,它将处理通过单击下拉框中的项来选择或取消选择该项的操作。我们还定义了一个名为 `getSelectedItems` 的方法,它将返回选中的项目。
在 `MainWindow` 类中,我们创建了一个 `MultiSelectComboBox` 实例,并将其添加到一个垂直布局中。我们还创建了一个 `QPushButton`,用于获取选中的项目,并将其添加到相同的布局中。当用户单击该按钮时,我们将显示一个包含选中项目的消息框。
pyqt各个组件的继承关系
PyQt是一个Python绑定Qt库的工具包,它提供了与Qt框架完全兼容的Python API。在PyQt中,各个组件的继承关系如下:
1. QWidget:是所有用户界面组件的基类,它提供了窗口的基本功能。其他大部分组件都是从QWidget派生而来。
2. QMainWindow:是主窗口的基类,提供了一个应用程序的主窗口框架。它通常包含了菜单栏、工具栏、状态栏等。
3. QDialog:是对话框的基类,用于显示一个模态对话框或非模态对话框。
4. QLabel:用于显示文本或图像。
5. QLineEdit:用于接收用户的单行文本输入。
6. QTextEdit:用于编辑多行文本。
7. QPushButton:用于显示一个按钮,用户可以点击它执行相应的操作。
8. QRadioButton:用于显示一个单选按钮。
9. QCheckBox:用于显示一个复选框。
10. QComboBox:用于显示一个下拉列表框,用户可以选择其中的一项。
11. QSpinBox:用于显示一个数字选择框,用户可以通过增加或减少按钮选择一个数字。
12. QSlider:用于显示一个滑动条,用户可以通过拖动滑块选择一个值。
13. QProgressBar:用于显示一个进度条,表示某个任务的进度。
14. QTableWidget:用于显示二维表格数据。
15. QTreeView:用于显示树形结构数据。
16. QGraphicsView:用于显示一个可交互的图形场景。
17. QWebView:用于显示Web页面。
以上仅是PyQt中常用组件的继承关系,还有其他一些特殊用途的组件可以通过查阅PyQt文档来获取更详细的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)