My goal is to have multiple screens while one of my screens has a custom button. The button works fine before i tried working with multiple screens. I tried adding the widget of the custom button to the screen but now it doesn't respond. I've also tried adding screen as a super to my custom button, but apparently it doesn't work that way.
It doesn't give an error, my custom button just doesn't do anything.
It seems like a simple problem, but i can't find any comparable examples. I'm still pretty new to this so i would love to learn about any silly mistakes i made.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.properties import NumericProperty
from kivy.lang import Builder
from kivy.vector import Vector
from kivy.base import runTouchApp
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Line
from kivy.uix.dropdown import DropDown
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<HomeScreenLogic>:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba: ((25/255, 181/255, 254/255, 1) if self.state == "normal" else (137/255, 196/255, 244/255, 1))
Ellipse:
size: root.width/4, root.width/4
pos: 0.5*root.width - root.width/8, root.height / 8
Color:
rgba: 1, 1, 1, 1
Line:
width: 2
points: [root.width/2-root.width/12, root.height/8+root.width/8,root.width/2+root.width/12, root.height/8+root.width/8]
Color:
rgba: 1, 1, 1, 1
Line:
width: 2
points: [root.width/2, root.height/8+root.width/8+root.width/12,root.width/2, root.height/8+root.width/8-root.width/12]
<AddScreen>:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
""")
class HomeScreenLogic(Widget, ButtonBehavior):
def collide_point(self, x, y):
if Vector(x, y).distance((Window.size[0]/2, 7*Window.size[1]/24)) <= Window.size[0]/8:
print('True')
return True
return False
class HomeScreen(Screen):
def __init__(self, **kwargs):
super(HomeScreen, self).__init__(**kwargs)
self.Logic = HomeScreenLogic()
self.add_widget(self.Logic)
class AddScreen(Screen):
pass
SM = ScreenManager()
SM.add_widget(HomeScreen(name='Home'))
SM.add_widget(AddScreen(name='Add'))
class HelloWorldApp(App):
def build(self):
return SM
if __name__ == "__main__":
HelloWorldApp().run()
I would like to have multiple screens while having a functioning custom button in one of them.
I swapped the supers ButtonBehavior and Widget, it seems that one was over-riding the other and made sure the collide_point function didn't work.
class HomeScreenLogic(ButtonBehavior, Widget):
def collide_point(self, x, y):
if Vector(x, y).distance((Window.size[0]/2, 7*Window.size[1]/24)) <= Window.size[0]/8:
print('True')
return True
return False