【问题标题】:Linking a Kivy Button to Function将 Kivy 按钮链接到函数
【发布时间】:2017-01-10 21:41:49
【问题描述】:

kivy 中有些东西我不明白,希望有人能解释一下。我已经阅读了很多关于这个主题的内容,但它似乎并没有在我的脑海中联系起来。

我的问题来自将函数链接到 kivy 按钮。 现在我正在尝试学习如何做一个简单的功能:

def Math():
    print 1+1

我想做一些更复杂的事情:

def Math(a,b):
    print a^2 + b^2

其中ab 是来自kivy 的输入标签,单击按钮后将打印答案。

这是我目前所拥有的:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout


#######``Logics``#######
class Math(FloatLayout):
    def add(self):
        print 1+1

#######``Windows``#######
class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
   pass

class ScreenManagement(ScreenManager):
   pass


presentation = Builder.load_file("GUI_Style.kv")

class MainApp(App):
    def build(self):
       return presentation

if __name__ == "__main__":
    MainApp().run()

这是我的 kivy 语言文件:

import NoTransition kivy.uix.screenmanager.NoTransition

ScreenManagement:
    transition: NoTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    FloatLayout:
        Button:
            on_release: app.root.current = "other"
            text: "Next Screen"
            font_size: 50
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            pos_hint: {"right":1, "top":1}

<AnotherScreen>:
    name: "other"
    FloatLayout:
        Button:
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            text: "add"
            pos_hint: {"x":0, "y":0}
            on_release: root.add
        Button:
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            text: "Back Home"
            on_release: app.root.current = "main"
            pos_hint: {"right":1, "top":1}

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:
    <AnotherScreen>:
        name: "other"
        FloatLayout: 
            Button:
                ...
                on_release: root.add <-- here *root* evaluates to the top widget in the rule.
    

    这是一个 AnotherScreen 实例,但它没有 add 方法。

    class Math(FloatLayout):
        def add(self):
            print 1+1
    

    在这里,您通过继承FloatLayout 声明了一个数学类,这是一个uix 组件-a 小部件-。你在这个类add上定义了一个方法。还是你没用过。在你使用FloatLayout的kv文件中。

    现在,为了让您访问 kv 中的函数,大多数时候您将通过使用 root/selfapp 将其作为 uix 组件的方法访问,您还可以导入它,例如:

    #: import get_color_from_hex kivy.utils.get_color_from_hex
    <ColoredWidget>:
        canvas:
            Color: 
                rgba: get_color_from_hex("DCDCDC")
            Rectangle:
                size: self.size
                pos: self.pos 
    

    所以你不能这样做:

    <AnotherScreen>:
        name: "other"
        Math:
            id: math_layout
            Button:
                ...
                on_release: math_layout.add()
    

    或者像这样:

    class AnotherScreen(Screen):
       def add(self):
           print(1+1)
    

    如果您仍然有关于这个话题的问题,我很乐意提供更多帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多