Issue
To create QLabel with QPixmap I'm currently using
label = QLabel()
label.setPixmap(QPixmap("path-to-img"))
So it's 2 lines.But I want one liner code.
I think following code could be an answer.
label = QLabel("<img src=path-toimg></img>")
But I suspect there is more elegant way.
Solution
Okay, after doing some research you can do it but it is considered non-pythonic since we abuse the tuple
(you can do this with list
, dict
or any other object that can store multiple values and you are able to access these in a decent manner). The following code exploits the evaluation order of a tuple. It also makes it possible not only to create a label and assign a QPixmap
to it but also to add it to a layout. As promised all in a single line. :D
layout.addWidget((lambda img, lbl: ((lambda i,l: l.setPixmap(i))(img,lbl), (lambda l: l)(lbl)))(QPixmap('../Pictures/IMAGE.jpg'), QLabel('Lambda'))[1])
The problem with lambda
in Python is that it doesn't allow more than one expression in its body. And expression is also a keyword here - you cannot use statements! What we can do is however use a chain of lambdas. The code above works because of the fact that a lambda always returns a value.
Example and explanation why this works:
>>> x = lambda x,y: (x**y, x+y)
returns a tuple. Each element inside the tuple is evaluated so that the final tuple can be returned by the lambda expression. Here is an example how it works
>>> x(2,3)
(8,5)
The first tuple element 8
is created by evaluating x**y
. The second one - the 5
- is the result of x+y
.
Further we use the fact that we can reference the elements of a tuple and return what's stored at position X. If we take the numeric example from above we get
>>> x(2,3)[0]
8
>>> x(2,3)[1]
5
And this is exactly what we are doing here.
- Outer lambda
(lambda img, lbl: ...)(QPixmap('../Pictures/IMAGE.jpg'), QLabel('Lambda'))[1]
- this lambda receives a newly createdQPixmap
along with a newly createdQLabel
. Since the outer lambda returns a tuple (see below) we can access the second element (see second inner lambda) - First inner lambda
(lambda i,l: l.setPixmap(i))(img,lbl)
- we redirect the pixmap and label objects to it (both are passed as references) and call theQLabel.setPixmap(...)
method - Second inner lambda
(lambda l: l)(lbl)
- this is basically just redirecting theQLabel
object (which has been passed as argument to the outer lambda and setup by the first inner lambda) so that - after accessing the[1]
element of our returned tuple we actually get an object that we can use.
If you want to store the created label you can omit the layout.addWidget(...)
and simply assign the lambda
's result to a variable for further use. If you use [0]
as index you will get a None
since the first inner lambda returns None
.
AGAIN - WARNING! This is - as mentioned above - abusing the way Python works. As suggested in the comment section you should use a normal def
(named function) with a return
statement.
Answered By - rbaleksandar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.