[英]How to Dynamically add a button from the .py file to a single screen of a screen manager in .kv file
[英]How to add for each screen an own .py and .kv file?
我希望每個屏幕都有一個單獨的.py和.kv文件。 在main.py/main.kv中的ScreenManager上,應該選擇屏幕。 應該從文件screen_X.kv加載設計,並且應該從文件screen_X.py加載類等。
屏幕:
文件:
這使得程序可以輕松擴展。 我怎樣才能做到這一點?
使用此代碼,我分離了.kv文件。 但我還需要分開.py文件。
main.py
from time import time
from kivy.app import App
from os.path import dirname, join
from kivy.lang import Builder
from kivy.properties import NumericProperty, BooleanProperty, ListProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen
class ShowcaseScreen(Screen):
fullscreen = BooleanProperty(False)
def add_widget(self, *args):
if 'content' in self.ids:
return self.ids.content.add_widget(*args)
return super(ShowcaseScreen, self).add_widget(*args)
class ShowcaseApp(App):
time = NumericProperty(0)
screen_names = ListProperty([])
bool_menu = BooleanProperty(False)
def build(self):
self.title = 'hello world'
Clock.schedule_interval(self._update_clock, 1 / 60.)
self.available_screens = [
'Buttons', 'CheckBoxes', 'ProgressBar', 'Switches', 'ToggleButton',
]
self.screen_names = self.available_screens
curdir = dirname(__file__)
self.available_screens = [join(curdir, 'data', 'screens', '{}.kv'.format(fn)) for fn in self.available_screens]
self.menu_screen = join(curdir, 'data', 'screens', '{}.kv'.format('Menu'))
self.go_menu()
def go_screen(self, idx):
screen = Builder.load_file(self.available_screens[idx])
self.root.ids.sm.switch_to(screen, direction='left')
def go_menu(self):
if not self.bool_menu:
screen = Builder.load_file(self.menu_screen)
self.root.ids.sm.switch_to(screen, direction='right')
def _update_clock(self, dt):
self.time = time()
if __name__ == '__main__':
ShowcaseApp().run()
showcase.kv
#:kivy 1.8.0
#:import KivyLexer kivy.extras.highlight.KivyLexer
#:import Factory kivy.factory.Factory
<ActionSpinnerOptions@SpinnerOption>
background_color: .4, .4, .4, 1
<ActionSpinner@Spinner+ActionItem>
canvas.before:
Color:
rgba: 0.128, 0.128, 0.128, 1
Rectangle:
size: self.size
pos: self.pos
border: 27, 20, 12, 12
background_normal: 'atlas://data/images/defaulttheme/action_group'
option_cls: Factory.ActionSpinnerOptions
<ActionDropdown>:
on_size: self.width = '220dp'
<ShowcaseScreen>:
ScrollView:
do_scroll_x: False
do_scroll_y: False if root.fullscreen else (content.height > root.height - dp(16))
AnchorLayout:
size_hint_y: None
height: root.height if root.fullscreen else max(root.height, content.height)
GridLayout:
id: content
cols: 1
spacing: '8dp'
padding: '8dp'
size_hint: (1, 1) if root.fullscreen else (.8, None)
height: self.height if root.fullscreen else self.minimum_height
BoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
size: self.size
source: 'data/background.png'
ActionBar:
ActionView:
id: av
ActionPrevious:
with_previous: (False if app.bool_menu == True else True) if not app.bool_menu else False
title: 'Menu'
on_release: app.go_menu()
ActionSpinner:
id: spnr
important: True
text: 'Select Program'
values: app.screen_names
on_text:
if (spnr.text != 'Select Program') and (sm.current != args[1]):\
idx = app.screen_names.index(args[1]);\
app.go_screen(idx)
ScreenManager:
id: sm
on_current_screen:
screen_name = args[1].name
spnr.text = 'Select Program' if screen_name == 'Menu' else screen_name
if screen_name == 'Menu': app.bool_menu = True
else: app.bool_menu = False
首先,你沒有提到你是否知道如何以通常的方式設置不同的屏幕,我不確定我是否在上面的代碼中看到它。 如果你不這樣做,有一個相當簡單的教程在這里創建一個簡單的多屏幕設置。
每個屏幕都是一個繼承自Screen
類,在單獨的.py文件中定義這些屏幕類並將它們導入到main.py文件中並不困難。 我之前做過這個。 例如,我有我的main.py(你需要),我在py文件中定義了所有我的各種屏幕,名為'game_screen.py',只是從那里導入。
我自己沒有看到多少kv文件的使用,但我知道你可以使用類似的代碼將kv規則添加到主(默認)kv文件規則中。 我相信,雖然這些規則必須在它們將影響的小部件之前加載才能正常工作。
from kivy.lang import Builder
Builder.load_file('screen1.kv')
Builder.load_file('screen2.kv')
Builder.load_file('screen3.kv')
因此,通過將屏幕類導入main.py並使用上面添加kv文件的方法,您可能可以實現您所要求的...然后決定這是否是一個好方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.