Renpy pasteable input - python

A game I'm working on needs an input in which a special code can be pasted. The code is a random hash of 24 characters, which would be hard for players to write manually. I haven't found any good quality ways of implementing such an input.
Could this be even done? And if so, how?
I'm just started with Renpy not so long ago and my knowledge is currently scarce

It is mentioned in the documentation.
Input
Creates a text input area, which allows the user to enter text. When > the user presses return, the text will be returned by the interaction.
copypaste
If True, it becomes possible to copy and paste into this input. (By
default, disabled.)
Just set copypaste=True

Related

What is it called when a program takes control over a console, and how can one do that in python?

I am planning to make a python program which displays some sequential info in the console without adding newlines, i.e. similarly to what man does - the console is taken over by the program and all the input goes to the program, and the program is able to update text in place rather than having to print on a newline every time. I don't know what is the proper term for this, which makes for a "problem of unknown terminology" (which I'm sure has a proper name as well, but ironically I do not know it either), and thus makes it near impossible for me to search for the answer myself.

Alternate ways to stop print() in python

I have to get user input from the console. Using python input() statement only stops and returns the input when the user clicks "ENTER". But I want it to also happen with "TAB".
Is there a way that python input() can respond to both "ENTER" and "TAB" and return the user input?
I have tried different modules like msvcrt, keyboard, pynput, etc., But they simply read characters one by one. I had to deal with the below items:
Shift + ch (for uppercase)
Inserting characters in between (using right/left arrows)
Copy and Paste
Insert, etc
So I don't want to manually code for all these and many unknowns may raise in the future. These are all already taken care by python input(). I just need to stop it and return the output for "TAB" as well.
Please let me know if there is a way to achieve this.
It might be helpful to a lot of users.

Efficiently applying text widget tags in tkinter text widgets

I'm trying to implement syntax highlighting for text in a Text widget.
I use an external library to lexically analyse the text and give me the tokenised text. After that I go over all the words in the text and apply tag to their position in the text widget so that I can style each word.
My concern now is how do I deal with changes. Every time the user presses a key, do I tokenise the entire text again and add style tags to the text widget for the entire text. This is proving to be quite slow.
I then transitioned to only doing the highlighting process for the line the insert character was to make it faster but this is giving faulty results and the highlighting is not perfect now.
What would be an ideal compromise between fast and perfect? What is the best way to do this?
One possible answer is to do something like Idle does. As a user hits each key, its custom incremental parser tags identifiers that are a keyword, builtin, or def/class name*. It also tags delimited char sequences that are a string or comment. I does what can be done quickly enough.
For example, if one types printer not in a string or comment, Idle checks if the word is a keyword or builtin name after each key. After t is hit, print is tagged. After e (or any other identifier char) is entered, printe is untagged.
I believe some of the code is in idlelib/Hyperparser.py and some in ColorDelegator.py. You are free to copy and adapt code, but please do not use it directly, as the API may change. I presume the parser does the minimum needed according to the current state (after def/class, in an identifier, comment, string, etc.)
Idle has an re-based function to retag the entire file. I 'think' this is separate from the incremental colorizer, but I have not read all the relevant code. If one edits a long enough file, such as idlelib/EditorWindow.py (about 3000 lines), and changes font size, Idle retags the file (I am not sure why). There is a noticeable delay between the file turning all black and its being recolorized. You definitely would not want that delay with each keystroke.
Class/functions names with non-ascii chars are not yet properly recognized in 3.x, but should be. The patch is stuck on deciding the faster accurate method. Recognizing an ascii-only (2.x) indentifier is trivial by comparison.
PS I am correctly in guessing that you are interested in tagging something other than Python code?

How to get Python GUI to call a genetic algorithm written in C

I'm new to Stack Overflow. I have a genetic algorithm written in C that accepts user input in the form of a number 0-100, and outputs an array of numbers. The C code is a full, stand-alone compiled program. It has a command-line interface. I'm relatively new to programming, mostly hacking until I find a solution to a specific task. and I'm very confused in reading the Python Subprocess management documentation. I have a GUI written in Python using tkinter, and I have a box where the user can type their response value (0-100). I also have an empty array in my code that I want to populate with the output from the genetic algorithm. The user will use that array for something, give another response (0-100) the C code will take that response, produce another array of numbers, and the process continues. My question is, can anyone explain to this novice in simple terms how to use the subprocess module to link my python GUI and the C code genetic algorithm together to this end? Thank you!
I assume that you are able to store the text that the user enters in a variable? If not, this question explains it pretty nicely. Anyway, once you get that, call subprocess.check_output like this:
result = subprocess.check_output(["./cexecutable", inputValue])
(replace "cexecutable" with the name of the executable of your genetic algorithm program and inputValue with whatever variable you're storing the input in)
This should store the output of the genetic algorithm in result. It will be all one string, so if there are multiple lines of output you'll probably want to call result.split("/n") to get a list of lines. You can then parse them and put them into the array as you see fit, based on how they're formatted.
Assuming that you have some sort of "enter" button associated with the text box, and you're doing this all as an event that occurs when the button is clicked, this will happen every time the user enters new text and clicks the button.
EDIT (in response to comment):
To keep the program running in the background, you'll need to use subprocess.Popen and Popen.communicate. Rather than just returning the output of your program, this will create a Popen object that you can continue to interact with. Without testing this on your code, I can't guarantee that the code below will do exactly what you want, but I think it should be close:
genAlg = subprocess.Popen(["./executable"])
...
#user enters data
...
result = genAlg.communicate(inputValue)[0]
#communicate sends the given argument to stdin, and returns a
#tuple (stdout, stderr) - you want stdout, hence the "[0]"
EDIT 2:
Turns out that's not actually a workable solution - see J.F. Sebastian's comments below.

how do I keep my input at the same screen location?

My current input method is a for loop. I input an integer it gets added to a list then it goes around to the next input. This produces a new line each time it goes around. I would like to keep the input entries at the same location. I'm new to Python and programming in general so I have no clue where to start. My input statement is in this form:
var= input(" message: ")
Thank You
If you are working with UNIX or Windows terminals, you can add an ASCI escape ("\033[A") to move the cursor one line up to ask for input again, as shown in the example here, so in this way, you can get your input message at the same position by "overwriting" the previous input message.
tl;dr: there's a reason most simple applications don't do this: it's a lot of work for not very much payoff.
If you're talking about making sure that the "message: " line always appears in the same place in the window, then you can't do it with plain input: it doesn't take an "end of line" argument and actually the newline happens before input gets a hold of the input string. (I.e. when the user hits RETURN.)
You can do it, especially if you don't care about Windows, using getch mode, which basically says that the terminal passes characters to you, not whole lines, and you need to decide how to interpret them. The getch package seems like it might be able to help here, it even supports Windows!
Another option (probably not as effective or a nuclear bomb when what you need is a scalpel) would be to use something like curses or blessings' terminal.location abilities. And blessings, I believe, incorporates ANSI escape chars. (As mentioned in the other answer to this question, it's probably the right way to go.)

Categories

Resources