2016-10-19 39 views
2

タイトルに記載されているように - 私は立ち往生しています。私はコードを使って遊んでいて、ScreenManagerとPopupを別々にしておく限り、すべて動く。一度組み合わされると、彼らは協力を拒否します。とにかく、ここに私が持っている問題を示す簡単なアプリです。KivyのScreenManagerとPopupsは一緒に働きたくない

from kivy.lang import Builder 
from kivy.app import App 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.popup import Popup 
from kivy.uix.screenmanager import Screen, ScreenManager 

class First(GridLayout,Screen): 
    def show_popup(self): 
     Popp().open() 
    pass 

class Second(Screen): 
    pass 

class Popp(Popup): 
    pass 

class ScreenManagement(ScreenManager): 
    pass 

app = Builder.load_file("main.kv") 

class MainApp(App): 
    def build(self): 
     return app 


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

そしてmain.kvファイル

ScreenManagement: 
    First: 
    Second: 

<First>: 
    name:"First" 
    rows: 2 
    Button: 
     text: "FirstButton" 
     on_release: app.root.current = "Second" 
    Button: 
     text: "Show popup" 
     on_release: root.show_popup() 

<Second>: 
    name:"Second" 
    Button: 
     text: "BUTTON" 
     on_release: app.root.current = "First" 

<Popp>: 
    title: "testing" 
    text: "Hello world" 
    size_hint: None,None 
    size: 400,400 
    auto_dismiss: False 
    Button: 
     text: "Okay" 
     on_press: root.dismiss() 

アプリケーションを開始し、第一及び第二の画面が作業しているが、最大のポップアップを取得しようとしたとき、私はで終わる:

kivy.uix.popup.PopupException: Popup can have only one widget as content 

何とか画面ですポップの中のウィジェットとして見られる?それとも、私はひどくKivyドキュメントを誤解していますか?

答えて

1

これはkvファイルをロードする際のバグです。この場合は例外がスローされます。

あなたがコードで行っていることは、kvファイルを2回読み込んでいます。これは何らかの奇妙な動作を引き起こします。 Builder.load_file(..)を削除するだけで問題ありません。ファイルは自動的にロードされます。

また、class First(GridLayout, Screen)のようなウィジェットの二重サブクラス化は、何らかの問題を引き起こす可能性があります。代わりに、画面内にグリッドレイアウトを作成します。

0

ポップアップ内の要素をレイアウト内に配置します(例:Boxlayout)。ここで

が私の意味は次のとおりです。

<Popp>: 
    title: "testing" 
    BoxLayout: 
     orientation: 'vertical' 
     size_hint: None,None 
     size: 400,400 
     Label: 
      text: "Hello world" 
     Button: 
      text: "Okay" 
      on_press: root.dismiss() 
+0

それがdownvotedされるのはなぜ?誰でも私を説明できますか?私はそれから学びたいと思います。 – picibucor

0

私はkivy Builder.load_fileとポップアップを使用して同じ問題を抱えている、彼らは一緒に仕事いけません。 解決策は簡単です、ポストビルドをポストしてください。これは、ロードポップアップ例です:

のpython:

from kivy.app import App 
    from kivy.uix.popup import Popup 
    from kivy.factory import Factory 
    from kivy.properties import ObjectProperty 
    from kivy.clock import Clock 
    from kivy.lang import Builder 
    from kivy.uix.popup import Popup 
    from kivy.uix.image import Image 
    from kivy.uix.label import Label 
    from kivy.uix.boxlayout import BoxLayout 

    import time, threading 


    buildKV = Builder.load_file("example.kv") 

    class ExampleApp(App): 
     def show_popup(self): 
      content = BoxLayout(orientation= "vertical") 
      image=Image(source= 'files/loading.gif', anim_delay= 0) 
      label=Label(text= 'Model is Running.\nBe Patient Please.') 
      content.add_widget(image) 
      content.add_widget(label) 
      self.popup = Popup(title='Model is Running.', 
         size_hint=(.250, .785), 
         content=content, auto_dismiss=False) 

      self.popup.open() 

     def process_button_click(self): 
      # Open the pop up 
      self.show_popup() 

      # Call some method that may take a while to run. 
      # I'm using a thread to simulate this 
      mythread = threading.Thread(target=self.something_that_takes_5_seconds_to_run) 
      mythread.start() 

     def something_that_takes_5_seconds_to_run(self): 
      thistime = time.time() 
      while thistime + 10 > time.time(): # 5 seconds 
       time.sleep(1) 

      # Once the long running task is done, close the pop up. 
      self.pop_up.dismiss() 

    if __name__ == "__main__": 
     ExampleApp().run() 

がkivy:

BoxLayout: 
     Button: 
      height: 40 
      width: 100 
      size_hint: (None, None) 
      text: 'Click Me' 
      on_press: app.process_button_click() 
関連する問題