Kivy:将应用程序变成一个小部件

时间:2015-02-20 06:12:59

标签: python widget kivy

我使用' App'构建了一个Kivy GUI。用于使用文件选择器加载文件并将其放入列表的类。我现在已经了解到,我的整个代码块需要转换为小部件,以便可以将其集成到现有的GUI支架中。我不知道如何去做小工具。我的代码。任何帮助将不胜感激。

以下是我的main.py文件:



from kivy.app import App
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.factory import Factory

from kivy.uix.popup import Popup
from kivy.uix.button import Button
import os
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.graphics import *
from kivy.properties import ObjectProperty

class OptionMenu(GridLayout):

    loadfile = ObjectProperty(None)
    savefile = ObjectProperty(None)
    text_input = ObjectProperty(None)
    def dismiss_popup(self):
        self._popup.dismiss()

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename):
        with open(os.path.join(path, filename[0])) as stream:
            self.text_input.text = stream.read()
        self.dismiss_popup()   

    def show_save(self):
        content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
        self._popup = Popup(title="Save file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def save(self, path, filename):
        with open(os.path.join(path, filename), 'w') as stream:
            stream.write(self.text_input.text)

        self.dismiss_popup()
class GraphMenu(GridLayout):
    pass
class UnnamedMenu(GridLayout):
    pass

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

class SaveDialog(FloatLayout):
    save = ObjectProperty(None)
    text_input = ObjectProperty(None)
    cancel = ObjectProperty(None)



class MainController(FloatLayout):
    pass


class MainApp(App):
    def build(self):
        rt = MainController()
        return rt
Factory.register('Main', cls=MainController)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)

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




这是我的main.kv文件:



#kivy 1.0
<OptionMenu>
    cols: 2
	row:10
    Button:
        size_hint_y: 0.05
        text: "Input File"
        on_release: self.parent.show_load()
	Button:
		size_hint_y:0.05
		text:"Save File"
		on_release:self.parent.show_save()
    TextInput:
        id: text_input
		text:''
	RstDocument:
		text: text_input.text
		show_errors: True

<MainController>
    BoxLayout:
        orientation: 'horizontal'
        Accordion:
            size_hint_x: 0.3
            orientation: 'vertical'
            AccordionItem:
                title:'Options'
                OptionMenu:
                    id: 'AppOpt'
			AccordionItem:
				title:'Config'
				UnnamedMenu:
					id:ConfigTab
			AccordionItem:
				title:'Graph'
				GraphMenu:
					id:GraphTab

        FloatLayout:
            size_hint_x: 0.7
            Label:
                
<scroller>

        
<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 10
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)
<SaveDialog>:
    text_input: text_input
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            on_selection: text_input.text = self.selection and self.selection[0] or ''

        TextInput:
            id: text_input
            size_hint_y: None
            height: 30
            multiline: False

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Save"
                on_release: root.save(filechooser.path, text_input.text)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我不明白这个问题,你的App没有什么特别的,所有功能都在MainController小部件中。你还需要什么分离呢?

Factory.register('Main', cls=MainController)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)

此外,这些Factory行是不必要的,小部件会自动注册。