我的目标是拥有多个屏幕,而我的一个屏幕有一个自定义按钮。在我尝试使用多个屏幕之前,该按钮工作正常。我尝试将自定义按钮的小部件添加到屏幕,但现在它没有响应。我也尝试将屏幕作为超级添加到我的自定义按钮,但显然它不起作用。
它没有给出错误,我的自定义按钮没有做任何事情。
这似乎是一个简单的问题,但我找不到任何可比较的例子。我对此还是很陌生,所以我很想了解我犯的任何愚蠢错误。
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()
我想拥有多个屏幕,同时在其中一个屏幕上有一个功能正常的自定义按钮。