Argument 1 has unexpected type 'NoneType'? - python

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"))

Related

How do I execute a function using Qtoolbar buttons?

I have tried triggered, actionTriggered and many more and yet it always throws an attribute error, like 'NoneType' object has no attribute 'actionTriggered'. I use a .UI file that i created in QTDesigner.
from PyQt5.QtWidgets import QMainWindow, QApplication, QToolBar, QAction
from PyQt5 import uic
import sys
class UI(QMainWindow):
def __init__(self):
super(UI,self).__init__()
uic.loadUi("TTTT.ui",self)
self.show()
self.tbar=self.findChild(QToolBar,"actionfff")
self.tbar.actionTriggered.connect(self.T)
def T(self):
print("mmmmm")
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()
It's a test program, because I thought I messed up in the original code.
Any help will be mutch appreciated!

Qt.FramelessWindowHint not working on QInputDialog

I'm learning PySide6 and I'm trying to create a frameless QInputDialog.
When I set:
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QInputDialog, QMainWindow
app = QApplication()
input_dialog = QInputDialog(flags=Qt.FramelessWindowHint)
text, ok = input_dialog.getText(QMainWindow(), 'input dialog', 'Is this ok?')
if ok:
print(text)
app.exec()
the frame still appears. Why?
The getText method is static so input_dialog is not the displayed window but an instance of QInputDialog is created internally, so you must pass the flags through the method:
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QInputDialog
app = QApplication()
text, ok = QInputDialog.getText(
None, "input dialog", "Is this ok?", flags=Qt.FramelessWindowHint
)
if ok:
print(text)

Icon not showing in QListWidgetItem

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?

i can't rebuild because of some error in python?

I don't know what happen my code.
I can't rebuild some error.
import os
from PyQt5.QtWidgets import QWidget, QApplication, QToolTip, QPushButton, QMainWindow
from time import strftime
Unused import ospylint(unused-import)
Undefined variable: 'QtWidgets'Python(undefined-variable)

pyqt5 - How to add value to QComboBox fromfunction

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)

Categories