Get the Text widget that is focused [tkinter] - python-3.x

Simple question, not sure if there is an answer.
Imagine you have 2 text boxes in a tkinter window, is it possible to know which of the 2 the user is focused on, then return the text widget object itself, or a variable that tells you something.
**Context: **Trying to insert a set of characters at the users cursor, but I want it to add it at their cursor regardless of the text box. Instead of:
self.text1.insert(tk.INSERT, "
Some text..."), I want self.focusedText.insert(tk.INSERT, "
Some text at your cursor in the focused text widget..."). Is there some way to figure out which of many Text widgets a user is focused on? Thanks!

Related

PyQt QTextEdit text selection without moving the cursor (like ubuntu terminal)

I've developed a shell (imitating the ubuntu terminal --> can only edit text after current prompt) by a PyQt QTextEdit.
The thing is when I select some text, the cursor moves as I'm selecting this text (so it disappers from the current command line) and I would like the cursor to stay where it is (only when I select text because I want it to move when I move it programmatically by textEdit.moveCursor(...)) at the same time I'm selecting the text.
Does anybody have any idea of how could I do that?
My solution for now, is to save the position at any change of it (except when it changes by a click), and when I copy some text en paste it, it'll be automatically pasted in the last position the cursor was before the click. That works perfectly but it's "ugly" for the user because, as I said, when he selects the text the cursor disappears of the current line and is where the user is selecting the text. Not like in ubuntu terminal.
Thanks in advance! And sorry for my english.
Adri
I don't see an easy solution to implement this with a text editor API. A terminal is a hack, basically. It mixes a read-only element (anything above the current prompt) with a text editor.
My approach would be to create two text editors, make one read only and display the results of all operations there. If you hide the borders of the two editors, then it will look like a single one. You may have to forward a bunch of events (like scrolling with the keyboard) to the read-only display.

How does Google Docs position the cursor when up or down keys are pressed?

Unlike most rich text editors on the web, Google Docs doesn't use contenteditable and instead uses its kix-editor. While moving the cursor on left/right presses is simple, how do they position their cursor when a user presses up or down?
For example, when a paragraph is multiple lines, how does the cursor know what is "above" it?
Or going up or down across multiple paragraphs, in a situation where there is an empty line between them and the cursor manages to save its horizontal positioning, despite the fact that the empty line forces it to move all the way to the left?
Looking at the DOM in Google Docs, it appears they do their own laying out. That is to say, they measure paragraph lengths and split them into separate divs. Therefore, the idea of iterating through the different ranges in the div "above" the currently selected div is a finite problem, instead of potentially an infinite one (if the previous paragraph is immensely long).

Qt text editor on picture

I haven't found an exact question on this.
I have a picture, it could be blank for the sake of the example, or contain something that I want to add a fancy caption to. I want to add text editing means (akin to Photoshop) on top of the picture.
I'm thinking about subclassing QGraphicsScene and placing a subclassed QTextEdit object on the scene upon pressing a text placement button. Then, when you click on this object, in addition to editing the text, additional text editing tools become available - so you can set parts of text to bold, italic, modify color, size, etc. And then you would be able to change the object's position - and I think that's available in QGraphicsScene by default. And for the background QBrush, there would be a background picture.
Is this a reasonable solution?
Maybe there are any ready-available examples of this, but I haven't found them yet.
My solution
Text item:
For the text item, I used QGraphicsScene with a subclassed QGraphicsTextItem. For the background picture, I used a QGraphicsRectItem with a QBrush and a loaded picture using QImage methods.
To edit parts of the selected text in this subclassed QGraphicsTextItem:
QTextCharFormat applied to my subclassed QGraphicsTextItem using QGraphicsTextItem::textCursor().mergeCharFormat(...). This way I can change formatting, such as boldness, italics, etc. on the selection only.
Some more thingies:
To pan and zoom around the scene, I used an excellent guide by Vpicaver complete with source code from http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming

GXT, how to display the widgets labels with a VBOXLayout

I have been trying to resolve this by myself but I can't find any answers. I need the text fields the combo box, etc., to display its own label, but I can't put it to work on a panel different from the FormPanel (in which all works great). I'm trying to display the labels for a text field on a VBoxLayout but I don't find the way to do it.
I need to work with a VBoxLayout because I need the widgets to position in the middle of the form after the window is maximized or minimized and this layout is the only one that proves to work. Is there another way to accomplish this?
Add another LayoutContainer and then set the panel to use FormLayout for example
LayoutContainer innerPanel = new LayoutContainer();
innerPanel.setLayout(new FormLayout());
Now it will work just like a form panel.

Browser Ctrl+F find non-visible text

Can the browser feature of Ctrl+F to find text be integrated with text in popup windows.
I'd like to have some scientific reference information given when someone hovers over a species name in a web page. Generating the popup, tooltip style text is no problem, the problem is that anyone using Ctrl+F won't be able to find it, or if I position the text out of view when not required, it will be found but be invisible.
The same sort of effect applies to "accordion" style expanding text areas.
I'm looking for some sort of event generated when find is highlighting a result.
Unfortunately there is no such event, you can't interfere with the built-in find.
About the best you can do in this case is to provide your own search function in-page, which searches the DOM for Text nodes containing the given text, highlights them, and opens up any closed accordions they're in.
The only idea I have, is to put all the text from your popups in one additional scrollbox (maybe at the bottom of the page) with a height just large enough to display one set of detail information at a time. This way, it doesn't take up too much space on the page, and the text can still be found using Ctrl+F.

Resources