Python Forum
need help with combobox Python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help with combobox Python 2.7
#1
I am trying to figure this out, I used Qt Designer to make this gui, I want the combobox to access a file on my harddrive. Can anyone give me a hand. I know if I wanted a button to open the file its not so hard just add buttonclicked but for the combobox well its not exactly a button, so I am a little lost.

Here is the code for the GUI

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'zip808easy1.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(792, 179)
        MainWindow.setStyleSheet(_fromUtf8("background-color: rgb(116, 116, 116);"))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_4.sizePolicy().hasHeightForWidth())
        self.pushButton_4.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Modern"))
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setStyleSheet(_fromUtf8("background-color: rgb(255, 85, 0);"))
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.gridLayout.addWidget(self.pushButton_4, 0, 0, 1, 1)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setStyleSheet(_fromUtf8("plastique"))
        self.label.setObjectName(_fromUtf8("label"))
        self.verticalLayout.addWidget(self.label)
        self.Bank1 = QtGui.QComboBox(self.centralwidget)
        self.Bank1.setStyleSheet(_fromUtf8("background-color: rgb(0, 170, 255);"))
        self.Bank1.setObjectName(_fromUtf8("Bank1"))
        self.verticalLayout.addWidget(self.Bank1)
        self.gridLayout.addLayout(self.verticalLayout, 0, 2, 3, 1)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 0, 1, 2, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 792, 26))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton_4.setText(_translate("MainWindow", "Pad1", None))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt; font-weight:600;\">BANK ONE</span></p></body></html>", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Reply
#2
Haven't used Qt for some time, but have used several other packages,
and they're all basically the same.

First, check Qt documentation to see if they have a load from file method already built in.

If not, You need to load the combo box manually, which isn't very difficult.

Opening and reading the file would be the same as for any other purpose.
when you isolate the data that you want to populate the combo box with, you will need to find a Qt command
to insert text into the combo box, and do that line by line as you read the data from the file.
See: http://doc.qt.io/qt-5/qcombobox.html find index and insert

You will then also have to bind clicks from the combobox to an event method.
In wxpython and tkinter, this is literally a bind instruction, and probably on Qt as well.
In the routine that you bind to, you will need to extract the index of the item in the combo box,
then get the item at that index, and use it to reference back to your main data source.

Since I don't know the specific commands in Qt, you will have to look them up.
The insert statement should be a method attached to the ComboBox widget, and found in the
documentation for that.

Bind is a bit more general, and probably has it's own documentation.
The syntax is usually the same for buttons, combobox, and many other widgets,
and will have the form of widget.bind(<event type>, methodToexecute)
see: http://doc.qt.io/archives/qt-4.8/eventsandfilters.html

You should be able to use this as a guide to help find associated docs.
Reply
#3
Thanx Lars for your reply, I have been searching Qt's doc's haven't found anything yet but I need to look more.

Do you think I will be able to populate the dropdown list with the contents of a folder without having to input them one by one?
Reply
#4
Look at the links I put in the post. They point to docs I believe you need.
It's not difficult. Here's one I did in wxpython:
    def rfc_selector_load(self):
        """
        Clears, then Loads self.rfc_selector ListCtrl with data from self.rfc_index dictionary.
        :return: None
        """
        self.rfc_selector.DeleteAllItems()
        for key, value in self.rfc_index.items():
            index = self.rfc_selector.GetItemCount()
            self.rfc_selector.InsertItem(index, key)
            self.rfc_selector.SetItem(index, 1, value['title'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,332 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
  [PyQt] Control a combobox from another python file PyQt5 prath 0 2,242 May-05-2020, 03:22 PM
Last Post: prath
  [python] [Tkinter] Problem bidding combobox with np.array NEL 3 3,345 Aug-04-2019, 11:07 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020