我的目标是拥有多个屏幕,而其中一个屏幕具有自定义按钮。在尝试使用多个屏幕之前,该按钮可以正常工作。我尝试将自定义按钮的小部件添加到屏幕,但现在没有响应。我还尝试过将屏幕作为超级按钮添加到我的自定义按钮中,但是显然那样行不通。
它没有给出错误,我的自定义按钮什么也没做。
这似乎是一个简单的问题,但我找不到任何可比的例子。我对此还很陌生,所以我很想了解我犯的任何愚蠢的错误。
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()
我希望有多个屏幕,同时其中一个屏幕具有自定义按钮。
答案 0 :(得分:0)
我交换了supers ButtonBehavior和Widget,似乎其中一个覆盖了另一个,并确保collide_point函数不起作用。
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