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

QtGui.QComboBox的更多相关文章

  1. Pyqt QComboBox 省市区县联动效果

    在Qt中, QComboBox方法窗口组件允许用户从列表清单中选择,在web中就是select标签,下拉选项. 省市区县的联动就是currentIndexChanged 获取当前的Index,通过这个 ...

  2. Pyqt Smtplib实现Qthread多线程发送邮件

    一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接 ...

  3. Pyqt QSystemTrayIcon 实现托盘效果

    pyqt的托盘效果很好实现,在Pyqt的demo中有个例子 路径:PyQt4\examples\desktop\systray.py 今天我就仿这个Tray效果做效果 一. 创建UI trayicon ...

  4. Qt4升级Qt5注意问题

    Qt4升级Qt5注意问题 Qt4过渡到Qt5的项目一开始就受阻,记录一下遇到的下面的问题 --->编译遇到类似错误: error: QCalendarWidget: No such file o ...

  5. pyqt记录内容(音乐播放器)

    #这是UI文件 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'AudioPlayerDia ...

  6. PYQT4 : QSystemTrayIcon练习

    照着demo自己做了一遍,练练手 import sys from PyQt4 import QtGui from PyQt4 import QtCore class SysTray(QtGui.QDi ...

  7. pyqt4_应用例子(计算器,对话框,进度条,日历等等)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  8. PYQT控件使用

    QtGui.QComboBox .addItem(string)#添加字符串项到Item.addItems(list)#添加列表或元组元素到Item.clear()#清除所有Item.clearEdi ...

  9. ZetCode PyQt4 tutorial widgets II

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

随机推荐

  1. [BZOJ 3720][JZYZOJ 2016]gty的妹子树 强制在线 树分块/树套树

    jzyzoj的p2016 先码着,强制在线的树分块或者树套树?关键是我树分块还在入门阶段树套树完全不会啊摔   http://blog.csdn.net/jiangyuze831/article/de ...

  2. NOIP2018之前

    先过了初赛再说. \(10.17\) 初赛成绩昨天出了,\(89\)的估分莫名多了\(5\)分问题求解第二题改卷老师看错了?,接下来大概是要停课在一中集训一个月备战\(NOIP\),回去第二天就要期中 ...

  3. 【9.23校内测试】【抽屉原理】【乱搞??(找众数】【Trie】

    看到题目一开始想到的是一道求子集和的异或和,可以用$bitset$实现求子集和.然而这道题如果要强算子集和肯定是带不动的,况且还要算方案,所以尝试去找题目中的性质. 看到整除,很容易想到如果是一段区间 ...

  4. 《python学习手册》第32章 异常基础

    发生异常与默认的异常处理   当发生异常的时候,我们代码没有刻意捕获这个异常,所以它会一直向上返回到程序顶层,并启用默认的异常处理器:打印标准出错信息.而且会终止程序.   执行下面程序 def fu ...

  5. Backbone.js 的最佳应用场景有哪些?#zhihu#

    这段时间,想再次了解下backbone js的相关知识,就把一些认为不错的拿过来了: 新版的有道笔记 Web 版(http://note.youdao.com)也使用了 Backbone.就像其他答案 ...

  6. JVM堆内存的分代

    虚拟机的堆内存共划分为三个代:年轻代(Young Generation).年老代(Old Generation)和持久代(PermanentGeneration).其中持久代主要存放的是Java类的类 ...

  7. 《C# to IL》第一章 IL入门

    我们用C#.VB.NET语言编写的代码最终都会被编译成程序集或IL.因此用VB.NET编写的代码可以在C#中修改,随后在COBOL中使用.因此,理解IL是非常有必要的. 一旦熟悉了IL,理解.NET技 ...

  8. js利用正则替换图片路径问题

    /* * 需求:给图片的src地址前面增加为assets * * 1. 将需要匹配的内容原封不动的写入正则表达式 * 2. 将改变的内容替换为.*? * 3. 将.*?添加(),目的是为了能够将src ...

  9. DLL Dynamic-Link Library Search Order

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx A system can contain ...

  10. source insight完全卸载

    由于不知名原因 source insight崩溃了,使用自带的卸载,完成之后重新安装软件注册还是出问题.在网上搜索资料发现就是删除注册表中的内容. 由于列出的删除项目不完全,导致还是出问题. 最后删除 ...