python - PyQt5 ComboBox 虚拟占位符
问题描述
我碰巧使用了 qcombobox,但找不到添加虚拟占位符的方法(就像我们在 tkinter 中一样)。例如,我想要的组合框是这样的:
- 选一个 -
苹果
香蕉
猕猴桃
我已经有了元素;Apple、Banana、Kiwi 就位,但我希望--Choose One--在我单击它时消失。
**Tkinter version**
from tkinter import ttk
#---
#some code here
#---
lst = ['Apple','Banana','Kiwi']
self.w0 = ttk.Combobox(self, values = lst, state='readonly')
self.w0.set('--Choose One--') ### This is the functionality that I need
self.w0.pack()
**PyQt version**
from PyQt5 import QtWidgets
lst = ['Apple','Banana','Kiwi']
#---
#some code here
#---
self.comboBox = QtWidgets.QComboBox()
self.comboBox.addItems(lst)
self.layout.addWidget(self.comboBox) ## self.layout is defined but not shown here
### I might add it as an item of the comboBox like
### self.comboBox.addItem('--Choose One--') but it then becomes selectable
### which I don't want it to.
解决方案
(在示例中)的set()
方法等价于的属性:ttk.Combobox
placeholderText
QComboBox
self.comboBox.setPlaceholderText("--Choose One--")
推荐阅读
- ajax - ajax调用后部分视图变为空
- android - 如果特定文件夹已创建或删除文件,如何在本机反应中获得通知
- html - 如何在与图像相同高度的情况下将锚点放置在 div 中间?
- c# - 如何在 .NET Framework 中停止 COM 代理
- javascript - Javascript KeyUp 没有调用 HTML 表单
- azure - 无法在 AKS 上启动 Confluent 控制中心
- c++ - c++ 代码不工作,我的输出屏幕崩溃
- gradle - Gradle 坚持解决依赖关系
- php - 使用 PhpExcel 通过 php 读取 xls 文件的奇怪问题
- reactjs - 如何使用反应样式的组件定位-webkit-calendar-picker-indicator?