Pygame: Graphical Input + Text Input - python

In Pygame, how can I get graphical input(e.g. clicking exit button) and also get input from the a terminal window simultaneously?
To give you context, my game has a GUI but gets its game commands from a "input()" command. How can I look for input from the command line while also handling graphics?
I'm not sure if this is possible, but if not, what other options do I have for getting text input from the user?
Thanks in advance.

You can't do that, unless you use the input command in a different thread, but then you have to deal with syncronization (which might be what you want or don't want to do).
The way I'd implement this is to create a kind of in-game console. When a special key (e.g. '\') is pressed you make the console appear, and when your application is in that state you interpreter key pressing not as in-game commands but... well, as text. You can print them in the console (using fonts). When a key (e.g "return") is pressed you can make the console disappear and the keys take back their primary functionality.
I did this for my pet-project and it works as a charm. Plus, since you are developing in python you can accept python instructions and use exec to execute them and edit your game "on fhe fly"

Related

Function running only after tkinter windows has been closed

I'm learning how to program in Python and use the PIL (Pillow) in order to create small bots which can recognize changes in the screen in order to execute repetitive commands (keyboard presses with pynput module).
I did a small GUI with Tkinter in order to get user information regarding the parameters which the user is going to use (Entry widgets) at their use of the bot and I also need a few buttons for enabling/disabling certain parts of the code (i'm using check buttons and using boolean logic in order to enable/disable) and also for enabling the main function of the bot (which is to press a button once it recognizes a certain change at the screen).
What I am currently experiencing is that the once I run my program, it opens up my GUI and all that a user would see but it doesn't execute my main function, it only executes the main function AFTER I close the GUI.
I'd love to post my code but it exceeds 220 lines and it would probably become even more confusing.
So, long story short, is it a kind of error which happens a lot when inexperienced programmers try to create softwares with tkinter? If yes, what causes it and how can it be fixed?
I appreciate any help you guys can get me. :]
...creates frames, widgets, entries...
mainHealerVar = IntVar()
main_healer_check = Checkbutton(root, text="Enable the bot", variable=mainHealerVar) #this should mean that when i press this checkbutton it'll enable the bot and perform the repetitive commands for as long as it stays checked
... some more buttons/code...
root.mainloop() #right after I stop setting up the GUI, I place this function
... make logic in order to know which part of the repetitive functions to execute...
if mainHealerVar.get() == 1:
mainHealFunction() #function to check if the checkbutton is still pressed (if it is, it should execute mainHealFunction()
It was supposed to execute the repetitive commands by the pynput module as soon as i clicked on the enabling Check-button, but it doesn't execute anything until I close the GUI, then it starts to execute the repetitive commands. I have also tried using a while loop instead of an "if" at the end, but ended up with the same result.

Block keyboard input in python

In Python, is there a way to prevent the user from entering anything via keyboard, so that terminal output does not get cluttered by unneeded input?
In my case, the user is supposed to press and hold a key to register a specific keyboard - so, the actual input (as in input()) is not needed, but only clutters the output and menu.
I would recommend pygame. http://www.pygame.org/download.shtml It allows you to read all keyboard keys as you like. Here's an example of how the code would look: How to get keyboard input in pygame?
It sounds like you want the terminal contents to redraw themselves on user input?
https://docs.python.org/3/howto/curses.html or https://docs.python.org/2.7/howto/curses.html depending on python version.
See also this question Dynamic terminal printing with python

Clear terminal up until point x - Python

A program I'm writing takes user commands from input(), executes corresponding functions, and displays relevant text.
After about 5 commands-worth of text, the terminal becomes cluttered even when the window is maximized. What I would like to do is clear the terminal after every five commands, but only clear the text that precedes (is above) the fifth command and its output text.
More specifically, after the user has typed in the fifth command, upon pressing Return (entering the command), I want commands 1-4 and their corresponding outputs to clear off the screen but have command 5 and its output remain at the top of the terminal.
For demonstration, here is what I want the screen would look like during this process:
The above becomes the below:
Using the os module and os.system('cls') or os.system('clear') functions will not exactly work in this situation. I don't want to clear all of the text on the screen, just the text before a certain point.
So, how can I do this on Windows with Python?
Note: If the solutions are simple, I would like both a method of obliterating the text so that it cannot be scrolled back up to as well as a method that would allow users to see previous commands and text.
Using simple terminal output, there isn't really a good way to do this. Even the operation of "clearing the screen" is outside what is normally considered simple terminal output, which is why you end up calling an external program to do it.
However, a different way of handling terminal output is to use the curses library. This library allows you extensive control over exactly how your output appears on the screen, and in fact includes functions like deleteln and insdelln to delete lines of text from the screen.

Specialized Text Console in wxPython

Is there any way that I can build an interactive text console using wxPython window application? It will be used to allow user to provide natural language input for the application to parse the grammar based on the language selected. It is not going to be used to run any shell or commands.
You can use a wx.TextCtrl for input and then when the user is done typing, you can have them press a button to do whatever checking you want done. Alternatively, you could use a wx.Timer to watch for idleness so when the user is idle for x seconds, it does the checking you want automatically too.

print a string in an external application (python 3.1)

Suppose I have a game and a python script running. In this game, to speak you just type whatever you want and hit enter. This python script has a button on it that I want to output a predefined string into the game, and hit enter automatically (essentially, the button causes the character to speak the string). What would be the easiest way to implement this?
(just the actual 'send string to game and hit enter' thing, not the buttons and stuff)
Assuming your game is not running in the console (in that case you could use stdin), sendkeys might be an option on Windows. It allows you to send keystrokes to a certain window - in this case, the game window.
If the game is scriptable, you should of course use the game's own scripting options if available.
It depends on what hooks the game provides. If it doesn't provide any hooks, you may want to look into whatever your windowing system uses for automation. I've only done this sort of thing in Linux where I could use X's XTest extension (xte).

Categories