Python kivy.lang.Builder.load_file() Examples

The following are 8 code examples of kivy.lang.Builder.load_file(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module kivy.lang.Builder , or try the search function .
Example #1
Source File: __init__.py    From kivystudio with MIT License 6 votes vote down vote up
def load_defualt_kv(filename, file_content):
    ''' load the default kivy file
        associated the the python file,
        usaully lowercase of the app class
    '''
    app_cls_name = get_app_cls_name(file_content)
    if app_cls_name is None:
        return

    kv_name = app_cls_name.lower()
    if app_cls_name.endswith('App'):
        kv_name = app_cls_name[:len(app_cls_name)-3].lower()

    if app_cls_name:
        file_dir = os.path.dirname(filename)
        kv_filename = os.path.join(file_dir, kv_name+'.kv')

        if os.path.exists(kv_filename):
            try:    # cacthing error with kivy files
                Builder.unload_file(kv_filename)
                root = Builder.load_file(kv_filename)
            except:
                trace = traceback.format_exc()
                Logger.error("KivyStudio: You kivy file has a problem")
                Logger.error("KivyStudio: {}".format(trace)) 
Example #2
Source File: node.py    From kivy3dgui with MIT License 5 votes vote down vote up
def load_kv_file(self, file_path):
        for e in self.fbo_widget.children:
            self.fbo_widget.remove_widget(e)
            
        if os.path.isfile(file_path):
            res = Builder.load_file(file_path)
            self.add_widget(res) 
Example #3
Source File: __init__.py    From kivystudio with MIT License 5 votes vote down vote up
def start_emulation(filename, file_content, threaded=False):
    root = None
    has_error = False
    if os.path.splitext(filename)[1] =='.kv':    # load the kivy file directly
        try:    # cacthing error with kivy files
            Builder.unload_file(filename)
            root = Builder.load_file(filename)
        except:
            has_error = True
            trace = traceback.format_exc()
            Logger.error("Emulator: {}".format(trace))

    elif os.path.splitext(filename)[1] =='.py':
        load_defualt_kv(filename, file_content)
        try:    # cahching error with python files
            root = load_py_file(filename, file_content)
        except:
            has_error = True
            trace = traceback.format_exc()
            Logger.error("Emulator: {}".format(trace))
    else:
        Logger.warning("KivyStudio: can't emulate file type {}".format(filename))

    if not root and not has_error:
        Logger.error('Emulator: No root widget found.')
    elif not isinstance(root,Widget) and not has_error:
        Logger.error("KivyStudio: root instance found = '{}' and is not a widget".format(root))
    elif root:
        if threaded:
            emulation_done(root, filename)
        else:
            get_emulator_area().screen_display.screen.add_widget(root)

    dirname=os.path.dirname(filename)
    sys.path.pop()
    resource_remove_path(dirname) 
Example #4
Source File: __init__.py    From kivystudio with MIT License 5 votes vote down vote up
def load_kv(filepath, file):
	''' load a kivy file from the current
		directory of the file calling this func
		where filepath is __file__ and file is a kv file''' 
	filepath = dirname(filepath)
	Builder.load_file(join(filepath, file)) 
Example #5
Source File: infoscreen.py    From RPi-InfoScreen-Kivy with GNU General Public License v3.0 5 votes vote down vote up
def add_screen(self, screenname):

        # Get the info we need to import this screen
        foundscreen = [p for p in getPlugins() if p["name"] == screenname]

        # Check we've found a screen and it's not already running
        if foundscreen and not screenname in self.availablescreens:

            # Get the details for the screen
            p = foundscreen[0]

            # Import it
            plugin = imp.load_module("screen", *p["info"])

            # Get the reference to the screen class
            screen = getattr(plugin, p["screen"])

            # Add the KV file to the builder
            Builder.load_file(p["kvpath"])

            # Add the screen
            self.scrmgr.add_widget(screen(name=p["name"],
                                   master=self,
                                   params=p["params"]))

            # Add to our list of available screens
            self.availablescreens.append(screenname)

            # Activate screen
            self.switch_to(screename)

        elif screenname in self.availablescreens:

            # This shouldn't happen but we need this to prevent duplicates
            self.reload_screen(screenname) 
Example #6
Source File: lang.py    From Tickeys-linux with MIT License 5 votes vote down vote up
def load_file(self, filename, **kwargs):
        '''Insert a file into the language builder and return the root widget
        (if defined) of the kv file.

        :parameters:
            `rulesonly`: bool, defaults to False
                If True, the Builder will raise an exception if you have a root
                widget inside the definition.
        '''
        filename = resource_find(filename) or filename
        if __debug__:
            trace('Builder: load file %s' % filename)
        with open(filename, 'r') as fd:
            kwargs['filename'] = filename
            data = fd.read()

            # remove bom ?
            if PY2:
                if data.startswith((codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)):
                    raise ValueError('Unsupported UTF16 for kv files.')
                if data.startswith((codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)):
                    raise ValueError('Unsupported UTF32 for kv files.')
                if data.startswith(codecs.BOM_UTF8):
                    data = data[len(codecs.BOM_UTF8):]

            return self.load_string(data, **kwargs) 
Example #7
Source File: lang.py    From Tickeys-linux with MIT License 5 votes vote down vote up
def load_file(self, filename, **kwargs):
        '''Insert a file into the language builder and return the root widget
        (if defined) of the kv file.

        :parameters:
            `rulesonly`: bool, defaults to False
                If True, the Builder will raise an exception if you have a root
                widget inside the definition.
        '''
        filename = resource_find(filename) or filename
        if __debug__:
            trace('Builder: load file %s' % filename)
        with open(filename, 'r') as fd:
            kwargs['filename'] = filename
            data = fd.read()

            # remove bom ?
            if PY2:
                if data.startswith((codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)):
                    raise ValueError('Unsupported UTF16 for kv files.')
                if data.startswith((codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)):
                    raise ValueError('Unsupported UTF32 for kv files.')
                if data.startswith(codecs.BOM_UTF8):
                    data = data[len(codecs.BOM_UTF8):]

            return self.load_string(data, **kwargs) 
Example #8
Source File: main.py    From real-time-plot-microphone-kivy with MIT License 5 votes vote down vote up
def build(self):
        return Builder.load_file("look.kv")