Editing text in a wx.textcontrol with wx validator - python

I have a validator which Ive attached to a wx.TextCtrl inside a wx.Dialog:
myinput = wx.TextCtrl(self, validator=MyValidator())
All that validator does is it binds a wx.EVT_CHAR event and checks whether the input is a number and whether the number of characters entered is less than 5.
The problem is when I select the text with my mouse i.e. turn it to blue I cant replace the text if the number of characters is already at its maximum.
How can I detect whether the user has selected the text of that specific text box and has pressed a key?

Take a look at how the IntValidator inside the file wx\lib\intctrl.py is created.
Now use the intctrl instead of creating your own and limit the characters entered to 4 by using the method SetMaxLength(4)

Related

How to make bolding text in the text widget work optimally?

I want to make text bold work as intended, meaning for example when you want to make a selected word bold, but you mistakenly didn't select the whole word, and you left out the last letter, and then you want to correct that mistake and select the whole word, instead of bolding the word, it will change its weight to normal. Specifically I'm talking about this way of doing this:
`
def bold_it():
bold_font = font.Font(my_text, my_text.cget("font"))
bold_font.configure(weight="bold")
my_text.tag_configure("bold", font=bold_font)
current_tags = my_text.tag_names("sel.first")
if "bold" in current_tags:
my_text.tag_remove("bold", "sel.first", "sel.last")
else:
my_text.tag_add("bold", "sel.first", "sel.last")
`
I am fully aware of what the problem is, and it is in the current_tags variable, since the variable will return "bold" because tag names only looks at tags which are at the first selected position. In turn, this will make the if statements remove the bold tag instead of applying it.
So my question is, how do you fix this, or optimize this?
Codemy.com did a video on this, and this question is based on this video, https://www.youtube.com/watch?v=X6zqePBPDVU.
I tried utilizing the tag_ranges() method so I could get two indexes instead of just where the selecting begins, but it did not work because tag_names() accepts only one argument.

Automatically populate python input prompt with default value

The code:
input("Type your input here:)
displays as:
Type your input here:
I want to automatically populate the input window with text that can be cleared by pressing backspace so the display looks like:
Type your input here: DEFAULT
and after pressing backspace 3 times the user would see:
Type your input here: DEFA
Other posts have indicated that this isn't something you can do in, say, bash, but is there a way to do this in Python?
Simple answer: No. It's just not something you can do in a console app. What's commonly done is to display the default you'll get if you press return:
Type your input here [DEFAULT]:

Is it possible to create default values with EasyGui multenterbox?

I have an EasyGui multenterbox and I was wondering if there was a way to set a default value for the inputs in the GUI?
Thanks
Probably you need default_text argument
Syntax : enterbox(message, title, default_text)
Argument : It takes 3 arguments, first string i.e message/information to be displayed, second string i.e title of the window and third is string which is default text
Return : It returns the entered text and None if cancel is pressed
EasyGui default text

Python Tkinter Entry List Impossible?

Is it utterly impossible to receive a list from user input in Tkinter? Something along the lines of an HTML textarea box - where a user can (1) copy and paste a list of things into a text box, and then (2) tkinter translates the input list into a list of strings, then (3) can assign them to a value and do fun python stuff etc
I have reasonable faith I can accomplish parts (2) and (3), but I'm stuck on (1).
I have explored Entry, which basically accomplishes that but awkwardly and with poor visibility onto the pasted items in the tiny Entry box. I have explored Listbox, which doesn't allow user input in the way of generating a new list from nothing?
The running example is: if I want to input some groceries into a variable, I can copy-paste a text list and paste as one item (rather than separately) --
eg: ["apples", "oranges", "raspberries"] clicks submit VS ["apples"] clicks submit ["oranges"] clicks submit ["raspberries"] clicks submit
-- Anyone have any recommendations for that elusive textarea-like input box for tkinter? Do I just wrestle with the Entry tiny box?
You want a tkinter.Text
import tkinter as tk
# proof of concept
root = tk.Tk()
textarea = tk.Text(root)
textarea.pack()
root.mainloop()
You can retrieve the text with textarea.get in the normal way
result = textarea.get(1.0, 'end') # get everything
result = textarea.get(1.0, 'end-1c') # get exactly what the user entered
# (minus the trailing newline)

tkinter text widget: Setting the insertion curser

I want to create automcomplete feature in a tkinter text widget. When the autocomplete finds a possible word, it deletes the user part-of-word, then insert the complete word:
#if some matched words are found
if self._hits != []:
#delete the part written by the user
self.text.delete("%s+1c" % Space1Index,INSERT)
#Inser the complete word
self.text.insert("%s+1c" % Space1Index,self._hits[self._hit_index])
Then I will tag the text added by the autocomplete to have a different look than the user input. For ex, if the user wrote te, autocomplete will write the complete word test. te will be with normal font, and st will be wrote in another color and waits for the user to confirm the selected word by the computer.
My question is, after inserting the word test and properly highlighting it, how can I move the INSERT position again after te?
I hope I could clarify my question enough, please let me know if more explanation is needed.
To move the insertion cursor, set the "insert" mark to wherever you want:
self.text.mark_set("insert", "%s+1c" % ...)
-or-
self.text.mark_set(INSERT, "%s+1c" % ...)
You can save the position of the insert mark before your autocompletion changes and reset the mark to the saved position after:
old_pos = self.text.index("insert")
# make autocompletion changes
self.text.mark_set("insert", old_pos)

Categories

Resources