Forum Archive

Keyboard script isn’t saving/writing files.

AceNinjaFire

ive just started getting how to work the ui and keyboard modules. And just recently figured out how to save rgb values to a json file and have them load automatically into my ui.View subclass and apply to my view and subview backgrounds. I originally started this in Pythonista app itself on some of my pyui apps. And I was looking to do the same thing in my keyboard scripts, however, as far as I’ve seen it has no problem opening files but it just never wants to save/write to files at all. I originally was trying to create a clipboard ui that could save multiple things and display them for use. But no matter how hard I’ve tried it just doesn’t want to work saving files. I’ve made a ui to set and save the color for my backgrounds from the keyboard, but again it seems to not want to save the information to the file. Anyone have any ideas? Is it even possible? I’ll leave my ui subclass and the contents of my json file below for everyone to critique as they will.


import keyboard
import ui
import json as j


def open_settings_file():
    with open('settings.json','r') as f:
        settings = j.load(f)
        f.close()
    return settings

def save_settings(settings):
    with open('settings.json','w', encoding = 'UTF-8') as f:
        j.dump(settings,f)
        f.close()

class main(ui.View):
    def __init__(self):
        self.settings = open_settings_file()
    def did_load(self):

        #gets subview list from pyui file
        subviews = self.subviews
        settings = open_settings_file()

        #Gets the background and ui sections
        #from settings.json file
        parts = list(settings.keys())

        #for testing to see if the program gets this far
        print(parts)

        #gets the background and ui colors and applys them
        for i in parts:
            r,g,b = settings[i]['Color'].values()
            if i == 'Background':
                self.background_color = (r,g,b)
            elif i == 'Ui':
                for j in subviews:
                    j.background_color = (r,g,b)

    def selection(self):

        #this is for the buttons to open
        #the multiple seperate ui files
        #from the keyboard
        name = self.name
        v = ui.load_view(name)
        if name in ['Html','color','border','other','test']:
            v.present()
            if name == 'color':
                v = self.superview
                v['scrollview1'].scroll_enabled = True
        else:
            keyboard.set_view(v)

    def colors(self):

        #getting superview of the buttons/segment
        #controll/sliders of my color settings
        #keyboard ui page
        v = self.superview

        #getting subviews of the ui page
        subviews = v.subviews

        #getting the segment strings and getting
        #selected index in order to change color 
        #of only the background or ui without 
        #affecting the other
        choices = v['Choice'].segments
        index = v['Choice'].selected_index
        choice = choices[index]

        #getting the rgb values from the
        #individual rgb sliders
        R = v['R'].value
        G = v['G'].value
        B = v['B'].value

        #combining them to make a tuple
        color = (R,G,B)


        if choice == 'Background':
            v.background_color = color
        elif choice == 'Ui':
            for i in subviews:
                i.background_color = color


    def set(self):
        v = self.superview

        settings = open_settings_file()

        choices = v['Choice'].segments
        index = v['Choice'].selected_index
        choice = choices[index]

        R = v['R'].value
        G = v['G'].value
        B = v['B'].value

        color = [R,G,B]
        colors = 'RGB'

        for l in range(len(colors)):
            settings[choice]['Color'][colors[l]] = color[l]

        save_settings(settings)
        self.title = 'Done'
v = ui.load_view('main')
if keyboard.is_keyboard():
    keyboard.set_view(v)
else:
    v.present()

{"Background": {"Color": {"R": 1, "G": 1, "B": 1}}, "Ui": {"Border": {"Width": 0, "Radius": 0}, "Color": {"R": 1, "G": 1, "B": 1}}}

mikael

@AceNinjaFire, just to be sure, can you share the code for save_settings as well?

omz

Please make sure that you have Full Access enabled for the Pythonista keyboard in the Settings app. Otherwise, there is no way for the keyboard and the app to share any files (because that could theoretically be abused to transfer input data over the network, so you basically need to "trust" the keyboard to enable these things).

AceNinjaFire

@omz

Thank you, it seems that worked, I had forgotten to do that😅.

AceNinjaFire

@omz

Btw thank you for pythonista, it was the one thing that actually got me started with programming and made me realize how much I love it. It’s been my hobby for the last 5 months overtaking my love for video games😂