I try to add items to QComboBox (deviceBox in my code) from function "get_devices" using "activated.connect()" method, but QComboBox is empty. Where I was mistaken?
#!/usr/bin/env python3
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialog
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from mainwindow import *
import sys
import pyudev
class MainProg (QMainWindow, Ui_MainWindow):
file_open = pyqtSignal(str, str)
def __init__(self, window):
QMainWindow.__init__(self)
self.setupUi(window)
self.openisoButton.clicked.connect(self.openISO)
self.aboutButton.clicked.connect(self.about)
self.deviceBox.activated.connect(self.get_devices)
def get_devices(self):
devices = []
context = pyudev.Context()
for device in context.list_devices(subsystem='block', ID_BUS="usb"):
devices.append(str(device['DEVNAME']))
self.deviceBox.addItems(devices)
QCombBox object have a method named addItem which could append item to exist item list.
so you need to modify your code in last line.
for device in devices:
self.deviceBox.addItem(device)
Related
Icons are not showing in my QListWidgetItems.
Edit 2:
Turns out you can't use absoulte path. You must use relative path. But why is this the case?
Minimum reproducible example:
import json
import os
import sys
from math import floor
from PyQt5 import QtTest, QtGui
from PyQt5.QtDesigner import QFormBuilder
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QFrame, QGridLayout, QLabel, QMainWindow, QScrollArea, QWidget, QVBoxLayout, \
QListView, QListWidget, QListWidgetItem, QToolButton
from PyQt5.QtCore import QPoint, Qt, QIODevice, QFile
from PyQt5.Qt import QPixmap
class MainWindow(QMainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
#ListWidget
listWidget = QListWidget(self)
listWidget.setViewMode(QListWidget.IconMode)
listWidget.setFixedSize(500, 700)
dir = r'Players'
for filename in os.listdir(dir):
#Item
item = QListWidgetItem(QIcon('Players/Pogba.jpg'), '<Name>', listWidget)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.showMaximized()
app.exec_()
I followed the docs(https://doc.qt.io/qt-5/qlistwidgetitem.html), so why are they not appearing?
Edit:
It looks like it only happens when the image is outside the source code's folder. Why is this the case?
My code was created with PyQt4 and I want to convert it to PyQt5.
I have tried some scripts to convert the code; but, nothing changed except the name.
What do I need to change manually in order to make the code work with PyQt5?
Here is the first part of my code:
import sys
from pymaxwell import *
from numpy import *
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import QMainWindow, QApplication
from PyQt4.QtCore import *
from PyQt4.phonon import Phonon
from ffmpy import FFmpeg
import os
import app_window_dark
import about
uifile = 'Ui/app_window_dark.ui'
aboutfile = 'Ui/about.ui'
Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)
class About(QtGui.QMainWindow, about.Ui_Dialog):
def __init__(self, parent=None):
super(About, self).__init__()
QtGui.QMainWindow.__init__(self, parent)
Ui_Dialog.__init__(self)
self.setWindowModality(QtCore.Qt.ApplicationModal)
point = parent.rect().bottomRight()
global_point = parent.mapToGlobal(point)
self.move(global_point - QPoint(395, 265))
self.setupUi(self)
class MyApp(QtGui.QMainWindow, app_window_dark.Ui_MainWindow):
def __init__(self):
super(MyApp, self).__init__()
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow.__init__(self)
self.setupUi(self)
self.about_btn.clicked.connect(self.popup)
#prev next
self.btn_next.clicked.connect(self.renderSet)
self.btn_prev.clicked.connect(self.renderSet)
and also this code:
if __name__ == "__main__":
app = QApplication(sys.argv)
#style = QApplication.setStyle('plastique')
window = MyApp()
window.setFixedSize(750, 320)
window.show()
sys.exit(app.exec_())
The main change from Qt4 to Qt5 and hence from PyQt4 to PyQt5 is the rearrangement of certain classes so that the Qt project is scalable and generates a smaller executable.
The QtGui library was divided into 2 submodules: QtGui and QtWidgets, in the second only the widgets, namely QMainWindow, QPushButton, etc. And that is the change you must make:
[...]
from PyQt5 import QtGui, QtCore, uic, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import *
[...]
Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)
class About(QtWidgets.QMainWindow, about.Ui_Dialog):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.setWindowModality(QtCore.Qt.ApplicationModal)
point = parent.rect().bottomRight()
global_point = parent.mapToGlobal(point)
self.move(global_point - QPoint(395, 265))
class MyApp(QtWidgets.QMainWindow, app_window_dark.Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.about_btn.clicked.connect(self.popup)
#prev next
self.btn_next.clicked.connect(self.renderSet)
self.btn_prev.clicked.connect(self.renderSet)
Note: Phonon does not exist in PyQt5, you must use QtMultimedia, an accurate solution you can find it in the following answer: Phonon class not present in PyQt5
I have a strange problem, hope someone can clear it for me
import os
from os import path
import sys
import pathlib
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget,
QWizard, QWizardPage, QLineEdit, \
QTabWidget, QApplication,
QTextEdit,QToolTip,QPushButton,QMessageBox
from PyQt5.QtCore import QSize,pyqtSlot,pyqtProperty
from PyQt5.QtGui import QFont
from PyQt5.uic import loadUiType
app = QApplication(sys.argv)
if getattr(sys, 'frozen', False):
# we are running in a bundle
installPath = sys._MEIPASS
print('we are running in a bundle')
else:
# we are running in a normal Python environment
installPath = os.path.dirname(os.path.abspath(__file__))
print('we are running in a normal Python environment')
UI_File, _ = loadUiType(path.join(path.dirname(__file__), 'test.ui'))
class MainAPP(QTabWidget, UI_File):
def __init__(self, parent=None):
super(MainAPP, self).__init__(parent)
self.setupUi(self)
self.handle_buttons()
def handle_buttons(self):
self.pushButton.clicked.connect(self.test_2)
def test_2(self):
for i in range(10):
self.listWidget.addItem(str('lklk'))
self.listWidget.itemClicked.connect(self.test)
def test(self):
for i in range(10):
self.listWidget_2.addItem(str('DDD'))
self.listWidget_2.itemClicked.connect(self.test_3)
def test_3(self):
print ('hi')
def main():
app = QApplication(sys.argv)
main = MainAPP()
main.show()
app.exec_()
if __name__ == "__main__":
main()
so basically, I have a push button, if I click on it it will display some data at listWidget and if I clicked on any item in listWidget , it will display other data on ListWidget_2 and then if I click on item in List_widget_2 it then should print ('Hi')
the problem is if I click multiple times in ListWidget and then click on an item in ListWidget_2 , I received more than one ('Hi) , it will diplay ('Hi') according to the number of clicks I clicked in the Listwidget
any idea what could be the issue
You only need to make a connection between a signal and a slot once. Currently you are making additional connections each time you click an item in the first list widget, which results in your method printing "hi" executing once for every connection you made.
To fix this, make both of the signal connections either in the test_2 method or in the __init__ method
I have a problem with my PyQt button action. I would like to send a String with the Function but I got this Error:
TypeError: argument 1 has unexpected type 'NoneType'
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QAction
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import *
from PyQt5.uic import *
app = QApplication(sys.argv)
cocktail = loadUi('create.ui')
def mixCocktail(str):
cocktail.show()
cocktail.showFullScreen()
cocktail.lbl_header.setText(str)
widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
widget.show()
sys.exit(app.exec_())
As suggested by user3030010 and ekhumoro it expects a callable function. In which case you should replace that argument with lambda: mixCocktail("string")
AND ALSO don't use str it's a python built-in datatype I have replaced it with _str
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QAction
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import *
from PyQt5.uic import *
app = QApplication(sys.argv)
cocktail = loadUi('create.ui')
def mixCocktail(_str):
cocktail.show()
cocktail.showFullScreen()
cocktail.lbl_header.setText(_str)
widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(lambda: mixCocktail("string"))
widget.show()
sys.exit(app.exec_())
More about lambda functions: What is a lambda (function)?
instead of this
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
write
widget.btn_ckt1.clicked.connect(lambda:mixCocktail("string"))
I need your help with the following problem that I've encountered.
I have two Python files, Main.py and Module.py, which need to communicate using PyQt5 signals. Here's the code:
Main.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MyGUI import main_window_GUI
from Modules import Module.py
class MainWindow(QMainWindow, main_window_GUI.Ui_main_window):
def __init__(self):
QMainWindow.__init__(self)
main_window_GUI.Ui_main_window.__init__(self)
self.setupUI(self)
sub_win = QMdiSubWindow()
sub_win.setWidget(Module.moduleWindow())
self.mdi.addSubWindow(sub_win)
# this part reports error saying:
# 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'
Module.moduleWindow.my_signal.connect(self.do_something)
def do_something(self):
pass
Module.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MyGUI import module_window_GUI
class moduleWindow(QMainWindow, module_window_GUI.Ui_module_window):
my_signal = pyqtSignal()
def __init__(self):
QMainWindow.__init__(self)
module_window_GUI.Ui_module_window.__init__(self)
self.setupUI(self)
# the rest is not important
# what is important is th following function
def closeEvent(self, event):
# when the user closes this subwindow signal needs to
# be emitted so that the MainWindow class knows that
# it's been closed.
self.my_signal.emit()
event.accept()
Any kind of help is more than welcome. Thanks in advance.
You need to connect the signal from an instance of the moduleWindow class, and not from the class itself:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MyGUI import main_window_GUI
from Modules import Module
class MainWindow(QMainWindow, main_window_GUI.Ui_main_window):
def __init__(self):
QMainWindow.__init__(self)
main_window_GUI.Ui_main_window.__init__(self)
self.setupUI(self)
sub_win = QMdiSubWindow()
module_window = Module.moduleWindow()
sub_win.setWidget(module_window)
self.mdi.addSubWindow(sub_win)
module_window.my_signal.connect(self.do_something)
#pyqtSlot()
def do_something(self):
pass
I would also recommend to decorate the do_something method with pyqtSlot as reported in the documentation