Adding dotted text placeholders within a textfield in Python? - python

So far I am using Tkinter to make textfields in Python.
My question is how do I make it so there are placeholders, preferably in the style of mathematica or something similar so that when a user starts a new line, a right and left place holder appear on that line and the user can only enter text in these placeholders? Eventually I would like to be able to make it so all the right placeholders are aligned as well, but that may be too complicated. I can't seem to find a way to do this in Tkinter. Is there possibly a better package for this?
I'm not sure how to generate and format "text placeholders"
Edit: I think this question is coming down to: how do I dynamically add text placeholders within already existing text fields based on certain key commands?

tkinter doesn't have anything built-in to support this. Tkinter likely has all of the fundamental building blocks in order to build it yourself, but it will require a lot of work on your part.

Related

Replacing keystrokes using Python

I use a multilingual keyboard. Sometimes I write something in one language, forgetting to switch the keyboard layout (e.g. Alt+Shift in Windows) and getting gibberish, which I then erase, switch languauges and rewrite.
I thought of writing a script that replaces highlighted text automatically and assign a keyboard shortcut to it (in Linux). This is intended as a recreational project, and I'm a programming noob, so I don't yet know how to do most of it and I don't have code to share.
My concrete question is (assuming Python, but I haven't started yet so willing to consider alternatives):
Is there an efficient way to map keystrokes in one layout with keystrokes in another layout?
Right now my default idea is to create an explicit table of replacements, e.g. a<->ש, b<->נ etc.
But this would be neither pleasant nor elegant.
Any suggestions?
EDIT: I'll emphasize that I'm not asking about how to perform the actual replacement (e.g. via a dicitionary if using Python), but rather if there is a way to avoid writing an explicit table (dictionary or whatever) for each character in my keyboard.
For example, if there is a way to say "given the layout ENG (US), the letter 'a' corresponds to this phyiscal key right here on the left side of the keyboard, and this physical key, when using the HEB layout, corresponds to the letter ש".
If there's a way to do such a thing, I can write something like keyID = GetKeyID(char,layout) followed by altchar = GetChar(keyID,altlayout).
I hope this makes sense. Note this was only an example of what I might hope for, I'd be happy to hear other ideas!

How to print HTML and to highlight some tags in PyQt?

I'm making the program using PyQt5. One of functions is printing HTML with highlight of some tags by different colours. Every new string is processed and added to text using .append method. I need to print clear HTML, that's way class QTextEdit is not suitable. To solve this problem, one needs to use QPlainTextEdit. But I got a new problem. Now I can't use tags <font> to appoint colour to certain tag. Shielding of tags in class QTextEdit is not good idea. Also, I can't appoint colour to whole field.
How can I solve this problem?
P.S. Sorry for mistakes in my English. You can tell me about them.
I would like to make a comment but I don't have enough reputation.
The comment section already has a good way of doing it, and here is another way.
You can just import html and use html.escape(text). This way you can escape part of the html code that are supposed to be literal strings while keeping other html working. This way you can also keep using QTextEdit.
Here's a quick example:
what I did was:
a = html.escape("<font size=\"3\" color=\"red\">This is some text!</font>")
self.append(""+a+"")
and this is the result.

VIM: How to access the redo-register

As a secondary task to a Python auto-completion (https://github.com/davidhalter/jedi), I'm writing a VIM plugin with the ability to do renaming (refactoring).
The most comfortable way to do renaming is to use cw and autocommand InsertLeave :call do_renaming_func(). To do this I need to access the redo-register (see help redo-register) or something similar, which would record the written text.
If possible, I like to do this without macros, because I don't want to mess up anything.
The . register (#.) contains all editing keys, unfortunately in raw form, so also <Del> and <BS>, which show up as <80>kD, and which insert completion does not interpret. Instead, to extract only the net text entered, use the range delimited by the marks '[ and '] (last one exclusive).
For an example on how to do this, have a look at my PrevInsertComplete plugin.
The . register contains the last inserted text. See :help quote_..
The help doesn't specifically mention any caveats of when this register is populated, however it does mention that it doesn't work when editing the command line. This shouldn't be an issue for you.
The problem was not knowing which register it was, but to access it.
I eventually found the method:
getreg('.')
As #Ingo Karkat points out, this register might include some escape chars.
However, I used a different method in the end. I just read expand('<cword>') to get the new word (because a rename is always only one word). This is far easier and more reliable.
Here's the code (line 113):
https://github.com/davidhalter/jedi/commit/6920f15caf332cd14a7833a071765dfa77d82328

Widget to add, remove, and sort entries in a wxpython program

I'm working on a program that outputs a text procedure file, based on user input. ie, the user will have a few options they can select from, and I'd like for them to be able to add them, reorder them, etc. into a custom order, and have it output that list into a text file.
I'd like to write this in python, and I am familiar with wxWidgets, but not tied to that. Any ideas?
If you're talking about some kind of list box with strings in it, then you might want to look at the EditableListBox, which is in wx.gizmos.EditableListBox. See the wxPython demo for more info.
Otherwise you'll have to roll your own, which really shouldn't be that hard.

How do you make it possible to make accounts? Python

I want to make a simple game on python, all text based. Sort of an interactive story with puzzles etc. but it is likely to be long. I want to be able to it possible for poeple to sort of, 'Save Progress'. Perhaps assign them selves a name etc? I am new to Python and am wondering if it is possible to set up a bank containing details of game play that can be accessed by entering a username and so will load the correct area of the code to allow the player to pick up where they left of.
Say they decided to quit the game just after... say, finding a clue to a murder. The computer would store the line of code they were on and get them to enter their name. Next time they could select 'Load' from the start up menu and then enter their name. The computer would then search for their name and pull up the peice of code they were at and continue as normal from there.
I am new at this so please try and explain simply. I can make a menu etc. no problems with the if's etc and make the story it's self. It is just the loading I want to add.
I hope this isn't tooooo complicated!? Please help me!
You should check out the pickle module. It allows you to store/retrieve any python object. One way to do this would be to have your game take place in a class, where all of the states/etc were members of that class (or derived classes). When they wanted to save you would just pickle your class!
You are pretty new to python, I would recommend reading the documentation provided here: http://docs.python.org/tutorial/ .
Python tries to be both easy to learn and comprehensive. Most questions answer themselves!
Depends on how you create your game...
Do you follow the tree of decisions that the user took in the game? If so you save the decisions in a file, that you save to a location (for example "saves/{username}/{name_of_save}.sav or just "saves/{username.sav} if every username should have only one save, but this is just a example it all depends on you). You could then allow them to select their save file and then simulate the actions from the file till you reach the point where the last decision took place.
If you would like to easily save the data to the file(s) you could also use a format like JSON, which python has built-in support for - read more at http://docs.python.org/library/json.html . This will allow you to easily create objects that you want to save/read from the file and use for processing.
Hope that helps...
Just a Idea, I have not actually tried anything like this
Take a look at http://www.sqlite.org/ or use plain text files for the beginning:) you could use the username:bitfield to encode you progress. Or use the ConfigParser module for Python and create a scetion for each user:) have fun learning:)

Categories