Python Kivy按钮未按预期工作
几天前我开始制作我的第一款kivy应用程序,直到有一天一切都很顺利 我有一个标签,里面有GridLayout和256个按钮(Python Kivy按钮未按预期工作,python,kivy,Python,Kivy,几天前我开始制作我的第一款kivy应用程序,直到有一天一切都很顺利 我有一个标签,里面有GridLayout和256个按钮(PaletteColorButton),它们应该代表调色板。我为这个类创建了一个on\u touch\u down方法,并尝试单击任何按钮,它们都会执行on\u touch\u down中的内容 以下是我的代码中最重要的部分: class PaletteLabel(ToolBarLabel): def make_palette(self, layout):
PaletteColorButton
),它们应该代表调色板。我为这个类创建了一个on\u touch\u down
方法,并尝试单击任何按钮,它们都会执行on\u touch\u down中的内容
以下是我的代码中最重要的部分:
class PaletteLabel(ToolBarLabel):
def make_palette(self, layout):
for i in range(0, 256):
palette_color_button = PaletteColorButton()
with palette_color_button.canvas:
Color(i/255, i/255, i/255, 1)
layout.add_widget(palette_color_button)
class PaletteColorButton(Button):
def on_touch_down(self, touch):
if touch.is_double_tap:
print(self.pos)
class BamEditor(App):
Config.set('kivy', 'window_icon', r'.\static\program_icon\BamEditor-icon.png')
def build(self):
main_label = MainLabel()
main_label.ids['palettelabel'].make_palette(main_label.ids['palettelayout'])
return main_label
以下是.kv文件中的数据:
<PaletteLabel>:
height: 160
width: 640
<PaletteColorButton>:
size:(20,20)
canvas.after:
Rectangle:
pos: self.x + 1 , self.y + 1
size: self.width - 2, self.height - 2
<MainLabel>:
PaletteLabel:
id: palettelabel
pos: (self.parent.x + 120, self.parent.y + 20)
GridLayout:
id: palettelayout
cols: 32
rows: 8
pos: self.parent.x , self.parent.y
size: self.parent.width, self.parent.height
:
身高:160
宽度:640
:
尺寸:(20,20)
在下列情况之后:
矩形:
位置:自x+1,自y+1
尺寸:self.width-2,self.height-2
:
调色板标签:
id:palettelabel
位置:(自父x+120,自父y+20)
网格布局:
id:调色板布局
科尔斯:32
行数:8
位置:self.parent.x,self.parent.y
大小:self.parent.width、self.parent.height
我只想打印点击按钮的pos
,但我得到所有256个按钮的位置,有人知道如何实现吗?Ofc,我可以在按下按钮时使用,它可以工作,但我希望我的按钮在点击一次时有不同的行为,在点击两次时有不同的行为。感谢您的帮助。摘自《kivy编程指南》:
默认情况下,触摸事件会发送到当前显示的所有屏幕
小部件。这意味着无论触摸事件是否发生,小部件都会收到触摸事件
是否在其物理区域内
[……]
为了提供最大的灵活性,Kivy将
事件,并让它们决定如何对它们作出反应。
如果您只想响应小部件内的触摸事件,您可以
只需检查:
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
# The touch has occurred inside the widgets area. Do stuff!
pass
当您不想管理此按钮中的触摸时(因此当冲突测试失败时),您应该让事件分派到小部件树的其余部分
return super(WidgetClass, self).on_touch_down(touch)
从《kivy编程指南》中:
默认情况下,触摸事件会发送到当前显示的所有屏幕
小部件。这意味着无论触摸事件是否发生,小部件都会收到触摸事件
是否在其物理区域内
[……]
为了提供最大的灵活性,Kivy将
事件,并让它们决定如何对它们作出反应。
如果您只想响应小部件内的触摸事件,您可以
只需检查:
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
# The touch has occurred inside the widgets area. Do stuff!
pass
当您不想管理此按钮中的触摸时(因此当冲突测试失败时),您应该让事件分派到小部件树的其余部分
return super(WidgetClass, self).on_touch_down(touch)
好的,这太有趣了!这也许可以解释为什么当我点击一个按钮,另一个按钮被激活时,这是非常有趣的!这也许可以解释为什么当我点击1个按钮时,其他按钮也会被激活