i was wondering how to set a phonon player to full screen?
im trying this codes.
if not self.ui.videoPlayer.isFullScreen():
self.ui.videoPlayer.enterFullScreen()
else:
self.ui.videoPlayer.exitFullScreen()
but i keep on getting this error message
TypeError: 'sip.methoddescriptor' object is not callable
the code above works is from a sample project. the original code was
def full(self):
if not self.videoWidget.isFullScreen():
self.videoWidget.enterFullScreen()
else:
self.videoWidget.exitFullScreen()
im recreating it in PyQT and it seems hard for me.
can anyone please guide me on what im missing(having a hunch about it)
or what im doing wrong?
A VideoPlayer is not the same thing as a VideoWidget.
VideoPlayer is a subclass of QWidget, so it will have an isFullScreen method - but it won't have the methods enterFullScreen and exitFullScreen, which belong to the VideoWidget class.
However, the VideoPlayer class has a videoWidget method which returns the instance of the video widget it uses, so your code example should probably be changed to:
videoWidget = self.ui.videoPlayer.videoWidget()
if videoWidget.isFullScreen():
videoWidget.exitFullScreen()
else:
videoWidget.enterFullScreen()
EDIT
To provide a method for exiting fullscreen mode, set up a keyboard shortcut:
class MainWindow(QtGui.QMainWindow):
def __init__(self)
...
self.shortcutFull = QtGui.QShortcut(self)
self.shortcutFull.setKey(QtGui.QKeySequence('F11'))
self.shortcutFull.setContext(QtCore.Qt.ApplicationShortcut)
self.shortcutFull.activated.connect(self.handleFullScreen)
def handleFullScreen(self):
videoWidget = self.ui.videoPlayer.videoWidget()
if videoWidget.isFullScreen():
videoWidget.exitFullScreen()
else:
videoWidget.enterFullScreen()
I think the problem is your use of self.ui.videoPlayer.isFullScreen, it's probably returning True or False, which when you use self.ui.videoPlayer.isFullScreen() is really resolving down to 'False()'.
Oddly enough, the PyQT documentation doesn't even list 'isFullScreen' as part of the available, methods/properties. However the QWidget documentation does show isFullScreen as returning a boolean.
Instead, try this:
if not self.ui.videoPlayer.isFullScreen:
self.ui.videoPlayer.enterFullScreen()
else:
self.ui.videoPlayer.exitFullScreen()
Related
I have ascertained that making a QAction disabled does not in fact prevent code from being able to run activate() on it, which strikes me as curious. So I want to make a helper subclass:
class DeactivatableAction(QtWidgets.QAction):
def activate(self, event):
if self.isEnabled():
super().activate(event)
This seems to work in an app I'm working on, in practice. Then I wanted to include testing of this functionality (pytest):
#unittest.mock.patch('PyQt5.QtWidgets.QAction.activate')
def test_deactivatable_action_should_only_superactivate_if_enabled(mock_super):
import gen_fmwrk.deactivatable_action as d_action
QtWidgets.QApplication([]) # without this, I get a complaint about "Application not initialized"
da = d_action.DeactivatableAction()
da.setEnabled(False)
# da.setDisabled(True) - NB same effect as previous line
assert not da.isEnabled() # this fails!
da.activate(None)
assert not mock_super.called # this also fails
I realise this is a sort of disembodied way to run PyQt5 code... but I'd still expect to be able to disable a QAction in a pytest context like this. What's going wrong, and is there a solution?
The problem is that QtWidgets.QApplication([]) is not assigned to a variable so it will not be constructed correctly causing unexpected behavior. Change to
app = QtWidgets.QApplication([])
I am reading a sensor and want to display its output as a decimal number in a GUI using PyQt5. I have found a number of tutorials that point out the label.setText('myStr') function. This does not work for my setup, however, because I need to update the field based on the input from another function. I'm not very familiar with PyQt5 yet, and I would appreciate any insight into how this problem ought to be approached.
Note: (I am using LCM to acquire data from a Raspberry Pi. I'm not sure that that is relevant to the problem, but it helps explain my code below.)
Here is what I am trying to do:
class Home_Win(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("sensor_interface.ui", self)
self.label_temp.setText('temperature') #Just to verify that I can change it from here
def acquire_sensors(self):
temp = 0 #Make variable available outside nested function
def listen(channel, data):
msg=sensor_data.decode(data)
temp = msg.temperature
lc = lcm.LCM()
subscription = lc.subscribe("sensor_data_0", listen)
while True:
lc.handle()
self.label_temp.setText(str(temp))
Any thoughts on how I can update the GUI to display the readings I am getting from my sensors?
Thanks!
You're almost there. All you need to do is to save the ui in an instance variable in __init__:
self.ui = loadUi("sensor_interface.ui", self)
Then, assuming label_temp is the name of your QLabel widget, just do:
self.ui.label_temp.setText(str(temp))
It turned out that I needed to add repaint(). I also switched to a QLineEdit as this seemed to work better for me. So inside the while loop I now have:
self.ui.lineEdit_temp.setText(str(temp))
self.ui.lineEdit_temp.repaint()
This now outputs live updates to the GUI while reading the data stream.
From the code below, all I want to know is what is the window used for?
plt.ion()
figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
I went to the matplotlib documentation, searched for get_current_fig_manager() and it retured FigureManagerBase. I then looked at FigureMangerBase, and there's no window attribute, methods, super class is object.
In case you are using the Qt5Agg backend and look at the repr of the figManager.window, it is a matplotlib.backends.backend_qt5.MainWindow object.
Digging into the MPL code, you can find it is just a wrapper object for the QtWidgets.QMainWindow object. Code found on github:
class MainWindow(QtWidgets.QMainWindow):
closing = QtCore.Signal()
def closeEvent(self, event):
self.closing.emit()
QtWidgets.QMainWindow.closeEvent(self, event)
It looks like it just gets tacked on as an undocumented attribute to the FigureManagerBase object as a way to reference the open Qt window. This allows the user to access the Qt windows when in interactive mode without having to import Qt.
Probably should be documented. You can always put in an issue.
I have almost the exact same question as the one found here:
Override shouldInterruptJavaScript in QWebPage with PySide
In my case though I want to override the copy and paste slots on QLineEdit
import sys
from PySide import QtGui, QtCore
class myLineEdit(QtGui.QLineEdit):
# FIXME: This is not working, the slot is not overriden!
#QtCore.Slot()
def copy(self):
print 'overridden copy event'
App.clipboard().setText('customized text')
return False
#QtCore.Slot()
def paste(self):
print 'overridden paste event'
self.setText('customized text')
return False
if __name__ == "__main__":
App = QtGui.QApplication(sys.argv)
Widget = myLineEdit()
Widget.show()
cmenu = Widget.createStandardContextMenu()
sys.exit(App.exec_())
I'm using Python 2.7.3, with PySide 1.2.2
I don't know if these methods are even supposed to be override-able, but I can't find any documentation that says they shouldn't be.
I also found this page
http://qt-project.org/faq/answer/is_it_possible_to_reimplement_non-virtual_slots
The page explains how certain kinds of slots get pointers set to them by functions that get called when the object is initialized (or something along those lines, I'm not as familiar with the C++).
Following this logic I added the createStandardContextMenu() call above in the hopes that it would reinitialize the slots for at least the context menu, but no luck.
Am I doing something wrong? Or should I try filing a bug report?
You cannot override QLineEdit.copy or QLineEdit.paste in such a way that they will be called internally by Qt.
In general, you can only usefully override or reimplement Qt functions that are defined as being virtual. The Qt Docs will always specify whether this is the case, and for QLineEdit, there are no public slots that are defined in that way.
There is also no easy workaround. There are a lot of different ways in which copy and paste operations (or their equivalents) can be invoked, such as keyboard shortcuts, context menu, drag and drop, etc. It can be done: but it's a lot of work to get complete control over all these sorts of operations. So you need to think carefully about what you're trying to achieve before deciding how to proceed.
On a happy (if not irrevelent) note, this is the absolute last obstacle in this particular project. If I fix this, I have my first significant dot release (1.0), and the project will be going public. Thanks to everyone here on SO for helping me through this project, and my other two (the answers help across the board, as they should).
Now, to the actual question...
I have a toolbar in my application (Python 2.7, PyGTK) which has a number of gtk.ToolButton objects on it. These function just fine. I have working "clicked" events tied to them.
However, I need to also connect them to "enter-notify-event" and "leave-notify-event" signals, so I can display the button's functions in the statusbar.
This is the code I have. I am receiving no errors, and yet, the status bar messages are not appearing:
new_tb = gtk.ToolButton(gtk.STOCK_NEW)
toolbar.insert(new_tb, -1)
new_tb.show()
new_tb.connect("clicked", new_event)
new_tb.connect("enter-notify-event", status_push, "Create a new, empty project.")
new_tb.connect("leave-notify-event", status_pop)
I know the issue is not with the "status_push" and "status_pop" events, as I've connected all my gtk.MenuItem objects to them, and they work swimmingly.
I know that gtk.ToolButton objects are in the Widgets class, so "enter-notify-event" and "leave-notify-event" SHOULD technically work. My only guess is that this particular object does not emit any signals other than "clicked", and thus I'd have to put each in a gtk.EventBox.
What am I doing wrong here? How do I fix this?
Thanks in advance!
Your guess was correct, you should wrap your widget in a gtk.EventBox, here is an example that i hope will be hopeful:
import gtk
def callback(widget, event, data):
print event, data
class Win(gtk.Window):
def __init__(self):
super(Win, self).__init__()
self.connect("destroy", gtk.main_quit)
self.set_position(gtk.WIN_POS_CENTER)
self.set_default_size(250, 200)
tb = gtk.ToolButton(gtk.STOCK_NEW)
# Wrap ``gtk.ToolButton`` in an ``gtk.EventBox``.
ev_box = gtk.EventBox()
ev_box.connect("enter-notify-event", callback, "enter")
ev_box.connect("leave-notify-event", callback, "leave")
ev_box.add(tb)
self.add(ev_box)
if __name__ == '__main__':
Win()
gtk.main()
It appears, based on experimentation and evidence, this is impossible in PyGtk 2.24.