0

I am having trouble understanding how to inherit the functionality of one class into another with Kivy. I understand the error message ('LoadDialog_Directory' object has no attribute 'manager'), but I'm just not grasping exactly how to fix it. I believe I need to do something with the function below, but ultimately, I do not know.

def __init__(self, **kwargs):
        super().__init__(**kwargs)

The goal of this script is to be able to select a specific driver as the path for the filechooser. I choose this method vs. others because most were using Kivy 1.11.1, this version has a lot of deprecated functions that do not work with 2.0.

.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.spinner import SpinnerOption
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.utils import platform
from kivy.properties import StringProperty

import os
import string


class WindowManager(ScreenManager):
    pass   
class MyOption(SpinnerOption):
    pass


class LoadDialog_Directory(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)

    def dir_driver(self):
        x = self.manager.get_screen("first")
        return str(x.ids.drive_dir.text)

        
        
class FirstWindow(Screen):

    def get_drives(self):
        drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
        return drives
        
      
    def dismiss_popup(self):
        self._popup.dismiss()
  
        
    def show_load_directory(self):
        content = LoadDialog_Directory(load=self.directroy_path, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
        self._popup.open()
        
                
    def directroy_path(self, path, filename):        
        self.ids.text_input_directory.text = str(filename[0])
        self.dismiss_popup()
        
             
kv_main = Builder.load_file('main.kv')

#
class MyApp(App):
    def build(self):
        return kv_main
        

if __name__ == '__main__':
    
    MyApp().run()

main.kv

#:include first.kv

WindowManager:
    FirstWindow:
    
<LoadDialog_Directory>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            dirselect: True
            path: root.dir_driver()

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()
            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

first.kv

<FirstWindow>
    name: 'first'
    GridLayout:
        cols: 1
        
        BoxLayout:
            orientation: "horizontal"               
            TextInput:
        
        BoxLayout:
            orientation: "horizontal"
            Spinner:    
                id: drive_dir
                text: "Root Drive"
                halign: 'center'
                option_cls: "MyOption"
                values: root.get_drives()               
            Button:
                text: "Set Result Directory"
                on_release: root.show_load_directory()
            TextInput:
                id: text_input_directory
                disabled: True
                text: text_input_directory.text
        
        BoxLayout:
            size_hint: (0.01, 1)
            orientation: "horizontal"       
            TextInput:

Side Note: The reason for the extra blank TextInput is because the Spinner will not function (show drivers) if it is taking too much of the App.

Binx
  • 382
  • 7
  • 22

1 Answers1

0

After a few hours of trail and error I finally got it to work, but I have no idea why it works. Here is what changed:

  1. New variable in class LoadDialog_Directory(FloatLayout) - input_pth
  2. Called input_pth into my content variable within class FirstWindow(Screen) function show_load_directory(self)
  3. Set my filechooser.path to root.input_pth in the main.kv file.

I do not understand how the input_pth variable within content is able to reference the class LoadDialog_Directory(FloatLayout) without having to pass something like:

def __init__(self, input_pth, **kwargs):
        super(LoadDialog_Directory, self).__init__()
        self.input_pth = input_pth

.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.spinner import SpinnerOption
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.utils import platform
from kivy.properties import StringProperty

import os
import string


class WindowManager(ScreenManager):
    pass  
    
class MyOption(SpinnerOption):
    pass


class LoadDialog_Directory(FloatLayout):   
    input_pth = StringProperty()
    load = ObjectProperty()
    cancel = ObjectProperty()
    
        
        
class FirstWindow(Screen):

    def drive_list(self):
        drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
        return drives
        
      
    def dismiss_popup(self):
        self._popup.dismiss()
  
        
    def show_load_directory(self):
        content = LoadDialog_Directory(load=self.directroy_path, cancel=self.dismiss_popup, input_pth=self.ids.drive_dir.text)
        
        self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
        self._popup.open()
        
                
    def directroy_path(self, path, filename):        
        self.ids.text_input_directory.text = str(filename[0])
        self.dismiss_popup()

        
             
kv_main = Builder.load_file('main.kv')

#
class MyApp(App):
    def build(self):
        return kv_main
        

if __name__ == '__main__':
    
    MyApp().run()

main.kv

#:include first.kv

WindowManager:
    FirstWindow:
    
<LoadDialog_Directory>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            dirselect: True
            path: root.input_pth

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()
            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

first.kv

<FirstWindow>
    name: 'first'
    GridLayout:
        cols: 1
        
        BoxLayout:
            orientation: "horizontal"               
            TextInput:
        
        BoxLayout:
            orientation: "horizontal"
            Spinner:    
                id: drive_dir
                halign: 'center'
                option_cls: "MyOption"
                values: root.drive_list()

            Button:
                text: "Set Result Directory"
                on_release: root.show_load_directory()
            TextInput:
                id: text_input_directory
                disabled: True
                text: text_input_directory.text
        
        BoxLayout:
            size_hint: (0.01, 1)
            orientation: "horizontal"       
            TextInput:
Binx
  • 382
  • 7
  • 22