PyQt4 Highlighting - python

I'm trying to add some syntax highlighting to a text editor in PyQt4. I've found an example in the documentation which works find when compiled from C++ but when i convert it to Python/PyQt it no longer works.
The part of the code that fails (no longer highlights anything) is:
def highlightCurrentLine(self):
extraSelections = []
if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
lineColor = QColor(Qt.yellow).lighter(160)
selection.format.setBackground(lineColor)
selection.format.setProperty(QTextFormat.FullWidthSelection, QVariant(True))
selection.cursor = self.textCursor()
selection.cursor.clearSelection()
extraSelections.append(selection)
self.setExtraSelections(extraSelections)
which is called by:
self.connect(self, SIGNAL('cursorPositionChanged()'), self.highlightCurrentLine)
Anyone have any idea why this doesn't work?
The versions i am usuing are: Python 2.6.2, PyQt 4.4.4

Ok... turns out i wasn't going mad, i was just using an out of date version of PyQt4.
For information the version of PyQt4 that ships with Ubuntu 9.04 is 4.4.4 but this functionality seems to require 4.5+.
I've upgraded to PyQt4 4.6 and it works fine (plus 4.6 seems to have some nice new functionality too).

Save lineColor somewhere (like self.lineColor). Otherwise, Python will discard the object when the method returns and the format will use an illegal pointer.

Related

HTML not rendering properly with Canopy 1.7.1.3323 / IPython 4.1.2

I've just upgraded to Canopy 1.7.1; I think this problem stems from the change in IPython version from 2.4.1 to 4.1.2.
The issue I have is that calling a DataFrame object in Python seems to use the __print__ method, i.e. there's no difference between typing print df and df into the interpreter, and unfortunately this gives me an all-text output rather than the nice tables I normally get.
So I get something that looks exactly like this when I call df rather than a table:
date flag
1 20151102 0
98663 20151101 1
This happened immediately after the upgrade, and I also tried updating all my packages. I've also looked at this and this, but none of the solutions there work for me. ('display.notebook_repr_html' is already True)
EDIT: The issue seems to do with rendering HTML; typing in
from IPython.core.display import display, HTML
display(HTML('<h1>Hello, world!</h1>'))
returns
<IPython.core.display.HTML object>
This has purposely been disabled. I have requested a way to have it re-enabled but but unsupported.
Please see the request. https://github.com/jupyter/qtconsole/issues/165

No module named objects [bokeh]

Note from maintainers: This question is no longer relevant. The bokeh.objects module has not existed for years
I'm trying to run this script:
#Code will be significantly simplified in the 0.4 release
import time
from bokeh.objects import GlyphRenderer
renderer = [r for r in curplot().renderers if isinstance(r, GlyphRenderer)][0]
ds = renderer.data_source
while True:
df = pd.io.json.read_json(url+json_call)
ds.data["x"] = x+N*i
ds.data["y"] = df.rssi
ds._dirty = True
session().store_obj(ds)
time.sleep(1.5)
i+=1
from:
https://www.continuum.io/content/painless-streaming-plots-bokeh
but at this line:
from bokeh.objects import GlyphRenderer
I got:
No module named objects
The version I'm using is
0.11.1
On linux mint 17.1
Note from maintainers: This answer is no longer relevant. The bokeh.objects module has not existed for years
did you try installing bokeh before trying the examples? If not, just run:
pip install bokeh
and try your script again.
if it does not work, it's likely that the bokeh sources changed, so you might want to change the
from bokeh.objects import GlyphRenderer
into
from bokeh.models.renderers import GlyphRenderer
cf the source code
At the first line of your example it states:
#Code will be significantly simplified in the 0.4 release
which means that the example's code was already about to be deprecated at the time of the writing of the tutorial.
So instead of copy/pasting that code, you should try to understand how it works, and recreate it using the documentation and sources:
http://docs.bokeh.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart
https://github.com/bokeh/bokeh
have fun!
The objects module was deleted in commit 5b5d28304c5ea209e243af5943917fe494d9ef9c (v0.7.1) after being deprecated in 8bb4a2f1f43b39b869c508ef7aee69f7aabb46b8 (v0.7.0). The deprecation message reads: "use bokeh.models instead". I leave finding GlyphRenderer in the current codebase as an exercise for you.
conda update bokeh solved this for me.
Regarding streaming (since that is is the example the OP was interested in), the current and stable streaming API is demonstrated here:
https://github.com/bokeh/bokeh/tree/master/examples/app/ohlc
This simple interface has been the way to efficiently stream to data soruces since 0.11.1, and will continue to be going forward.
The basic idea is to construct a dict with all the columns of a data source, and just the new data that is to be appended:
# construct a source with x and y columns
source = ColumnDataSource(data=dict(x=[....], y=[....]))
# sends the few new data points to the client, does not re-send all the data
source.stream(dict(x=[10, 11], y=[20, 21]))
Typically you'd probably call stream from some kind of periodic callback. The OHLC app linked above is a complete example.

Qt Clipboard operation does not work in tests on OSX

