2017-03-19 26 views
0

スクリーンマネージャーにいくつかのスクリーンを入れて、運動ルーチンを登録するアプリを開発してkivyを試しています。リダイレクト時にKivyスクリーンマネージャー「名前のないスクリーン」エラーが発生する

kivy.uix.screenmanager.ScreenManagerException: No Screen with name "('Suplementos', <kivy.uix.button.Button object at 0xb3d3341c>)". 

アプリは現在、次のコードの断片で構成されています(のpython 2.7 + kivy 1.9):コードを実行している場合しかし、私はScreenManagerException取得しています

main.pyを

#coding:utf-8 

from functools import partial 
from kivy.app import App 
from kivy.properties import ObjectProperty, StringProperty 
from kivy.uix.button import Button 
from kivy.uix.label import Label 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.uix.scrollview import ScrollView 

import time 

class TelaInicial(Screen): 
    layout_telainicial = ObjectProperty(None) 

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

     opcoes = [ 
       'Suplementos', 
       'Cardiovascular', 
       'Novo treino' 
       ] 

     for opt in opcoes: 
      Opcao = Button(
          text=str(opt), 
          size_hint_y=None, 
          height=60, 
          ) 
      Opcao.bind(on_press=partial(self.ir_para, opt)) 

      self.ids.layout_telainicial.add_widget(Opcao) 

    def ir_para(self, *args): 
     self.parent.current = str(args) 


class Suplementos(Screen): 
    layout_suplementos = ObjectProperty(None) 

    def __init__(self, **kwargs): 
     hora = time.asctime() 
     super(Suplementos, self).__init__(**kwargs) 
     mostrar_hora = Label(text=str(hora)) 
     self.ids.layout_suplementos.add_widget(mostrar_hora) 

class Cardiovascular(Screen): 
    layout_cardiovascular = ObjectProperty(None) 

    def __init__(self, **kwargs): 
     hora = time.asctime() 
     super(Cardiovascular, self).__init__(**kwargs) 
     mostrar_hora = Label(text=str(hora)) 
     self.ids.layout_cardiovascular.add_widget(mostrar_hora) 

class NovoTreino(Screen): 
    layout_novotreino = ObjectProperty(None) 

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

     AdicionarExercicio = Button(
            text='+', 
            size_hint_y=None, 
            height=60, 
            ) 
     AdicionarExercicio.bind(on_press=lambda a: self.NovoExercicio) 

     self.ids.layout_novotreino.add_widget(AdicionarExercicio) 

    def NovoExercicio(self, *args): 
     self.parent.current = 'Seleção de exercícios' 

class SelecaoExercicio(Screen): 
    pass 

class GerenciadorApp(App): 
    def build(self): 
     MyScreenManager = ScreenManager() 
     MyScreenManager.add_widget(TelaInicial()) 
     MyScreenManager.add_widget(Suplementos()) 
     MyScreenManager.add_widget(Cardiovascular()) 
     MyScreenManager.add_widget(NovoTreino()) 
     MyScreenManager.add_widget(SelecaoExercicio()) 
     return MyScreenManager 

GerenciadorApp().run() 
コードの元のバージョンで

gerenciador.kv

<TelaInicial> 
    name: 'Tela inicial' 
    ScrollView: 
     GridLayout: 
      id: layout_telainicial 
      cols: 1 
      padding: 10 
      spacing: 10 
      size_hint: 1, None 
      height: self.minimum_height 

<Suplementos> 
    name: 'Suplementos' 
    ScrollView: 
     GridLayout: 
      id: layout_suplementos 
      cols: 1 
      padding: 10 
      spacing: 10 
      size_hint: 1, None 
      height: self.minimum_height 
      Label: 
       text: 'Registro de suplementos' 

<Cardiovascular> 
    name: 'Cardiovascular' 
    ScrollView: 
     GridLayout: 
      id: layout_cardiovascular 
      cols: 1 
      padding: 10 
      spacing: 10 
      size_hint: 1, None 
      height: self.minimum_height 
      Label: 
       text: 'Exercício cardiovascular' 

<NovoTreino> 
    name: 'Novo treino' 
    ScrollView: 
     GridLayout: 
      id: layout_novotreino 
      cols: 1 
      padding: 10 
      spacing: 10 
      size_hint: 1, None 
      height: self.minimum_height 
      Label: 
       text: 'Treino de musculação' 

<SelecaoExercicio> 
    name: 'Seleção de exercícios' 
    ScrollView: 
     GridLayout: 
      id: layout_selecaoexercicios 
      cols: 1 
      padding: 10 
      spacing: 10 
      size_hint: 1, None 
      height: self.minimum_height 

、私はまた、ラムダFを試し以下のために、NovoTreino 『:TelaInicialクラスのコンストラクタ内で使用される代わりに、部分的に()のfuctionの慰めは、「on_press」ボタンの

class TelaInicial(Screen): 
    layout_telainicial = ObjectProperty(None) 

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

     opcoes = [ 
       'Suplementos', 
       'Cardiovascular', 
       'Novo treino' 
       ] 

     for opt in opcoes: 
      Opcao = Button(
          text=str(opt), 
          size_hint_y=None, 
          height=60, 
          ) 
      Opcao.bind(on_press=self.ir_para(opt)) 

      self.ids.layout_telainicial.add_widget(Opcao) 

    def ir_para(self, destino, *args): 
     self.parent.current = str(destino) 

これは、オプションのリストには、常に最後の画面を返すプロパティ』をバインドします各ボタンによって識別される画面ではなく、すべてのボタンを選択します。

これらの各ケースでコードが動作しないのはなぜですか?どのように修正できますか?

答えて

1

問題は、argsの最初の項目だけが必要なときに、screenmanagersを現在のargsのリスト全体に設定することです。

>>> print(args) 
('Suplementos', <kivy.uix.button.Button object at 0xb3d3341c>) 
>>> print(args[0]) 
'Suplementos' 

だから、何が必要です:実際には非常にシンプル

def ir_para(self, *args): 
    self.parent.current = str(args[0]) 
+0

HM、、!どうもありがとうございました! –

+0

@BernardoChrispimBarontあなたは幸せです:) – EL3PHANTEN

関連する問題