首页 > 解决方案 > Kivy 使用两个文件 .kv

问题描述

我正在尝试通过我的 .py 代码上的函数来切换 kv 文件。在我的第一个 kivy 代码中,屏幕很少,在我的第二个 kivy 代码中,有一个 swipebutton@carausel。有什么方法可以连接这两个文件。我发布了一个示例:<Screen1> id:_screen1在另一个文件上有:<SwipeButton@Carousel>. 我希望有人可以帮助我这是我卸载第一个文件并加载第二个文件时收到的输出,它不会破坏代码: 输出

` class MainApp(MDApp) def build(self): self.title = "Meet!"

    if "Colore" in impostazioni:
        self.theme_cls.theme_style = impostazioni.get("Colore")["coloresfondo"]
    else:
        print("Nulla")
        self.theme_cls.theme_style = "Light"
    if "Nome" in impostazioni:
        Nome = impostazioni.get("Nome")["nome"]
        print(Nome)
    else:
        print("Non trovato")

    if "Sesso" in impostazioni:
        Sesso1 = impostazioni.get("Sesso")["sesso"]
        print(Sesso1)
    else:
        print("Non trovato")
    self.theme_cls.primary_palette = "Red"
    self.theme_cls.primary_hue = "A700"
    self.dativari = [{'id': i, 'data_index': i, 'index': 1, 'height': 48, 'text': str(calendariofile.get(str(i)))} for i in calendariofile]
    self.screen = Builder.load_file("num3.kv")
    self.root = Builder.load_file("prova.kv")
    return self.screen`

#:import C kivy.utils.get_color_from_hex <SwipeButton@Carousel>: text: '' size_hint_y: None height: 48 ignore_perpendicular_swipes: True data_index: 0 min_move: 20 / self.width on__offset: app.aggiorna(root.data_index)#print(root.data_index) #app.update_index(root.data_index, self.index) canvas.before: Color: rgba: C('FFFFFF33') Rectangle: pos: self.pos size: self.size Line: rectangle: self.pos + self.size Button: text: 'delete ({}:{})'.format(root.text, root.data_index) on_press: app.elimina(root.data_index) Label: text: root.text Button: text: 'archive' on_press: app.passachat(root.data_index) RecycleView: data: app.dativari viewclass: 'SwipeButton' do_scroll_x: False scroll_timeout: 100 RecycleBoxLayout: orientation: 'vertical' size_hint_y: None height: self.minimum_height default_size_hint: 1, None

标签: python-3.xkivykivymd

解决方案


如果这两个kv文件没有重新定义相同的类,那么您可以同时加载它们。

如果这两个kv文件确实重新定义了相同的类,那么您可以使用Builder.unload_file()在加载另一个之前卸载一个。请注意,加载/卸载kv文件不会影响已创建的 Widget,只会影响更改后创建的 Widget。


推荐阅读