KIVY python:在python代码中使按钮可点击

Posted

技术标签:

【中文标题】KIVY python:在python代码中使按钮可点击【英文标题】:KIVY python: make button clickable in python code 【发布时间】:2018-04-09 05:18:11 【问题描述】:

我有一个ScrollView,里面有一个GridLayout,里面有10个按钮。 我无法解决我的问题:仅使用 python 文件(无 .kv)将所有按钮添加到网格布局中,因此我需要在创建每个按钮时添加“on_press:something”。 我希望每个按钮在单击时打印其名称('text: something' 属性)。

debug.kv

#: kivy 1.9.1

<AppScreenManager>:
    Home:

Home:

debug.py

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

class AppScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super(AppScreenManager, self).__init__(**kwargs)

class Home(Screen):

    def __init__(self, **kwargs):
        super(Home, self).__init__(**kwargs)
        self.myinit()

    # function that loads 10 buttons on the Home menu (in a ScrollView) when it is launched
    def myinit(self):

        # create some strings
        numbers = [str(i) for i in range(1, 11)]

        # The scrollview will contain this grid layout
        self.layout = GridLayout(cols=1, padding=5, spacing=5, size_hint=(1,None))
        # I don't know why do this line exists but it works x)
        self.layout.bind(minimum_height=self.layout.setter('height'))

        # create 10 buttons
        for number in numbers:
            ### My problem is HERE, under this line, with the on_press property ###
            btn = Button(text=number, on_press=print number, background_color=(.7, .7, .7, 1), color=(1,1,1,1), size=(32,32), size_hint=(1, None))
            # add the button to the grid layout
            self.layout.add_widget(btn)

        # create the scroll view
        self.scrll = ScrollView(size_hint=(1, .6), pos_hint='center_x': .5, 'center_y': .5, do_scroll_x=False)
        # add the grid layout to the scroll view
        self.scrll.add_widget(self.layout)
        # add everything (the scroll view) to the HOME menu
        self.add_widget(self.scrll)




class MyAppli(App):

    def build(self):
        Window.clearcolor = (1,1,1,1)
        return AppScreenManager()

Builder.load_file("debug.kv")
if __name__ == '__main__':
    MyAppli().run()

【问题讨论】:

【参考方案1】:

您传递给 on_press 的可调用对象将接收按钮实例,因为它是参数:

class Home(Screen):
    def button_pressed(self, btn):
        print(btn.text)

    # ...

Button(text=number, on_press=self.button_pressed,  # ...

【讨论】:

【参考方案2】:

其他(恕我直言,更好)方式:您可以创建自己的 MyButton 类作为 Button 类的子类并定义 on_press() 方法:

class MyButton(Button):

    numeric_property = NumericProperty(0)

    def __init__(self, numeric_property, **kwargs):
        super(MyButton, self).__init__(**kwargs)
        self.numeric_property = numeric_property

    def on_press(self):
        print self.numeric_property

然后你可以添加它:

for number in numbers:
    btn = MyButton(text=number, numeric_property=number, background_color=(.7, .7, .7, 1), color=(1,1,1,1), size=(32,32), size_hint=(1, None))
    self.layout.add_widget(btn)

【讨论】:

以上是关于KIVY python:在python代码中使按钮可点击的主要内容,如果未能解决你的问题,请参考以下文章

使用 python 主文件传递按钮 ID:kivy

如何为 python kivy 绘画应用程序添加清除按钮

在 Kivy for Python 中按下按钮时更新标签的文本

在 Kivy for Python 中按下按钮时更新标签的文本

是否可以使按钮透明(Kivy)

将当前单击的按钮作为参数传递给 Kivy (Python) 中的 on_press