[HELP] Update View During Button Action
-
def changeBackground(sender): z=sender.superview x=1 while x<=10: button='button'+str(x) z[button].background_color=(0,0,0) x+=1 time.sleep(1)
In my mind, this code (assuming that this gets called by pressing a button) should change the background color of 'button1', 'button2', and so on in order. Changing one every second.
However, if this code was ran the program would wait for 10 seconds and then update all of the buttons' background colors at once. How can I get it to update the UI at the time when that line of code is ran? I have tried
z[button].set_needs_display()
to no avail.Thanks!
-
Thank you all for the help! I once thought I was fairly fluent in Python, however I now realize how little I know.
EDIT: Another quick thing. Let's say I make a game on Pythonista, is it possible to make some sort of "savegame" file? That way You could save leaderboards and/or progress?
-
I'm no expert on this (still learning python myself), but there are countless different ways to save data, be it a database, JSON, shelve, and others. Which one is best depends on your application. Saving a leader board is simple enough that you could do it with with a simple shelve style database. Simply storing the last level a person was on would also be simple. If you need to save exactly what's going on in a game that has lots of independent characters that all have logic and history associated with them you'll probably want something more sophisticated.
-
@TQA, check our the
high-scores
module in theGames
section ofPythonista-Tools
. It does a lot of what you are looking for.
-
Personally I prefer JSON for persistent data storage with Python. The types it has are very similar to Python's basic data types, so if you can represent all your data in some combination of
dict
s,list
s,str
s,float
s,int
s andbool
s you can easily dump and load that data using thejson
module.A database is probably more complex than necessary for something as simple as a score leaderboard, but if you need to store large data sets a database may be a better choice than JSON.
If you need to store the state of multiple Python objects, then
pickle
orshelve
are probably best. I'm not sure what exactly the limits of those modules are, certain objects can or cannot be pickled/shelved depending on how they are laid out.
-
@dgelessus, I completely agree with your recommendation of
json
as the preferred method of storing Python objects into files. It is also super helpful that modules likeRequests
have built-in support for json and that the resulting files are very human-readable and human-editable.When @techteej was working on the high scores module that I mentioned above, he made the design choice to use pickle instead of json because it was too easy to cheat by opening up high_scores.json in the Pythonista Editor and increase your high score. The use of pickle (slightly) increases the difficulty of cheating in this way.
-
I just want to save a single integer along with the date/time. I feel pretty stupid. I have no idea how this works as I have never done anything with reading/writing files before. How do I create a file to be written to?
-
with open('myfile.txt','w') as f: f.write('something')
-
import datetime, json, random filename = 'my_file.json' data_a = [random.randint(1, 100), unicode(datetime.datetime.now())] print(data_a) with open(filename, 'w') as out_file: json.dump(data_a, out_file) with open(filename) as in_file: data_z = json.load(in_file) print(data_z) assert data_a == data_z, 'Something went wrong: {} != {}'.format(data_a, data_z)
-
I seem to have run into a problem..
I write
import os import os.path with open('save.txt','w') as f: f.write('hello') with open('save.txt','r') as f: f.read()
Nothing happens. I don't get any errors, but the console output window doesn't even open.
-
Try changing the last line to:
print(f.read())
.