Why Is Switching Screens Not Working In Kivy?
Solution 1:
It appears that you are confusing classes and instances. In your switch_button()
method your code:
ScreenManager.current = "FileScreen"
is setting the current
attribute of the ScreenManager
class, but the current
property is an instance property and must be set on an instance of ScreenManager
. And it must be the instance that is managing the FileScreen
Screen
.
A better coding of the switch_button()
method:
classMainScreen(Screen):
defswitch_button(self):
print("switching")
self.manager.current = "FileScreen"
The self.manager
is a reference to the ScreenManager
that is managing the MainScreen
, which is also managing the FileScreen
.
Elsewhere, you are making a similar confusion between class and instance:
MainScreen.switch_button(self)
Again, you need the instance of MainScreen
, not the MainScreen
class. This line can be replaced by:
self.root.get_screen('MainScreen').switch_button()
This code uses the get_screen()
method of ScreenManager
to access the instance of MainScreen
, then calls the instance method switch_button()
.
A more direct approach would be to replace that line with:
self.root.current = 'FileScreen'
Post a Comment for "Why Is Switching Screens Not Working In Kivy?"