I am basically building a GUI with pyqt5 supposed to incorporate two videos. To do this, I use QMediaPlayer in combination with QVideoWidget, one for each class. The point is: while the first video plays as expected, the second one refuses to play. It uses exactly the same framework as the first one (one pushbuton for play/pause and one slidebar), and the same structure of code, but the screen remains desperately black when trying to play.
Worse, if I comment the code for the first video, the second now plays normally. Could that mean there is some conflict between the two QMedialPlayers? I can't make sense of that.
Any help would be greatly appreciated.
Here is my code (the GUI looks weird because I have removed most of it for clarity):
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QPushButton, QLineEdit, QFrame, QHBoxLayout, QCheckBox, QRadioButton, QButtonGroup, QStyle, QSlider, QStackedLayout
import sys
from tkinter import Tk
from PyQt5.QtCore import pyqtSlot, QRect, Qt, QRunnable, QThreadPool, QThread, QObject, QUrl, QSize
import time
from PyQt5 import QtMultimedia
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QImage, QPalette, QBrush, QIcon, QPixmap
class DNN_Viewer(QWidget):
def __init__(self, n_filters=2):
super(DNN_Viewer, self).__init__()
# initialise GUI
self.init_gui()
# initialise videos to display images
self.mp1.play()
self.mp1.pause()
self.mp2.play()
self.mp2.pause()
def init_gui(self):
# main window
root = Tk()
screen_width = root.winfo_screenwidth() # screen width
screen_height = root.winfo_screenheight() # screen heigth
self.width = 1900 # interface width
self.heigth = 1000 # interface height
self.left = (screen_width - self.width) / 2 # left-center interface
self.top = (screen_height - self.heigth) / 2 # top-center interface
self.setFixedSize(self.width, self.heigth)
self.move(self.left, self.top)
self.setStyleSheet("background: white"); # interface background color
# bottom left frame
self.fm2 = QFrame(self) # creation
self.fm2.setGeometry(30, 550, 850, 430) # left, top, width, height
self.fm2.setFrameShape(QFrame.Panel); # use panel style for frame
self.fm2.setLineWidth(1) # frame line width
# video for weights and gradients
self.vw1 = QVideoWidget(self) # declare video widget
self.vw1.move(50,555) # left, top
self.vw1.resize(542,380) # width, height
self.vw1.setStyleSheet("background-color:black;"); # set black background
# wrapper for the video
self.mp1 = QMediaPlayer(self) # declare QMediaPlayer
self.mp1.setVideoOutput(self.vw1) # use video widget vw1 as output
fileName = "path_to_video_1" # local path to video
self.mp1.setMedia(QMediaContent(QUrl.fromLocalFile(fileName))) # path to video
self.mp1.stateChanged.connect(self.cb_mp1_1) # callback on change state (play, pause, stop)
self.mp1.positionChanged.connect(self.cb_mp1_2) # callback to move slider cursor
self.mp1.durationChanged.connect(self.cb_mp1_3) # callback to update slider range
# play button for video
self.pb2 = QPushButton(self) # creation
self.pb2.move(50,940) # left, top
self.pb2.resize(40,30) # width, height
self.pb2.setIconSize(QSize(18,18)) # button text
self.pb2.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) # standard triangle icon for play
self.pb2.clicked.connect(self.cb_pb2) # callback on click (play/pause)
# position slider for video
self.sld1 = QSlider(Qt.Horizontal,self) # creation
self.sld1.setGeometry( 110, 940, 482, 30) # left, top, width, height
self.sld1.sliderMoved.connect(self.cb_sld1) # callback on move
# title label
self.lb23 = QLabel(self) # creation
self.lb23.setText("Loss and accuracy") # label text
self.lb23.move(980,10) # left, top
self.lb23.setStyleSheet("font-size: 30px; font-family: \
FreeSans; font-weight: bold") # set font and size
# top right frame
self.fm3 = QFrame(self) # creation
self.fm3.setGeometry(980, 50, 850, 430) # left, top, width, height
self.fm3.setFrameShape(QFrame.Panel); # use panel style for frame
self.fm3.setLineWidth(1) # frame line width
# video for loss and accuracy
self.vw2 = QVideoWidget(self) # declare video widget
self.vw2.move(1000,55) # left, top
self.vw2.resize(542,380) # width, height
self.vw2.setStyleSheet("background-color:black;"); # set black background
# wrapper for the video
self.mp2 = QMediaPlayer(self) # declare QMediaPlayer
self.mp2.setVideoOutput(self.vw2) # use video widget vw1 as output
fileName2 = "path_to_video_2" # local path to video
self.mp2.setMedia(QMediaContent(QUrl.fromLocalFile(fileName2))) # path to video
self.mp2.stateChanged.connect(self.cb_mp2_1) # callback on change state (play, pause, stop)
self.mp2.positionChanged.connect(self.cb_mp2_2) # callback to move slider cursor
self.mp2.durationChanged.connect(self.cb_mp2_3) # callback to update slider range
# play button for video
self.pb3 = QPushButton(self) # creation
self.pb3.move(1000,440) # left, top
self.pb3.resize(40,30) # width, height
self.pb3.setIconSize(QSize(18,18)) # button text
self.pb3.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) # standard triangle icon for play
self.pb3.clicked.connect(self.cb_pb3) # callback on click (play/pause)
# position slider for video
self.sld2 = QSlider(Qt.Horizontal,self) # creation
self.sld2.setGeometry(1060, 440, 482, 30) # left, top, width, height
self.sld2.sliderMoved.connect(self.cb_sld2) # callback on move
def cb_mp1_1(self, state):
if self.mp1.state() == QMediaPlayer.PlayingState: # if playing, switch button icon to pause
self.pb2.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
elif self.mp1.state() == QMediaPlayer.StoppedState: # if stopped, rewind to first image
self.mp1.play()
self.mp1.pause()
else:
self.pb2.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) # if paused, switch button icon to play
def cb_mp1_2(self, position):
self.sld1.setValue(position) # set slider position to video position
def cb_mp1_3(self, duration):
self.sld1.setRange(0, duration) # set slider range to video position
def cb_pb2(self):
if self.mp1.state() == QMediaPlayer.PlayingState: # set to pause if playing
self.mp1.pause()
else:
self.mp1.play() # set to play if in pause
def cb_sld1(self, position):
self.mp1.setPosition(position) # set video position to slider position
def cb_mp2_1(self, state):
if self.mp2.state() == QMediaPlayer.PlayingState: # if playing, switch button icon to pause
self.pb3.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
elif self.mp2.state() == QMediaPlayer.StoppedState: # if stopped, rewind to first image
self.mp2.play()
self.mp2.pause()
else:
self.pb3.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) # if paused, switch button icon to play
def cb_mp2_2(self, position):
self.sld2.setValue(position) # set slider position to video position
def cb_mp2_3(self, duration):
self.sld2.setRange(0, duration) # set slider range to video position
def cb_pb3(self):
if self.mp2.state() == QMediaPlayer.PlayingState: # set to pause if playing
self.mp2.pause()
else:
self.mp2.play() # set to play if in pause
def cb_sld2(self, position):
self.mp2.setPosition(position) # set video position to slider position
# run GUI
def dnn_viewer():
app = QApplication(sys.argv) # initiate app; sys.argv argument is only for OS-specific settings
viewer = DNN_Viewer() # create instance of Fil_Rouge_Dashboard class
viewer.show() # display dashboard
sys.exit(app.exec_()) # allow exit of the figure by clicking on the top right cross
# call window function
dnn_viewer()
tl:dr;
Use layout managers.
Explanation
Well, it seems you accidentally found a (possible) bug by doing something really wrong.
QVideoWidget is a widget that is more complex than it seems, since it interfaces itself with the underlying graphics system of the OS, and, in order to correctly show its contents (the video), it has to be actively notified of its geometry.
Simply speaking, QVideoWidget does not directly show the "pictures" of the video QMediaPlayer shows, but tells the Operating System to do so (well, not exactly, but we won't discuss it here). This is because video displaying might take advantage of some hardware acceleration, or require some processing (for example, for HDR videos), similarly to what 3D/OpenGL graphics does.
When a program is going to display some (system managed) video, it has to tell the OS about the available geometry for that video, so that the OS is able to show it at the correct coordinates, and possibly apply resizing, some form of "clipping" (if another window is overlayed, for example) or any other level of [post]processing.
The "something really wrong" I was talking about before is based on the fact that you are using fixed geometries (sizes and positions) for both video widgets, and I think that Qt is not able to notify the system about those geometries for more than a widget at once if that happens before the video window is actually mapped (as in "shown").
Why is it really wrong, besides the issue at hand?
Each one of our devices is mostly unique: what you see on your device will be shown in a (possibly radically) different way on other's.
There are many reasons for that, including:
Operating System (and versions) and its behavior;
screen size and DPI (for example, I wasn't able to view the complete window of your code, since I've a smaller screen);
default/customized system font size; most importantly, if the default font is very big, the widgets might overlap;
further customization (for example, default margins and spacing);
if the interface is "adaptive", the user should be able to resize the interface:
if the user has a smaller screen, the ui should be resizable, so that everything is visible instead of having the need to move the window beyond the screen margins (something that is sometimes impossible: for example on Windows you can't move a window above the top margin of the screen);
if the user has a bigger screen (or uses a very high DPI setting), the interface would be too small and some elements might be hard to read or interact with;
That's the reason for which almost any nowadays website uses "responsive" layouts, which adapt the contents according to the screen of the device they're going to be displayed into.
The solution is very simple, and will also solve the big issue about your GUI: avoid any fixed geometry for your GUI and use layout managers instead.
Note that you can still use fixed sizes (not positions, sizes!): that's not that big of an issue, but using layout managers will help you a lot with it, by repositioning all elements according to the available space.
The reason for it is that layout managers ensure that any resizing operation (something that also happens many times as soon as a window is shown the first time) is also notified to the system, whenever it's required (like, for instance, adapting the QVideoWidget output).
If you want to keep the "bottom-right/top-left" layout, you can still do that:
set a main QGridLayout for the widget (DNN_Viewer), create another grid layout for each player and add those layout to the main one.
The structure will be something like this:
+------------------------- DNN_Viewer -------------------------+
| | +------ player2Layout ------+ |
| | | | |
| | | vw2 | |
| | | | |
| | +-------+-------------------+ |
| | | pb2 | sld1 | |
| | +-------+-------------------+ |
+------------------------------+-------------------------------+
| +------ player1Layout------+ | |
| | | | |
| | vw1 | | |
| | | | |
| +-------+------------------+ | |
| | pb1 | sld2 | | |
| +-------+------------------+ | |
+------------------------------+-------------------------------+
class DNN_Viewer(QWidget):
# ...
def init_gui(self):
# create a grid layout for the widget and automatically set it for it
layout = QtWidgets.QGridLayout(self)
player1Layout = QtWidgets.QGridLayout()
# add the layout to the second row, at the first column
layout.addLayout(player1Layout, 1, 0)
# video for weights and gradients
self.vw1 = QVideoWidget(self)
# add the video widget at the first row and column, but set its column
# span to 2: we'll need to add two widgets in the second row, the play
# button and the slider
player1Layout.addWidget(self.vw1, 0, 0, 1, 2)
# ...
self.pb2 = QPushButton(self)
# add the button to the layout; if you don't specify rows and columns it
# normally means that the widget is added to a new grid row
player1Layout.addWidget(self.pb2)
# ...
self.sld1 = QSlider(Qt.Horizontal,self)
# add the slider to the second row, besides the button
player1Layout.addWidget(self.sld1, 1, 1)
# ...
player2Layout = QtWidgets.QGridLayout()
# add the second player layout to the first row, second column
layout.addLayout(player2Layout, 0, 1)
self.vw2 = QVideoWidget(self)
# same column span as before
player2Layout.addWidget(self.vw2, 0, 0, 1, 2)
# ...
self.pb3 = QPushButton(self)
player2Layout.addWidget(self.pb3, 1, 0)
# ...
self.sld2 = QSlider(Qt.Horizontal,self)
player2Layout.addWidget(self.sld2, 1, 1)
This will solve your main issue (and lots of others you didn't consider).
Some further suggestions:
use more descriptive variable names; things like pb2 or lb23 seem easier to use and you might be led to think that short variables equals less time spent typing. Actually, there's no final benefit in that: while it may be true that shorter variable names might improve compiling speed (especially for interpreted languages like Python), at the end there's almost no advantage; on the contrary, you'll have to remember what "sld2" means, while something like "player2Slider" is way more descriptive and easier to read (which means you'll read and debug faster, and people reading your code will understand it and help you much more easily)
for the same reason above, use more descriptive function names: names like cb_mp1_3 mean literally nothing; naming is really important, and the startup speed improvement reported above is almost dismissible with todays computers; it also helps you to get help from others: it took more time to understand what was your actual issue, than to understand what your code does, since all those names were almost meaningless to me; read more on the official Style Guide for Python Code (aka, PEP 8);
use comments wisely:
avoid over-commenting, it makes comments distracting while losing much of their purpose (that said, while "Let the code be the documentation" is a good concept, don't overextimate it)
avoid "fancy" formatted comments: they might seem cool, but at the end they are just annoying to deal with; if you want to comment a function to better describe what it does, use the triple quotes feature Python already provides; also consider that many code sharing services have column limits (and StackOverflow is amongst them): people would need to scroll each line to read the corresponding comment;
if you need a description for a single line function, it's possible that the function is not descriptive as it could or should be, as explained above;
be more consistent with blank lines separations between functions or classes: Python was created with readability in mind, and it's a good thing to follow that principle;
don't overwrite existing attribute names: self.width() and self.height() are base properties of all QWidgets, and you might need to access them often;
be more consistent with the imports you're using, especially with complex modules like Qt: you should either import the submodules (from PyQt5 import QtWidgets, ...) or the single classes (from PyQt5.QtWidgets import QApplication, ...); note that, while the latter could be considered more "pythonic", it's usually tricky with Qt, since it has hundreds of classes (and you might need tens of them in each script), then you always have to remember to add every class each time you need it, and you might end up importing unnecessary classes you're not using anymore; there's not much performance improvement using this approach, at least with Qt, especially if you forget to remove unnecessary imports (in your case, the possible benefit of importing single classes is completely canceled by the fact that there are at least 10 imported classes that are never actually used);
avoid unnecessary imports from other frameworks if they are not absolutely necessary: if you need to know the screen geometry, use QApplication.screens(), don't import Tk just for that;
So, I started again trying to solve the issue with musicamant's very exhaustive answer. It indeed solved the issue, but I was not happy with having a solution that would work only with adaptative GUIs. So I investigated again the issue, starting with a minimal GUI where only the two videos would be present. And, to my greatest amazement, the two videos played fine, even with a fixed size GUI.
So I started inflating the GUI again, adding all the elements till I recovered my initial GUI. And at some point, I experienced the bug again, which made it possible to identify the actual cause.
So the culprit is called... QFrame. Yes, for real. The Qframe caused all that mess. At first I was using a QFrame with setFrameShape(QFrame.Panel), so that a rectangular frame is created at once. Then I installed the video widget inside the frame. It turns out that with certain videos, the QFrame adopts a strange behaviours and kind of "covers" the video output, making the video viewer screen vanish. The sound remains unaffected. It only happens for certain videos and not for others, which does not make any real sense. Still, removing the frame instantaneously solves the issue, so that really is a bug.
It seems that with musicamante's solution, the frame does not adopt this strange behaviour, hence a working solution. Another possible solution with fixed size GUIs is to use frames that don't cover the video. Concretely, rather than using a single QFrame with setFrameShape(QFrame.Panel) which creates a rectangle in one frame, a set of four frames must be used, two of them being QFrame with setFrameShape(QFrame.Hline), and the other two being QFrame with setFrameShape(QFrame.Vline), organised to form a rectangle. I tested it and it works. The frames only cover the horizontal/vertical surfaces they go through, and so the "inside" of the rectangle is not part of any frame, which avoids the bug.
I display images with Qlabel.I need image coordinates/pixel coordinates but, I use mouseclickevent its show me only Qlabel coordinates.
for examples my image is 800*753 and my Qlabel geometry is (701,451).I reads coordinates in (701,451) but I need image coordinates in (800*753)
def resimac(self):
filename= QtWidgets.QFileDialog.getOpenFileName(None, 'Resim Yükle', '.', 'Image Files (*.png *.jpg *.jpeg *.bmp *.tif)')
self.image=QtGui.QImage(filename[0])
self.pixmap=QtGui.QPixmap.fromImage(self.image)
self.resim1.setPixmap(self.pixmap)
self.resim1.mousePressEvent=self.getPixel
def getPixel(self, event):
x = event.pos().x()
y = event.pos().y()
print("X=",x," y= ",y)
Since you didn't provide a minimal, reproducible example, I'm going to assume that you're probably setting the scaledContents property, but that could also be not true (in case you set a maximum or fixed size for the label).
There are some other serious issues about your answer, I'll address them at the end of this answer.
The point has to be mapped to the pixmap coordinates
When setting a pixmap to a QLabel, Qt automatically resizes the label to its contents.
Well, it does it unless the label has some size constrains: a maximum/fixed size that is smaller than the pixmap, and/or the QLabel has the scaledContents property set to True as written above. Note that this also happens if any of its ancestors has some size constraints (for example, the main window has a maximum size, or it's maximized to a screen smaller than the space the window needs).
In any of those cases, the mousePressEvent will obviously give you the coordinates based on the widget, not on the pixmap.
First of all, even if it doesn't seem to be that important, you'll have to consider that every widget can have some contents margins: the widget will still receive events that happen inside the area of those margins, even if they are outside its actual contents, so you'll have to consider that aspect, and ensure that the event happens within the real geometry of the widget contents (in this case, the pixmap). If that's true, you'll have to translate the event position to that rectangle to get its position according to the pixmap.
Then, if the scaledContents property is true, the image will be scaled to the current available size of the label (which also means that its aspect ratio will not be maintained), so you'll need to scale the position.
This is just a matter of math: compute the proportion between the image size and the (contents of the) label, then multiply the value using that proportion.
# click on the horizontal center of the widget
mouseX = 100
pixmapWidth = 400
widgetWidth = 200
xRatio = pixmapWidth / widgetWidth
# xRatio = 2.0
pixmapX = mouseX * xRatio
# the resulting "x" is the horizontal center of the pixmap
# pixmapX = 200
On the other hand, if the contents are not scaled you'll have to consider the QLabel alignment property; it is usually aligned on the left and vertically centered, but that depends on the OS, the style currently in use and the localization (consider right-to-left writing languages). This means that if the image is smaller than the available size, there will be some empty space within its margins, and you'll have to be aware of that.
In the following example I'm trying to take care about all of that (I'd have to be honest, I'm not 100% sure, as there might be some 1-pixel tolerance due to various reasons, most regarding integer-based coordinates and DPI awareness).
Note that instead of overwriting mousePressEvent as you did, I'm using an event filter, I'll explain the reason for it afterwards.
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
layout = QtWidgets.QGridLayout(self)
self.getImageButton = QtWidgets.QPushButton('Select')
layout.addWidget(self.getImageButton)
self.getImageButton.clicked.connect(self.resimac)
self.resim1 = QtWidgets.QLabel()
layout.addWidget(self.resim1)
self.resim1.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter)
# I'm assuming the following...
self.resim1.setScaledContents(True)
self.resim1.setFixedSize(701,451)
# install an event filter to "capture" mouse events (amongst others)
self.resim1.installEventFilter(self)
def resimac(self):
filename, filter = QtWidgets.QFileDialog.getOpenFileName(None, 'Resim Yükle', '.', 'Image Files (*.png *.jpg *.jpeg *.bmp *.tif)')
if not filename:
return
self.resim1.setPixmap(QtGui.QPixmap(filename))
def eventFilter(self, source, event):
# if the source is our QLabel, it has a valid pixmap, and the event is
# a left click, proceed in trying to get the event position
if (source == self.resim1 and source.pixmap() and not source.pixmap().isNull() and
event.type() == QtCore.QEvent.MouseButtonPress and
event.button() == QtCore.Qt.LeftButton):
self.getClickedPosition(event.pos())
return super().eventFilter(source, event)
def getClickedPosition(self, pos):
# consider the widget contents margins
contentsRect = QtCore.QRectF(self.resim1.contentsRect())
if pos not in contentsRect:
# outside widget margins, ignore!
return
# adjust the position to the contents margins
pos -= contentsRect.topLeft()
pixmapRect = self.resim1.pixmap().rect()
if self.resim1.hasScaledContents():
x = pos.x() * pixmapRect.width() / contentsRect.width()
y = pos.y() * pixmapRect.height() / contentsRect.height()
pos = QtCore.QPoint(x, y)
else:
align = self.resim1.alignment()
# for historical reasons, QRect (which is based on integer values),
# returns right() as (left+width-1) and bottom as (top+height-1),
# and so their opposite functions set/moveRight and set/moveBottom
# take that into consideration; using a QRectF can prevent that; see:
# https://doc.qt.io/qt-5/qrect.html#right
# https://doc.qt.io/qt-5/qrect.html#bottom
pixmapRect = QtCore.QRectF(pixmapRect)
# the pixmap is not left aligned, align it correctly
if align & QtCore.Qt.AlignRight:
pixmapRect.moveRight(contentsRect.x() + contentsRect.width())
elif align & QtCore.Qt.AlignHCenter:
pixmapRect.moveLeft(contentsRect.center().x() - pixmapRect.width() / 2)
# the pixmap is not top aligned (note that the default for QLabel is
# Qt.AlignVCenter, the vertical center)
if align & QtCore.Qt.AlignBottom:
pixmapRect.moveBottom(contentsRect.y() + contentsRect.height())
elif align & QtCore.Qt.AlignVCenter:
pixmapRect.moveTop(contentsRect.center().y() - pixmapRect.height() / 2)
if not pos in pixmapRect:
# outside image margins, ignore!
return
# translate coordinates to the image position and convert it back to
# a QPoint, which is integer based
pos = (pos - pixmapRect.topLeft()).toPoint()
print('X={}, Y={}'.format(pos.x(), pos.y()))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Now. A couple of suggestions.
Don't overwrite existing child object methods with [other] object's instance attributes
There are various reasons for which this is not a good idea, and, while dealing with Qt, the most important of them is that Qt uses function caching for virtual functions; this means that as soon as a virtual is called the first time, that function will always be called in the future. While your approach could work in simple cases (especially if the overwriting happens within the parent's __init__), it's usually prone to unexpected behavior that's difficult to debug if you're not very careful.
And that's exactly your case: I suppose that resimac is not called upon parent instantiation and until after some other event (possibly a clicked button) happens. But if the user, for some reason, clicks on the label before a new pixmap is loaded, your supposedly overwritten method will never get called: at that time, you've not overwritten it yet, so the user clicks the label, Qt calls the QLabel's base class mousePressEvent implementation, and then that method will always be called from that point on, no matter if you try to overwrite it.
To work around that, you have at least 3 options:
use an event filter (as the example above); an event filter is something that "captures" events of a widgets and allows you to observe (and interact) with it; you can also decide to propagate that event to the widget's parent or not (that's mostly the case of key/mouse events: if a widget isn't "interested" about one of those events, it "tells" its parent to care about it); this is the simplest method, but it can become hard to implement and debug for complex cases;
subclass the widget and manually add it to your GUI within your code;
subclass it and "promote" the widget if you're using Qt's Designer;
You don't need to use a QImage for a QLabel.
This is not that an issue, it's just a suggestion: QPixmap already uses (sort of) fromImage within its C++ code when constructing it with a path as an argument, so there's no need for that.
Always, always provide usable, Minimal Reproducible Example code.
See:
https://stackoverflow.com/help/how-to-ask
https://stackoverflow.com/help/minimal-reproducible-example
It could take time, even hours to get an "MRE", but it's worth it: there'll always somebody that could answer you, but doesn't want to or couldn't dig into your code for various reasons (mostly because it's incomplete, vague, inusable, lacking context, or even too expanded). If, for any reason, there'll be just that one user, you'll be losing your occasion to solve your problem. Be patient, carefully prepare your questions, and you'll probably get plenty of interactions and useful insight from it.
Important note: I'm using PyGObject to get access to the GTK widgets, not PyGTK. That's what makes this question different from similar ones:
PyGTK: How do I make an image automatically scale to fit it's parent widget?
Scale an image in GTK
I want to make a very simple app that displays a label, an image and a button, all stacked on top of each other. The app should be running in fullscreen mode.
When I attempted it, I've run into a problem. My image is of very high resolution, so when I simply create it from file and add it, I can barely see 20% of it.
What I want is for this image to be scaled by width according to the size of the window (which is equal to the screen size as the app runs in fullscreen).
I've tried using Pixbufs, but calling scale_simple on them didn't seem to change much.
Here's my current code:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf
class Window(Gtk.Window):
def __init__(self):
super().__init__(title='My app')
layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
dimensions = layout.get_allocation()
pixbuf = GdkPixbuf.Pixbuf.new_from_file('path/to/image')
pixbuf.scale_simple(dimensions.width, dimensions.height, GdkPixbuf.InterpType.BILINEAR)
image = Gtk.Image.new_from_pixbuf(pixbuf)
dismiss_btn = Gtk.Button(label='Button')
dismiss_btn.connect('clicked', Gtk.main_quit)
layout.add(image)
layout.add(dismiss_btn)
self.add(layout)
win = Window()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
The problem is that scale_simple actually returns a new Pixbuf, as per GTK+ docs.
Getting screen dimensions can be done by calling .get_screen() on the window and then calling .width() or .height() on the screen object.
The whole code put together looks something like this:
screen = self.get_screen()
pixbuf = GdkPixbuf.Pixbuf.new_from_file('/path/to/image')
pixbuf = pixbuf.scale_simple(screen.width(), screen.height() * 0.9, GdkPixbuf.InterpType.BILINEAR)
image = Gtk.Image.new_from_pixbuf(pixbuf)
How can I give a widget a fixed position? Like so I can "attach"/put it at the bottom of the window and it will always be there; even when the window is expanded. I couldn't find anything useful on how to do it and, I suppose obviously, none of the obvious things work (resize(), setGeometry(), etc.). Any help?
I assume by "fixed position" you mean a position relative to one of the window edges. That's what your second sentence implies. So that's the question I will answer.
Use a layout manager with stretches and spacings. Here's a simple example to attach a widget "w" to the bottom of a window "win". This code typically gets called by (or goes inside) your window's constructor.
lay = QVBoxLayout(win)
lay.addStretch(1)
lay.addWidget(w)
The BoxLayout makes "w" stick to the bottom of the window and stay in that position as the window is resized.
you must reimplement the parent windows resizeEvent function, here is the code to make a widget "attached" to the bottom of the window:
def resizeEvent(self, event):
#widget.move(x, y)
self.bottom_widget.move(0, self.height() - self.bottom_widget.height())
#if you want the widgets width equal to window width:
self.bottom_widget.setWidth(self.width())
whenever the window is resized, this function will be called and it will move the widget to the bottom of the window. this is an absolute positioning approach, but you can always use QSpacerItem to push your widget to the bottom.