Python Pyqt QComboBox显示选项

时间:2016-09-12 08:16:17

标签: python pyqt

我正在尝试使用按钮,文本框和组合框创建GUI。我的组合框有问题。我可以创建它,但是一旦我点击它就不会显示选项。我没有收到任何让我很难找到问题的错误。这是到目前为止的代码:

from PyQt4.QtGui import *
from PyQt4 import  QtGui
import sys

class Window(QtGui.QMainWindow,QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(500, 5, 1100, 1000)
        self.setWindowTitle("FourC Analyser")
        self.app = QtGui.QApplication([])
        self.app.setStyleSheet('QMainWindow{background-color: rgb(49,79,79);border: 1px solid black;}')


        extractAction = QtGui.QAction("&GET TO THE CHOPPAH!!!", self)
        extractAction.setShortcut("Ctrl+Q")
        extractAction.setStatusTip('Leave The App')
        self.home()

    def home(self):
        labelCol1= 'color: rgb(255,250,205)'
        textboxCol1=("QLineEdit {background-color: rgb(49,79,79); color: rgb(218,165,32);}")

        #label
        self.lbl_project = QtGui.QLabel('Project Name', self)
        self.lbl_project.move(20, 120)
        self.lbl_project.setStyleSheet(labelCol1)

        #text box
        self.textbox = QtGui.QLineEdit(self)
        self.textbox.move(20, 150)
        self.textbox.resize(280,30)
        self.textbox.setStyleSheet(textboxCol1)

        #Combo box
        self.cb=QtGui.QComboBox(self)
        self.cb = QComboBox()
        self.cb.addItems(["1","2","3"])


    def selectionchange(self,i):
        print "Items in the list are :"
        for count in range(self.cb.count()):
            print self.cb.itemText(count)
        print "Current index",i,"selection changed ",self.cb.currentText()

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    GUI.show()
    sys.exit(app.exec_())
run()

1 个答案:

答案 0 :(得分:1)

您正在重新定义self.cb

self.cb = QComboBox()

通过删除此行,它适用于我。
这两个定义都起作用,因为你的import语句。使用from PyQt4.QtGui import *导入模块QtGuifrom PyQt4 import QtGui内的所有内容,即可导入模块QtGui本身。我建议删除from PyQt4.QtGui import *