简体   繁体   English

Kivy 从 FloatLayout 继承 ScreenManager

[英]Kivy Inherit ScreenManager from FloatLayout

I am having trouble understanding how to inherit the functionality of one class into another with Kivy.我无法理解如何使用 Kivy 将一个 class 的功能继承到另一个。 I understand the error message ( 'LoadDialog_Directory' object has no attribute 'manager' ), but I'm just not grasping exactly how to fix it.我了解错误消息( 'LoadDialog_Directory' object has no attribute 'manager' ),但我只是不知道如何修复它。 I believe I need to do something with the function below, but ultimately, I do not know.我相信我需要对下面的 function 做点什么,但最终,我不知道。

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 .此脚本的目标是能够将filechooser特定驱动程序作为文件选择器的path 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.我之所以选择这种方法而不是其他方法,是因为大多数人使用的是 Kivy 1.11.1,这个版本有很多不推荐使用的功能,不适用于 2.0。

.py .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第一个.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.旁注:额外空白TextInput的原因是如果Spinner占用过多的应用程序,它将不会 function(显示驱动程序)。

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 class LoadDialog_Directory(FloatLayout)中的新变量 - input_pth
  2. Called input_pth into my content variable within class FirstWindow(Screen) function show_load_directory(self)在 class input_pth class FirstWindow(Screen) function show_load_directory(self)中将 input_pth 调用到我的content变量中
  3. Set my filechooser.path to root.input_pth in the main.kv file.在 main.kv 文件中将我的filechooser.path设置为root.input_pth

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:我不明白content中的input_pth变量如何能够引用class LoadDialog_Directory(FloatLayout)而无需传递类似的内容:

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

.py .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第一个.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:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM