Why is my screen object "not callable" when trying to display a screen with a button?

时间:2019-04-17 00:45:00

标签: python kivy

I want to display my second screen when a button is pressed but I get an error, TypeError: 'kivy.properties.StringProperty' object is not callable. If I remove the ScreenManager.current('ScreenTwo') from the code then the error goes away, however, I need some way to display the screen after saving the inputted data. Why am I getting this error and how would I fix it?

here is my code:

class TwoLayout(GridLayout):
    def display_btn(self):
        for key in MainApp.store:
            streak_button = Button(text=key)
            ids.streak_zone.add_widget(streak_button)



presentation = Builder.load_file("StreakStar.kv")

class MainApp(App):
    def build(self): # build() returns an instance
        self.store = JsonStore("streak.json")

        return presentation

    # creates the Streak object
    def create(self):
        obj = self.root.get_screen('one') # get info from ScreenOne
        self.streak = Streak(obj.ids.action_entry.text, obj.ids.streak_entry.text,
                            obj.ids.day_entry.text, obj.ids.hour_entry.text,
                            obj.ids.minute_entry.text)

        empty_error = "Make sure to fill out all boxes!"

        popup = Popup(title="Not filled", content=Label(text=empty_error),
                     size_hint=(None, None), size=(300, 100))



        # error handling and calculating total seconds
        parsed = False
        try:
            total = ((int(self.streak.day) * 86400) + (int(self.streak.hour) * 3600) +
                    (int(self.streak.minute) * 60)) # convert into seconds
            parsed = True
            # delete later and replace with return
            print("[seconds:", total,']' , "[action:", self.streak.action,']',
                 "[action number:", self.streak.action_num,']')

            self.store.put(self.streak.action, action=self.streak.action,
                          action_num=self.streak.action_num, seconds=total,
                          score=self.streak.score)

            ScreenManager.current('ScreenTwo')


        except ValueError as error:
            popup.open()

.kv code:

ScreenManagement:
    transition: SlideTransition()
    MainScreen:
    ScreenOne:
    ScreenTwo:
      ...

<TwoLayout>
<ScreenTwo>
    on_pre_enter: TwoLayout.display_btn()
    name: "two"
    GridLayout:
        cols: 2
        rows: 1
        BoxLayout:
            id: streak_zone
            orientation: 'vertical'
        AnchorLayout:
            anchor_x: "center"
            anchor_y: "center"
            Label:
                id: high_lable
                text: "Highest Streak: "
                size_hint: None, None
                font_size: 20


full traceback:

 Traceback (most recent call last):
   File "C:\Users\tonya\Desktop\realProjects\HaStreakual\HaStreakual.py", line 105, in <module>
     MainApp().run()
   File "C:\Python36\lib\site-packages\kivy\app.py", line 826, in run
     runTouchApp()
   File "C:\Python36\lib\site-packages\kivy\base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Python36\lib\site-packages\kivy\core\window\window_sdl2.py", line 727, in mainloop
     self._mainloop()
   File "C:\Python36\lib\site-packages\kivy\core\window\window_sdl2.py", line 460, in _mainloop
     EventLoop.idle()
   File "C:\Python36\lib\site-packages\kivy\base.py", line 340, in idle
     self.dispatch_input()
   File "C:\Python36\lib\site-packages\kivy\base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Python36\lib\site-packages\kivy\base.py", line 231, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Python36\lib\site-packages\kivy\core\window\__init__.py", line 1360, in on_motion
     self.dispatch('on_touch_down', me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Python36\lib\site-packages\kivy\core\window\__init__.py", line 1376, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Python36\lib\site-packages\kivy\uix\screenmanager.py", line 1191, in on_touch_down
     return super(ScreenManager, self).on_touch_down(touch)
   File "C:\Python36\lib\site-packages\kivy\uix\widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Python36\lib\site-packages\kivy\uix\relativelayout.py", line 288, in on_touch_down
     ret = super(RelativeLayout, self).on_touch_down(touch)
   File "C:\Python36\lib\site-packages\kivy\uix\widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Python36\lib\site-packages\kivy\uix\widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Python36\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
   File "C:\Python36\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "C:\Users\tonya\Desktop\realProjects\HaStreakual\StreakStar.kv", line 100, in <module>
     on_press: app.create()
   File "C:\Users\tonya\Desktop\realProjects\HaStreakual\HaStreakual.py", line 84, in create
     ScreenManager.current('ScreenTwo')
 TypeError: 'kivy.properties.StringProperty' object is not callable
[Finished in 17.269s]

1 个答案:

答案 0 :(得分:0)

因为根是我的屏幕,所以我用它来指代Screen,下面是两个代码:

def change_screen(self, *args):
        self.root.current = 'two'
        self.display_btn()

    parsed = False
        try:
            total = ((int(self.streak.day) * 86400) + (int(self.streak.hour) * 3600) +
                    (int(self.streak.minute) * 60)) # convert into seconds
            parsed = True

            # delete later just used to test
            print("[seconds:", total,']' , "[action:", self.streak.action,']',
                 "[action number:", self.streak.action_num,']')

            self.current_time = time.time()
            self.count = self.current_time + total

            # store streak attributes inside "streak.json"
            self.store.put(self.streak.action, action=self.streak.action,
                          action_num=self.streak.action_num, seconds=total,
                          score=self.streak.score, delta=self.count)




            self.change_screen(self)