I have a widget where the mac shortcut Command+C works well interactively, and the content is correctly stored in the clipboard.
I am trying to implement the same behavior in the unittests. It works on other platforms, but not on Mac.
expected_clipboard = "whatever"
self._widget.show()
Test.processEvents()
QtTest.QTest.keyClick(self._widget, QtCore.Qt.Key_C, QtCore.Qt.ControlModifier)
Test.processEvents()
current_clipboard= QtGui.qApp.clipboard().text()
self.assertEqual(current_clipboard, expected_clipboard) # Fails.
According to the Qt documentation, ControlModifier is the proper corresponding entity for Command on Mac. I also tried MetaModifier just out of curiosity, but it doesn't work.
The action is correctly attached with the following code
self._action = QtGui.QAction('text', self)
action.connect(self._action, QtCore.SIGNAL('triggered()'), self.copyAction)
self._action.setShortcut(QtGui.QKeySequence.Copy)
self.addAction(self._action)
Qt 4.8 latest, osx 10.8.
Ok, the problem is that the window must be visible, raised and activated, otherwise OSX does not deliver events. Just call widget.raise_() and widget.activateWindow()

How I can get DrawingArea window handle in Gtk3?

I get this code on CEF Python 3 (link)
...
self.container = gtk.DrawingArea()
self.container.set_property('can-focus', True)
self.container.connect('size-allocate', self.OnSize)
self.container.show()
...
windowID = self.container.get_window().handle
windowInfo = cefpython.WindowInfo()
windowInfo.SetAsChild(windowID)
self.browser = cefpython.CreateBrowserSync(windowInfo,
browserSettings={},
navigateUrl=GetApplicationPath('example.html'))
...
This code [self.container.get_window().handle] don't work with PyGI and GTK3.
I trying port the code from GTK2 to GTK3, how I can do this?
Edited:
After some search, I found a tip to make get_window work: I call: self.container.realize() before self.container.get_window(). But I cant't get Window Handle yet.
I need put CEF3 window inside a DrawingArea or any element. How I can do this with PyGI?
Edited:
My environment is:
Windows 7
Python 2.7 and Python 3.2
Sadly there seems to be no progress on the python gobject introspection to fix this and make gdk_win32_window_get_handle available (reported a bug in the gnome bugtracker quite a while ago) - it is also quite needed for Python GStreamer and Windows ...
So I followed the suggestion of totaam and used ctypes to access gdk_win32_window_get_handle. Took me forever since I had no experience with this - and well it is somehow quite an ugly hack - but well when needed...
Here is the code:
Gdk.threads_enter()
#get the gdk window and the corresponding c gpointer
drawingareawnd = drawingarea.get_property("window")
#make sure to call ensure_native before e.g. on realize
if not drawingareawnd.has_native():
print("Your window is gonna freeze as soon as you move or resize it...")
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object]
drawingarea_gpointer = ctypes.pythonapi.PyCapsule_GetPointer(drawingareawnd.__gpointer__, None)
#get the win32 handle
gdkdll = ctypes.CDLL ("libgdk-3-0.dll")
hnd = gdkdll.gdk_win32_window_get_handle(drawingarea_gpointer)
#do what you want with it ... I pass it to a gstreamer videosink
Gdk.threads_leave()
You must first import GdkX11 for get_xid() to be available on the returned GdkX11Window.
from gi.repository import GdkX11
...
-windowID = self.container.get_window().handle
+windowID = self.container.get_window().get_xid()
The answer advising you to use .handle or .get_xid() works on GTK2, but not with GTK3 or on MS Windows, which are part of your question.
I've done a lot of digging and found that there is a function in GTK3 which does what you want: gdk_win32_window_get_handle, but sadly it is not available in the gi bindings.
You may be able to get to it using ctypes or Cython (which is what I am going to do).

How can I use Python 3.3 to continuously update my MouseSpeed in Windows?

I am developing a python script to drive a prototype I'm developing, and my current stumbling block is attempting to continuously write to the MouseSensitivity register in Windows. I'm using Windows 7, for specificity.
Unfortunately, I've hit some dead ends.
I've attempted to use pywin32 to write to the registry, but have not succeeded. After some searching, I've come across the SystemParametersInfo Function, but have not had any success with that either.
Essentially, for the purpose of solving this crisis, I've installed pyserial, pywin32, and ctypes (it appears that pywin32 may not be required). Assume I have an integer variable that will be between (and including) 1 to 20.
What I have for code right now is listed below. Much of it has been modified from here, as it appears that the author was using python, ctypes, user32.dll, and the SystemParametersInfo Function. However, when I run the code below, the shell enters a loop where the next line gets two tabs of space, and the cursor sits blinking until I keyboard interrupt. Any ideas?
import sys
import ctypes
import ctypes.wintypes
num = 12 # Between 1 and 20
SystemParametersInfo = ctypes.windll.user32.SystemParametersInfoW
SystemParametersInfo.argtypes = (
ctypes.wintypes.UINT,
ctypes.wintypes.UINT,
ctypes.c_void_p,
ctypes.wintypes.UINT,
)
SPI_SETMOUSESPEED = 0x0071
SystemParametersInfo(SPI_SETMOUSESPEED, 0, ctypes.cast(num, ctypes.c_void_p, 0)

Categories