Is there a way to emmit a signal when a cell is edited with same value? - python-3.x

I need to change the font of a cell in a QTableWidget if the user change the text in it, EVEN if the text is the same as the default one.
I used to use itemChanged signal from the table for that kind of things, but here I'm stuck because the signal won't raise if the user type the exact same text.
item = QtWidgets.QTableWidgetItem("42")
self.my_table.setItem(0, 0, item)
self.my_table.itemChanged.connect(self.update)
def update(self, cell):
print("update")
self.my_table.itemChanged.disconnect(self.update)
font = cell.font()
if cell.text() == "":
cell.setText("42")
font.setUnderline(False)
else:
font.setUnderline(True)
cell.setFont(font)
self.my_table.itemChanged.connect(self.update)
knowing :
default text in the cell is "42"
default font is not underline, other one is
Case 1:
User type "3" -> text become underlined
User type "42" -> text stay underlined
Case 2:
User type "42" -> text should become underlined
Why it doesn't ? :
Because the ItemChanged signal won't raise if the user type exactly the same thing.
Should I use another Signal ? (didn't find any which could match my need)
Is there another way to do so ?
Why does it make sens to try to do so ? :
The user want to make the difference between a value that is the default value and a value that is FIXED to a value (and doesn't care if that fixed value is the default one or not, because the default value may change).
Also if the user want to set back the value to the default one, he can empty the cell.

Related

Select or Highlight all instances of a word type or color in Sublime Text 4

Note: I am not trying to select all instances of a single word.
I can not find a way to select all instances of a word type, similarly how different words are colored according to the format rules from the file type you have.
Here is an image of some sample code of a SQL file.
For example, I would like to Highlight all instances of this reddish pink color, or the baby blue colored words in the whole file. So that way I can capitalize them or copy them or what have you.
From the Tools menu -> Developer -> New Plugin...
Replace the template with the following:
import sublime
import sublime_plugin
class SelectByScopeSelectorCommand(sublime_plugin.TextCommand):
def run(self, edit, scope_selector=None, last_scope_only=True, auto_select=False):
if not scope_selector:
scope_at_first_caret = self.view.scope_name(self.view.sel()[0].a)
if last_scope_only:
scope_at_first_caret = scope_at_first_caret.split()[-1]
if auto_select:
scope_selector = scope_at_first_caret
else:
self.view.window().show_input_panel('Scope Selector', scope_at_first_caret, lambda value: self.view.run_command('select_by_scope_selector', { 'scope_selector': value }), None, None)
return
regions = self.view.find_by_selector(scope_selector)
if regions:
self.view.sel().clear()
self.view.sel().add_all(regions)
self.view.show_at_center(self.view.sel()[0])
else:
self.view.window().status_message('Unable to find anything matching selector "' + scope_selector + '"')
Save it, in the folder ST recommends, as something like select_by_selector.py (the filename doesn't matter too much, but the extension is important).
Then, in your User keybindings, you can add something like:
{ "keys": ["alt+;"], "command": "select_by_scope_selector", "args": { "last_scope_only": true, "auto_select": true } },
Then, pressing Alt+; with the selection caret somewhere in SELECT, it will automatically select all other words in the buffer with the same scope, like DELETE, UPDATE, INSERT etc. Or on INT, it could select all INT, CHAR etc.
You may notice that the "types" you referred to in your question are called scopes in ST parlance. Note that colors don't necessarily have a one to one mapping with scopes depending on your color scheme, so it may select more or less than you expect it to.
You can play around with the keybinding, by removing all arguments for example, to see what effect it has and customize which scopes are being searched for. You can also add it to a menu or the command palette, if that is more to your liking. I suggest to read the ST docs for more info.

How to print conditional fields in PPFA code

How do I print a conditional field using PPFA code. When a value is an 'X' then I'd like to print it. However, if the 'X' is not present then I'd like to print an image. Here is my code:
LAYOUT C'mylayout' BODY
POSITION .25 in ABSOLUTE .25 in
FONT TIMES
OVERLAY MYTEMPOVER 8.5 in 11.0 in;
FIELD START 1 LENGTH 60
POSITION 2.0 in 1.6 in;
Where it has FIELD START 1 LENGTH 60 that will print the given text at that location. But based on the value I want to print either the given text or an image. How would I do that?
Here is an answer from the AFP-L list:
I would create two PAGEFORMATS, one with LAYOUT for TEXT and one with LAYOUT for IMAGE. With CONDITION you can jump between the Pageformats (where Copygroup is always 'NULL')
If you work in a z/OS environment, be careful of 'JES Blanc Truncation'.
That means in one sentence:
if there is a X in the data, condition is true
if there is nothing in the data, condition doesn't work and is always wrong (nothing happens)
In this case you must create a Condition which is always true. I call it a Dummy-Condition.
PPFA sample syntax:
CONDITION TEST start 1 length 1
when eq 'X' NULL PAGEFORMAT PRTTXT
when ge x'00' NULL PAGEFORMAT PRTIMAGE;
You must copy this CONDITION into both PAGEFORMATS after LAYOUT command.
Blanc truncation is a difficult problem on z/OS.
In this sample, the PAGEFORMAT named PRTTXT contains all the formatting and printing directives when the condition is true, and the other called PRTIMAGE contains every directive needed to print the image.
HTH

Get length of a string in label widget

Is there a way to get the length of the string of characters in a label widget in Tkinter? I'm trying to update the value of a label with a button press, and my first check is to see if there is something in the label. The basics of what I'm trying to do is as follows
if len(label_widget) > 0:
do something....
else:
do something else
The cget method lets you get the value of any of the configured options.
For example:
if len(label_widget.cget("text")) > 0:
...

Why Text cursor coordinates are not updated correctly?

To create a simple line-column counter for my text editor, I decided to simply use the index function of the tkinter.Text widget. In fact, the index function returns a string representing the line and column of the coordinates passed as argument to it.
Specifically, I am using cursor_pos = text.index(tkinter.INSERT) to get the index of the cursor, since, from the effbot website on tkinter.INSERT:
tkinter.INSERT corresponds to the insertion cursor.
The problem is that tkinter.INSERT seems to give me the last cursor position until I move the cursor with the arrows (for example).
This is the function that handles the count of the lines and columns:
def on_key_pressed(self, event=None):
"""Docs """
if self.tpane is not None:
print(self.lines, self.columns)
self.tpane.update()
cursor_pos = self.tpane._tabs[self.tpane.selected()].text.index(tkinter.INSERT)
self.lines = int(cursor_pos.split('.')[0])
self.columns = int(cursor_pos.split('.')[1])
print(self.lines, self.columns)
self.line_c.config(text='Lines: ' + str(self.lines))
self.col_c.config(text='Columns: ' + str(self.columns))
self.update()
I don't know if you can understand the situation... When I first type a letter on the editor, the self.columns variable does not update to 1 (remains 0) until I write the second letter, where it updates to 1, and so on. But there's a trick to make it update without writing a new letter. Once written the first letter, if I move the cursor with the arrows, it updates the self.columns to actually 1.
Another problem is when I try to delete a existent character. For example, if I have 3 characters (and suppose I have self.columns to 3), and I press delete, self.columns update inexplicably to 4, and if I try to remove another character, at this point, it updates to 3.
This problems exists also for the self.lines in the on_key_pressed event handler. I am not sure if this is supposed to happen, and if yes, then I am missing something...
This happens because your custom binding fire before the built-in bindings, and it is the built-in bindings that actually modify the widget and change the cursor position.
You can change the order by leveraging bindtags. For more information see this question: Basic query regarding bindtags in tkinter
For an example of how to change the bindtags, see this answer: https://stackoverflow.com/a/3513906/7432
If you don't want to deal with bindtags, you can bind to <KeyRelease>. Built-in bindings happen on a key press, so the release binding will always fire after the widget has been updated.

How to verify ANY text is present with selenium IDE

I know how to verify if a specific text is present in a web page using Selenium IDE. But what I wanted to know is, can you verify that any text is present in an element?
For example there's a text box with the title "Top Champion". This text box will be changed daily with the name of a person. Now I just wanted to check whether there is a text in this text box, no matter what the text actually is. I've tried the verify text command and tried blanking the value, but it doesn't work. If the command can return a true or false command that would be really helpful
BTW, verify value doesn't work either since the element that I'm testing is not a form field
Your best bet is as follows (I have written single tests for this for numbers)
Medium rigour:
waitForText | css=.SELECTORS | regex:.+?
This will wait until there is at least 1 character present.
Strong rigour (only works if you have a subset of characters present):
waitForText | css=.SELECTORS | regex:^[0-9]+$
This will wait until there is text. This text must start with a number, have at least 1 number, and then finish. It does not permit any character outside of the subset given. An example you could do to match numbersNAMEnumbers would be.
waitForText | css=.SELECTORS | regex:^[0-9]+[a-zA-Z]+[0-9]+$
This would wait for a string such as 253432234BobbySmith332
Luke
If i have understood your question properly there below is one way you can search for an element contains a string. Not sure if this is what you are looking.
List<WebElement> findElement = webElement.findElements(By.xpath("YOUR_TEXTINPUT_PATH_HERE"));
if( findElement.size() > 0 ){
if( findElement.get(0).getText() != null && findElement.get(0).getText().indexOf("THE_STRING_THAT_YOU_WANT_TO SEARCH") != -1 ) {
// IF IT COMES HERE, THAT MEANS THE ELEMENT IS PRESENT WITH THE TEXT
}
}
store text|[your element]|StoredText
execute script|return ${StoredText}.length > 0|x
assert|x|true
Using these three lines in the Selenium IDE, the first line will extract the text from the element into the variable StoredText.
The second line will store whether the length of that text is greater than zero into the variable x (a true or false result).
The third line asserts that the result was true, failing the test if not. You don't need the third line if all you want is the true or false result.
So if the element contains any text, the extracted text length will be greater than zero, the variable x will be true, and the assert will pass. This verifies that any text is present in the element.

Resources