PyGTK Hide Cursor
As stated in the PyGTK FAQ, you should set the cursor on the realize
signal. If you don't wait for the realize
signal, the gtk.gdk.window
hasn't been created yet, so you can't change the cursor.
So, you can do something like:
#!/usr/bin/env python
import gtk
class app:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("TestApp")
window.set_default_size(400,200)
window.connect("realize", self.realize_cb)
window.connect("destroy", gtk.main_quit)
window.show_all()
def realize_cb(self, widget):
pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
color = gtk.gdk.Color()
cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
widget.window.set_cursor(cursor)
app()
gtk.main()
Is there a way to deactivate mouse cursor in Gtk window/widget?
Question: deactivate mouse cursor in Gtk window/widget?
It's the same as setting a new/different Cursor
to a widget.
You have to wait for the realize
signal.
If the widget's Gdk.Window
hasn't been created yet, you can't change the cursor.
- Gtk.Widget.get_display
- Gdk.Cursor.new_for_display, Gdk.Cursor.new_from_name
- Gtk.Widget.get_window
- Gdk.Window.set_cursor
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib
class ApplicationWindow(Gtk.ApplicationWindow):
def __init__(self):
super().__init__()
self.connect("destroy", Gtk.main_quit)
self.set_size_request(100, 70)
self.connect("realize", self.on_realize)
GLib.timeout_add(interval=5000, function=self.reset_cursor)
def on_realize(self, widget):
# Step 1: Get the Gdk.Display for the toplevel for this widget.
display = widget.get_display()
# Step 2: Create a new Cursor of type BLANK_CURSOR
cursor = Gdk.Cursor.new_for_display(display, Gdk.CursorType.BLANK_CURSOR)
# Step 3: Get the widget’s Gdk.Window and set the new Cursor
widget.get_window().set_cursor(cursor)
def reset_cursor(self):
cursor = Gdk.Cursor.new_from_name(self.get_display(), 'default')
self.get_window().set_cursor(cursor)
if __name__ == "__main__":
ApplicationWindow()
Gtk.main()
Tested with Python: 3.5 - gi.__version__: 3.22.0
pygtk read-only gtk.TextView (make it ignore mouse clicks)
Found the answer. For anyone who is interested, here it goes.
The truth is, there is no way to make it ignore the mouse clicks. What you can do when you want a read-only kind of text view, you set the 'editable' property to False. This ignores the keyboard input.
The other thing is when inserting text, you want to use the insert
method rather than the insert_at_cursor
method.
Sample
tview = gtk.TextView()
tview.set_property('editable', False)
# Insert text at the end on the textview.
buffer = tview.get_buffer()
buffer.insert(buffer.get_end_iter(), 'This text goes at the end of the existing text')
HTH
pyGObject transparent cursor
This is the easiest solution to get a transparent cursor:
from gi.repository import Gtk, Gdk
win = Gtk.Window(title="Hello World")
win.show_all()
cursor = Gdk.Cursor.new(Gdk.CursorType.BLANK_CURSOR)
win.get_window().set_cursor(cursor)
Gtk.main()
Disable mouse click and change pointer icon
You practically need to interface with some desktop environment and some display server. On Linux that could be Xorg or Wayland. So you should use some GUI toolkit (and they usually provide some event loop). Read more about GTK & Qt. Both are interfaced to Python, with PyGTK or PyQt. Write first some simple GUI application (in PyGTK or PyQt) to understand the concepts.
BTW, xtrlock
is free software, so you can study its source code for inspiration.
PyGTK - GtkEntry insert-at-cursor signal doesn't work
It seems there is a conceptual error between signals and methods.
On one hand, the signals are triggered when an event occurs, those signals can be names as strings like 'insert-at-cursor' or 'insert_at_cursor'. On the other hand, you need to connect those signals (in a widget) with your functions/methods. The functions/methods can have any arbitrary name, for making them easier to read we try to name them as close as the signals, but it is not mandatory.
In your case, you might want to have something like:
class Foo:
...
def create_widgets(self):
entry.gtkEntry()
entry.connect('insert-at-cursor', self.entry_insert_at_cursor)
def entry_insert_at_cursor(self, *args):
# some code
As you can see, in entry.connect(...) happens the match between signal and method.
That explains the syntax error in your code.
The another misconception seems to be the use of the signal insert-at-cursor. For that signal, you have to bind the entry to a key, which does not seem the behaviour you are looking for. Depending of the version of GTK you are targeting, you might want to use:
entry.connect('changed', self.entry_changed)
or
entry.connect('insert-text', self.entry_insert_text)
In the latter, your method will receive the text changed as well as the position. Similarly, you can deal directly with the buffer associated with the entry:
entry.connect('inserted-text', self.buffer_entry_inserted_text)
Where the method will receive additionally the numbers of bytes inserted.
Related Topics
How to Install Writable Shared and User Specific Data Files with Setuptools
Unix Socket Credential Passing in Python
Custom Sorting in Pandas Dataframe
Removing Duplicates from a List of Lists
Filter Dict to Contain Only Certain Keys
How to Get an Absolute File Path in Python
Why Does Adding a Trailing Comma After a Variable Name Make It a Tuple
Label Encoding Across Multiple Columns in Scikit-Learn
Import CSV File as a Pandas Dataframe
Settingwithcopywarning Even When Using .Loc[Row_Indexer,Col_Indexer] = Value
Using Python Subprocess.Call() to Launch an Ncurses Process
How to Capture Mouseevents and Keyevents Using Python in Background on Linux
How to Run Script with Elevated Privilege on Windows
Create Multiple Dataframes in Loop
How to Urlencode a Querystring in Python
Typeerror: 'Module' Object Is Not Callable
Importing a CSV File into a SQLite3 Database Table Using Python