2015-01-20 3 views
0

두 화면으로 kivy 프로그램을 만들고 싶었습니다. 첫 번째 화면에는 다음 화면으로 이동할 수있는 단추가 하나만 있습니다. 두 번째 화면에는 첫 번째 화면으로 돌아가는 버튼과 화면 전체에 이미지를 표시하는 버튼이 있습니다. 이미지를 만들고 상자 레이아웃에 추가하는 함수를 작성하여이 작업을 수행했습니다. 나는 이미지가 전체 화면으로 표시되지는 않지만 더 나은 해결책을 알지 못했다는 것을 알고있다. 그러나 버튼을 클릭하면 전체 프로그램이 중단되었습니다. 다음은 소스 코드 :kivy로 이미지를 표시 할 수 없음

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.properties import StringProperty 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.image import Image 

class Manager(ScreenManager): 
    pass 
class FirstScreen(Screen): 

    def change_text(self): 

     self.manager.current = 'second' 
class SecondScreen(Screen): 
    def display_image(self): 
     bild = Image(source='Bild1.png') 
     box1.add_widget(bild) 
root_widget = Builder.load_string(''' 
Manager: 
    FirstScreen: 
    SecondScreen: 
<FirstScreen>: 
    name: 'first' 
    BoxLayout: 
     orientation: 'vertical' 
     Button: 
      id: b1 
      text: 'Go to next Screen' 
      on_release: root.change_text() 
<SecondScreen>: 
    name: 'second' 
    BoxLayout: 
     id: box1 
     orientation: 'vertical' 

     Button: 
      id: b2 
      text: 'Go back' 
      on_release: app.root.current = 'first' 
     Button: 
      id: b3 
      text: 'suprise' 
      on_release: root.display_image() 
''') 
class Caption(App): 
    def build(self): 
     return root_widget 

Caption().run() 

그리고 여기에 오류 메시지입니다 : 루트 규칙 위젯에 ids 객체를 통해 그들에 액세스해야 ID를 사용하려면

File "<string>", line 26, in <module> 
File "img.py", line 18, in display_image 
    box1.add_widget(bild) 
NameError: global name 'box1' is not defined 
+0

추적 기록을 남겨줄 수 있습니까? – Duikboot

+0

좋습니다, 여기에 오류가 있습니다 – Gilgamesch

답변

2

. 따라서 box1.add_widget(bild) 대신 self.ids.box1.add_widget(bild)을 사용해야합니다.

+0

감사합니다. – Gilgamesch

관련 문제