Python Forum
combobox value PyQt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
combobox value PyQt
#1
I a learning python programming and selected PyQt5 to use for my GUI.

I have created a test form just to get the feel of python and I want to connect functions from a module file called 'mymods' to each widget.

How can i pull say the selected text or item index from a combobox from my test form using the mymods.py file?

test.py:

import mymods

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(30, 30, 261, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(30, 90, 91, 41))
        self.checkBox.setObjectName("checkBox")
        self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton.setGeometry(QtCore.QRect(160, 100, 82, 17))
        self.radioButton.setObjectName("radioButton")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 180, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox_2.setGeometry(QtCore.QRect(340, 30, 241, 22))
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pushButton.clicked.connect(mymods.mytest1)
        self.comboBox.activated.connect(mymods.populatecombos)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.comboBox.setItemText(0, _translate("MainWindow", "Select Item"))
        self.comboBox.setItemText(1, _translate("MainWindow", "item1"))
        self.comboBox.setItemText(2, _translate("MainWindow", "item2"))
        self.comboBox.setItemText(3, _translate("MainWindow", "item3"))
        self.checkBox.setText(_translate("MainWindow", "CheckBox"))
        self.radioButton.setText(_translate("MainWindow", "RadioButton"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.comboBox_2.setItemText(0, _translate("MainWindow", "Select Item"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
mymods.py

def mytest1(self):
    print("mytest module 1")

def populatecombos(combobox1):
    cb1 = str(comboBox.currentText())
    print(cb1)
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply
#2
You should do it the other way around. As your UI file is likely auto-generated, instead it should be imported from your custom module (mymods). Then 'mymods' become your main thread and you can control everything from there. If you would rather load a raw ui file from QtDesigner (highly recommended), it would look like this:

#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets , uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "untitled.ui", self)
		self.ui.comboBox.currentTextChanged.connect(self._pullComboText)
        self.show()

	def _pullComboText(self, text):
		print(text)

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
Reply
#3
(Dec-05-2018, 02:32 PM)Alfalfa Wrote: You should do it the other way around. As your UI file is likely auto-generated, instead it should be imported from your custom module (mymods). Then 'mymods' become your main thread and you can control everything from there. If you would rather load a raw ui file from QtDesigner (highly recommended), it would look like this:

Ahh. makes sense. I do use QT Designer to create the form.
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply
#4
one other questions. can the python files be compiled into an executable for both Windows and Linux when using the .ui files ?
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply
#5
I don't know if it is possible to make an executable from PyQt at all, but it is possible to deploy your program with PyPi and to compile the ui files to python upon install. Here is how I do it on Linux (full example here):

install_requires=['mutagen']
try:
    # Compile ui files to python
    from PyQt5 import uic
    uic.compileUiDir('qoob/ui')
except:
    install_requires.append("pyqt5")

setuptools.setup(
...
Reply
#6
(Dec-07-2018, 08:14 PM)shift838 Wrote: one other questions. can the python files be compiled into an executable for both Windows and Linux when using the .ui files ?
Yes use pyinstaller it can build to Windows, Mac and linux.
E.G:
pyinstaller --noconfirm --log-level=WARN -D -w --name livros --distpath c:\ -y --icon=books.ico main.py
Reply
#7
(Dec-14-2018, 07:37 PM)starglider Wrote: Yes use pyinstaller it can build to Windows, Mac and linux.

Bu do not use python3 to make an app with pyinstaller or cxfreeze for PyQt5.

Both have the problem for some time that they copy with python3 the entire Qt5 into the app.
It works, but you get apps that are mostly more than 300MB in size.
The same app has only 22MB if you use python2.
Reply
#8
(Dec-14-2018, 08:52 PM)Axel_Erfurt Wrote: But do not use python3 to make an app with pyinstaller or cxfreeze for PyQt5.

Both have the problem for some time that they copy with python3 the entire Qt5 into the app.
It works, but you get apps that are mostly more than 300MB in size.
The same app has only 22MB if you use python2.

Do you know if this will likely be fixed in the future? I tried to make an exe once with this kind of tool, and it ended up so big. I thought it was just not appropriate for Qt. Good to know it works with Python2, altough not very useful.
Reply
#9
Don't use python 2.
whats 300 MB in today 1 TB hard drives?
Compress with 7z as an exe?
The changes from 2 to +3.5 and to PyQt5 are worth the size increase!
Think utf-8.
Get rid of str when calling a QString.
Don't use the Qt designer make the UI by hand much more easy and powerful.

my 0.02 €
Reply
#10
(Dec-14-2018, 09:22 PM)Alfalfa Wrote: Do you know if this will likely be fixed in the future? I tried to make an exe once with this kind of tool, and it ended up so big. I thought it was just not appropriate for Qt. Good to know it works with Python2, altough not very useful.

The mistake has existed for some time, I have little hope ...
from June 2017 (pyinstaller issues on github)
Quote:... the Qt5-stuff might have room for improvement. We appreciate improvements on this ...

If you use python2 to create the app you must have the modules used as versions for python2.

Check first if your python file works in python2.

As it is in Windows, I do not know.
Most entries are from people using Linux or OSX.

(Dec-14-2018, 09:54 PM)starglider Wrote: Get rid of str when calling a QString.

from stackoverflow Wrote:In PyQt5, there is no QString and hence no need for QStringList.

Any Qt API that would normally return a QString, will automatically return a Python string instead. Similarly, any Qt APIs that would normally return a QStringList will return a Python list containing Python strings. And the opposite also applies: any Qt API that would normally accept a QString or QStringList will accept the Python equivalents instead.

(Dec-14-2018, 09:54 PM)starglider Wrote: Don't use the Qt designer make the UI by hand much more easy and powerful.

Thumbs Up
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

Forum Jump:

User Panel Messages

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