Remove last character from wxPython wxTextCtrl? - python

I am trying to remove the very last character from a TextCtrl object in wxPython. I'm using wxPython 2.8.12 and Python 2.7.2.
My code is as follows:
def btnBkClicked(self, e):
self.txtItem.Remove(self, (self.txtItem.GetLastPosition()[-1]), (self.txtItem.GetLastPosition()))
However, that doesn't work :( What do I need to change/do?

If you need to remove the very last character from the string, try
self.txtItem.SetValue(self.txtItem.GetValue()[:-1])
This code gets current text from TextCtrl and sets and sets its value to this text up to the last symbol.
Regarding your input, TextCtrl.Remove needs two parameters: from and to, which are integer numbers giving the first and the last positions to be removed. As GetLastPosition returns the number of characters in the control, your code should be modified as
self.txtItem.Remove(self.txtItem.GetLastPosition()-1, self.txtItem.GetLastPosition())

Related

In python, is there a way to remove all text following the last instance of a delimiter?

I'm trying to create a random text generator in python. I'm using Markovify to produce the required text, a filter to not let it start generating text unless the first word is capitalized and, to prevent it from ending "mid sentence", want the program to search from the back of the output to the front and remove all text after the last (for instance) period. I want it to ignore all other instances of the selected delimiter(s). I have no idea how many instances of the delimiter will occur in the generated text, nor have anyway to know in advance.
While looking into this I found rsplit(), and tried using that, but ran into a problem.
'''tweet = buff.rsplit('.')[-1] '''
The above is what I tried first, and I thought it was working until I noticed that all of the lines printed with that had only a single sentence in them. Never more than that. The problem seems to be that the text is being dumped into an array of strings, and the [-1] bit is calling just one entry from that array.
'''tweet = buff.rsplit('.') - buff.rsplit('.')[-1] '''
Next I tried the above. The thinking, was that it would remove the last entry in the array, and then I could just print what remained. It... didn't go to plan. I get an "unsupported operand type" error, specifically tied to the attempt to subtract. Not sure what I'm missing at this point.
.rsplit has second optional argument - maxsplit i.e. maximum number of split to do. You could use it following way:
txt = 'some.text.with.dots'
all_but_last = txt.rsplit('.', 1)[0]
print(all_but_last)
Output:
some.text.with

Configuring a Tkinter Message to display tuples on individual lines

I am trying to do what the Title says, Configure the Tkinter message so that each tuple in WL_ratios has its own line without being surrounded in brackets. Creating more messages is not an option as the length of WL_ratios can vary, so i have to only use the one.
#(below) is in the __init__ of the class
self.Leaderboardtext = Message(self.LeaderboardFrame,text="",width=100)
self.Leaderboardtext.pack()
#this (below) is in another function in the same class.
WL_ratios = [["james",3]["harrison",2]["jo",1]]
self.Leaderboardtext.configure(text="Leaderboard: {0}".format(WL_ratios))
So far the width=100 is the only thing that is allowing me to get text onto a new line. However this still isnt working because each tuple varies in length so some tuples spread across two lines which isnt what i want. I know you could limit each line by how number of characters but again the tuples vary in character length so i dont think that is an option.
Does anyone have any suggestions that could work?
Just convert each tuple into a string, and add \n there, and that should do it. Alternatively, you could call each value in the tuple separately, and add \n there.

How do I find the start and end line number of a selection in QTextEdit cursor?

I have a QTextEdit widget which is showing lines of text. I want the user to be able to select a block of text to be acted on. I need to determine the starting and ending line numbers from the complete text that correspond to the selected block.
editor.textCursor().blockNumber() gives me the correct starting line number but I haven't been able to find the line number of the ending postion.
Finding the length of the selection in lines would be fine.
I'm using PySide and Python 2.7
Use QTextCursor::selectionStart and QTextCursor::selectionEnd to get block start and end position (as an int).
Then get copy of the text cursor, use QTextCursor::setPosition to set position to these two, and use QTextCursor::blockNumber to get the line numbers.
There may be a shorter way, considering how QTextCursor has quite a lot of methods, but this should work. You may want to write a helper method, for example one which takes the position and a QTextDocument or QTextCursor, and returns the line number for that position.

Unicode characters are boxes

Why is it that some characters show up normally, and some characters (for example, &#3676 - &#3712) show up as boxes? The website I'm using is http://www.tamasoft.co.jp/en/general-info/unicode-decimal.html, and even when I try to return the characters in python, they show up as boxes.
Note: The character codes end with semicolons
Some code points are not yet assigned to a character yet. Code point 3676, or U+0E5C as it's commonly written, is one of those.
As a consequence you don't have to worry about these, as they will not show up in any text.

wxPython SetStyle Not Working

I'm using wx.TextCtrl.SetStyle() in my code, but it's changing the style of all the text!
Here's my code:
# Get all the words in my TextCtrl
words = self.GetValue().split(" ")
# Find out what the farthest uneditable word is.
farthest_uneditable = (len(words) // length_constants["words_per_block"]) * length_constants["words_per_block"]
# Use this word knowledge to calculate the actual farthest uneditable character is
farthest_position = 0
for word in range(farthest_uneditable):
farthest_position += len(words[word]) + 1
# Make all the uneditable text (everything from the beginning to farthest_uneditable) have a grey background
self.SetStyle(0, farthest_position, wx.TextAttr(wx.NullColour, (84, 84, 84)))
I've tested this code and made sure my farthest_position isn't at the end of my TextCtrl (It's been in the expected position each time). For some reason though, all of the text in my TextCtrl box is getting a grey background.
From the wxPython 2.8 documentation. The last paragraph explains where your problem is:
"
**wxTextCtrl::GetRange
virtual wxString GetRange(long from, long to) const**
Returns the string containing the text starting in the positions from and up to to in the control. The positions must have been returned by another wxTextCtrl method.
Please note that the positions in a multiline wxTextCtrl do not correspond to the indices in the string returned by GetValue because of the different new line representations (CR or CR LF) and so this method should be used to obtain the correct results instead of extracting parts of the entire value. It may also be more efficient, especially if the control contains a lot of data."

Categories