Kivy:将应用程序变成小部件
Kivy: Turning an App into a widget
我使用 'App' class 构建了一个 Kivy GUI,它旨在使用文件选择器加载文件并将其放入列表中。我现在了解到我的整个代码块需要转换为一个小部件,以便它可以集成到现有的 GUI 脚手架中。我不确定如何处理 'widget-ifying' 我的代码。任何帮助将不胜感激。
下面是我的 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)
我不明白这个问题,你的应用程序没有什么特别之处,所有功能都在 MainController 小部件中。您还需要什么分离?
Factory.register('Main', cls=MainController)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
此外,这些 Factory 行是不必要的,小部件会自动注册。
我使用 'App' class 构建了一个 Kivy GUI,它旨在使用文件选择器加载文件并将其放入列表中。我现在了解到我的整个代码块需要转换为一个小部件,以便它可以集成到现有的 GUI 脚手架中。我不确定如何处理 'widget-ifying' 我的代码。任何帮助将不胜感激。
下面是我的 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)
我不明白这个问题,你的应用程序没有什么特别之处,所有功能都在 MainController 小部件中。您还需要什么分离?
Factory.register('Main', cls=MainController)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
此外,这些 Factory 行是不必要的,小部件会自动注册。