Run a counter in the background - python

Im trying to create an upgrader/tycoon game and realised to have the money continuously going up, I would have to start a different process so I can have my GUI which can be used as the money goes up without either stopping the other from working.
What is the best way to do this?
I've looked around on this site and nothing I could find has helped me!
(If you need any more information just let me know!)

Use threading.
Python has a module named threading, and you need
threading.Thread(target=somefunc).start()
My old answer in the same topic: Here.
If you consider adding some code to your question, I am happy to help in threading! Also, if you need help in designing the code, feel free to ask, threads can be messy.

Related

How do I re-obtain code of a still running fuction in Colaboratory?

a total beginner here. By being dumb, I accidentally deleted a function I have been working on for almost 2 hours and then overwrote the undo possibility. The previous version of the function is still active (I can call it) and I was wondering whether there is a way to recover the code or if I have to start all over again. I couldn't find an answer online so I came here.
Thank you in advance for any answers.
Did some more searching and found a similar question which presented a solution.
import inspect
inspect.getsource(function)
Using this I managed to get the source code back as a string with \n formating. Although setting such a code is going to take a while, it is definitely faster than rewriting it.
However, I wonder if perhaps there is a different or better solution.

Create a Program in Python with a Node conection GUI

I want to create a program with a GUI using Python. This program should show a list of nodes somewhere and allow me to insert them on a working diagram. I also need this nodes connected in some sequence. The following image is similar to what I need, it's from Orange3.
I come from a web development background and I've used Python for some Data Science but all using Terminal so right now I feel a little lost on where to get started.
I would much appreciate some help on where to look. Also I would like to use, if possible, existing tools instead of having to develop everything from scratch. Maybe there even is a project that does what I need and I could fork it from Github.
Thanks a lot for the help.
Check out Tkinter. Its great for GUI. Hard to add in images though. You could use Base64 to add in images.
There are plenty but it's best to create it yourself. There are infinite tutorials. Besides its gonna be full of bugs if you try to alter code that isn't yours.

Python - try for a set period

I Apologise for being a noob - both to asking questions, and python.
I've writen a program, which will be running full time. I have that working ok. It uses the try function to look for an input from a touchscreen.
I now wish to try for 30s,and if there is no input, jump to a blackscreen routine (screensaver), from which it will be looking for any touch to go back to the original try
I think i need an except routine, but i'm tying myself up in knots working out with where to put everything.
Any guidance appreciated
edit
this is Python for use on a raspberry PI.
all i have in code now, is the basic try as follows.
while(True):
try:
get_call()
except SystemExit:
the get_call() routine, sets up some pygame screens, then looks to an on_click() routine for touchscreen input.
I wish to try to get_call() for 30s, and if nothing happens from that, go to screensave()
I know its not much to go on, my code is that of a self taught noob, with no knowledge of best practices, or decent formatting. If anyone wants to look through what i've done, i'll happily zip it all up, and send it through.

Python - Passing keyboard input to a process

Alright, so I got a question and hopefully you guys will be able to help me out with this.
Basically what I'm trying to do is this:
Read what users write in Skype (using the Skype4Py lib).
Convert specific lines from users into a keyboard event.
Pass keyboard events to another (non-python-related) process (.exe) running locally.
Now the first two bullets are definitely doable but I'm not really sure how one would go about the last one. I'm assuming it's possible but I have absolutely no clue which module(s) I would need. If anyone has suggestions, modules or even examples for the last bullet I would very much appreciate it.

Leave some code running while executing more

I am starting a loop in python, and after I start the loop I want the execution of code after the loop to carry on, while the loop keeps looping (in the 'background').
x=True
while x:
#do some output
sleep(1)
#ask for input or something
if input()=='something':
x=False
So in that example, #do some output will keep happening while input is asked for.
Is this possible in python? Are there any work-arounds that could achieve this?
From what I understand, what you want is to create a Thread that keeps executing some tasks in the background.
http://docs.python.org/library/threading.html
Threading is a moderately complex issue, so giving you a recipe here wouldn't amount to much. I suggest you take a look at the documentation to understand what's involved.
Update your question or create a new one if you then have troubles with something specific to the implementation.
EDIT:
I agree with the comment made by Tim Hoffman, the correct solution depends on what you're trying to achieve. From my understanding, threading should work for you, but if you give us more details, it might be easier to give a more precise answer.
Following OP's wish I'm posting the answer. :) There are multiple libraries for achieving what you are trying to do. Most known include:
threading module
multiprocessing module
There are subtle differences between them, but I guess that you shouldn't worry about it at the moment. I encourage you though to read more about mutli-processing and threading in general. Also "green" threads is an interesting alternative.

Categories