The QtGui.QComboBox is a widget that allows a user to choose from a list of options.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial This example shows
how to use QtGui.QComboBox widget. author: Jan Bodnar
website: zetcode.com
last edited: September 2011
""" import sys
from PyQt4 import QtGui, QtCore class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.lbl = QtGui.QLabel("Ubuntu", self) combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo") combo.move(50, 50)
self.lbl.move(50, 150) combo.activated[str].connect(self.onActivated) self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QComboBox')
self.show() def onActivated(self, text): self.lbl.setText(text)
self.lbl.adjustSize() def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

The example shows a QtGui.QComboBox and a QtGui.QLabel. The combo box has a list of five options. These are the names of Linux distros. The label widget displays the selected option from the combo box.

combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo")

We create a QtGui.QComboBox widget with five options.

combo.activated[str].connect(self.onActivated)

Upon an item selection, we call the onActivated() method.

def onActivated(self, text):

    self.lbl.setText(text)
self.lbl.adjustSize()

Inside the method, we set the text of the chosen item to the label widget. We adjust the size of the label.

Figure: QtGui.QComboBox

最新文章

  1. hibernate中一对多关系中的inverse,cascade属性
  2. Windows安装mxnet
  3. 安装 Dubbo 注册中心(Zookeeper-3.4.6)--单节点
  4. magento缓存系列详解:clean cache
  5. IIS6.0禁止用户下载txt文件
  6. 学习java随笔第十一篇:java窗体程序
  7. (中等) POJ 2528 Mayor's posters , 离散+线段树。
  8. Java数据结构和算法(十五)——无权无向图
  9. 面试作业之浅析京东促销活动核心模型 - DDD
  10. Python3 NameError: name 'open' is not defined处理办法
  11. SpringBoot aop 注解 数据权限校验
  12. php 删除一维数组中某一个值元素的操作方法
  13. Linux学习系列之一:在centos 7.5上安装nginx 以及简单配置
  14. 【转】25个非常实用的jQuery/CSS3应用组件
  15. ASP.NET Web Pages:C# 和 VB 实例
  16. Ansible playbook基础组件介绍
  17. Java之MD5加密
  18. LR-事务
  19. Uncaught TypeError: Cannot set property 'value' of null
  20. Generator 函数的异步应用

热门文章

  1. serializable parcelable
  2. Codeforces Beta Round #7 C. Line Exgcd
  3. Educational Codeforces Round 10 C. Foe Pairs 水题
  4. js中进行金额计算 parseFloat 会产生精度问题
  5. KVM虚拟机安装使用教程(Ubantu)
  6. UVA1493 - Draw a Mess(并查集)
  7. Spring内部bean实例
  8. JavaScript中typeof详解
  9. 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
  10. WebLogic中WLS 组件漏洞(CVE-2017-10271)专项检测工具