###前提・実現したいこと
現状、ファイルの数が4個あります。
- main.py
- test.kv
- tops1
- tops2
main.pyはtest.kvでレイアウトしています。
main.pyからtops1に移動し、tops1からtops2に移動※1しています。
tops1,tops2のレイアウトは各.py内でBuilder.load_string(r'''~''')で指定しています。
<tops2からtops1>,<tops2からmain>など、ページを戻る操作がしたいです。
以下のプログラムだと、インポートできないとエラーが発生します。
importしたプログラムからimportで戻ることはできないのでしょうか?
前質問:[kivy][python]kivyファイルを複数使いたい
[エラー]
Python
1Traceback (most recent call last): 2 File "main.py", line 31, in <module> 3 from tops1 import Tops1 4 File "/Users/admin/Desktop/python/tops1.py", line 24, in <module> 5 from tops2 import Tops2 6 File "/Users/admin/Desktop/python/tops2.py", line 24, in <module> 7 from tops1 import Tops1 8 ImportError: cannot import name Tops1
#####main.py
Python
1〜 import文 省略 〜 2from tops1 import Tops1 3class TextWidget(BoxLayout): 4 def __init__(self, **kwargs): 5 super(TextWidget, self).__init__(**kwargs) 6 pass 7 def buttonClicked_tops(self): 8 self.clear_widgets() 9 tops =Tops1() 10 self.add_widget(tops) 11 12class TestApp(App): 13 def __init__(self, **kwargs): 14 super(TestApp, self).__init__(**kwargs) 15 self.title = 'Apomol' 16 reload(sys) 17 sys.setdefaultencoding('utf-8') 18 Window.clearcolor = [0.999,0.89,0.999,1] 19 20 def build(self): 21 return TextWidget() 22 23if __name__ == '__main__': 24 TestApp().run()
#####test.kv
Python
1TextWidget: # ルートに追加 2<TextWidget>: 3 orientation: 'vertical' 4 ScreenManager: 5 size_hint_y: 0.7 6 id: scrmgr 7 #TOPページ 8 Screen: 9 name: 'top' 10 #Main1ページ 11 Screen: 12 name: 'main1' 13 BoxLayout: 14 orientation:'vertical' 15 size:root.size 16 Label: 17 id:label2 18 font_size:20 19 text:'種類を選択' 20 color:[0.2,0.2,0.2,1] 21 Button: 22 id:button_tops 23 text:'トップス' 24 size_hint_y:0.3 25 on_press:root.buttonClicked_tops() 26 Button: 27 id:button_bottoms 28 text:"ボトムス" 29 size_hint_y:0.3 30 on_press:root.buttonClicked_bottoms() 31 Button: 32 id:button_autor 33 text:"アウター" 34 size_hint_y:0.3 35 on_press:root.buttonClicked_autor() 36 #Main1>bottoms 37 Screen: 38 name: 'bottoms' 39 #Main1>autor 40 Screen: 41 name: 'autor' 42 〜 省略 〜 43 44 #Main2 45 Screen: 46 name: 'main2' 47 〜 省略 〜 48 49 #MENU_bar 50 BoxLayout: 51 size_hint_y: 0.1 52 padding: 10,10,20, 10 53 Button: 54 text: "top" 55 font_size: 20 56 on_press: scrmgr.current = 'top' 57 Button: 58 text: "main1" 59 font_size: 20 60 on_press: scrmgr.current = 'main1' 61 Button: 62 text: "main2" 63 font_size: 20 64 on_press: scrmgr.current = 'main2'
#####tops1.py
Python
1〜 import文 省略 〜 2from tops2 import Tops2 3class Tops1(Widget): 4 def buttonClicked(self): 5 pass 6 def buttonClickedNext(self): 7 self.clear_widget() 8 tops2 = Tops2() 9 self.add_widget(tops2) 10Builder.load_string(r''' 11<Tops1>: 12 Button: 13 text:'< 前のページに戻る' 14 pos:50,550 15 size:200,50 16 Button: 17 text:'[カテゴリ選択]に戻る' 18 pos:300,550 19 size:200,50 20 Button: 21 text:'次のページに進む >' 22 pos:550,550 23 size:200,50 24 on_press:root.buttonClickedNext() 25
#####tops2.py
Python
1〜 import文 省略 〜 2from tops1 import Tops1 3class Tops2(Widget): 4 def buttonClicked(self): 5 pass 6 def buttonClickedBack(self): 7 self.clear_widget() 8 tops1 = Tops1() 9 self.add_widget(tops1) 10 def buttonClickedNext(self): 11 pass 12Builder.load_string(r''' 13<Tops1>: 14 Button: 15 text:'< 前のページに戻る' 16 on_press:root.buttonClickedBack() 17 pos:50,550 18 size:200,50 19 Button: 20 text:'[カテゴリ選択]に戻る' 21 pos:300,550 22 size:200,50 23 Button: 24 text:'次のページに進む >' 25 pos:550,550 26 size:200,50 27
###補足情報(言語/FW/ツール等のバージョンなど)
python2.7
kivy
MacOS
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/21 01:14
2017/11/21 08:30