简体   繁体   English

有人可以告诉我这种结构在哪里出错吗?

[英]Can someone tell me where I'm going wrong with this structure?

I'm trying to call a function from my .kv but I cannot find the correct way to reference a function outside the function that draws the widgets. 我正在尝试从我的.kv调用函数,但是我找不到在绘制小部件的函数之外引用函数的正确方法。 I've tried root.dostuff parent... self... MyApp... App... I could put the function into the Widgets class, but that breaks other stuff... 我已经尝试过root.dostuff父...我...我的App ...我的应用...我可以将该函数放入Widgets类中,但这会破坏其他内容...

MyApp.py MyApp.py

class Widgets(Widget):
    pass

def dostuff(x):
    print(x)

class MyApp(App):
    def build(self):
        global w
        print("Build")
        w = Widgets()
        return w

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

MyApp.kv: MyApp.kv:

Button:
    text: "Press Me"
    on_press: dostuff(1)

You have two problems. 你有两个问题。 The first is that the function dostuff isn't defined in the kv file. 首先是函数dostuff未在kv文件中定义。 You can import it with #:import dostuff MyApp.dostuff or make it a method of eg the app class and call it with app.dostuff() . 您可以使用#:import dostuff MyApp.dostuff导入它,或使其成为例如app类的方法,然后使用app.dostuff()调用。

Also, your kv file isn't actually loaded. 另外,您的kv文件实际上并未加载。 To have it be loaded and you don't display the button it would produce, so your example wouldn't actually demonstrate your problem. 要加载它,并且不显示它会产生的按钮,因此您的示例实际上不会演示您的问题。 Name the file my.kv to have it loaded automatically, and don't return anything from the build methd to have your Button used as the root widget. 将文件my.kvmy.kv使其自动加载,并且不从构建方法返回任何内容以将Button用作根窗口小部件。

the correct way to reference a function outside the function that draws the widgets. 在绘制窗口小部件的函数之外引用函数的正确方法。

You can also define on_press() outside the kv file : 您还define on_press() outside the kv file

from kivy.uix.button import Button
from kivy.app import App

def dostuff(x):
    print("x is %s" % x)


class MyButton(Button):
    def on_press(self):
        dostuff(22)

class MyApp(App):

    def build(self):
        return MyButton()

MyApp().run()

my.kv: my.kv:

<MyButton>:
    text: "Press Me"

Or, with on_press() inside the kv file : 或者, on_press() inside the kv file使用on_press() inside the kv file

my.kv: my.kv:

<MyButton>:
    text: "Press Me"
    on_press: self.dostuff(10, 20)  #Look in MyButton class for dostuff()

...
...

class MyButton(Button):
    def dostuff(self, *args):
        print(args)

...
...

I've tried root.dostuff parent... self... MyApp... App. 我已经尝试过root.dostuff父母...自我... MyApp ...应用程序。

Here's how root and app work in a kv file: rootapp在kv文件中的工作方式如下:

my.kv: my.kv:

<MyWidget>: #This class is the 'root' of the following widget hierarchy:
    Button:
        text: "Press Me"
        on_press: root.dostuff(20, 'hello') #Look in the MyWidget class for dostuff()
        size: root.size  #fill MyWidget with the Button

from kivy.uix.widget import Widget
from kivy.app import App

class MyWidget(Widget):
    def dostuff(self, *args):
        print(args)

class MyApp(App):

    def build(self):
        return MyWidget()

MyApp().run()

Or, you can put the function inside the App class : 或者,您可以put the function inside the App class

my.kv: my.kv:

<MyButton>:
    text: "Press Me"
    on_press: app.dostuff('hello', 22)

from kivy.app import App
from kivy.uix.button import Button

class MyButton(Button):
    pass

class MyApp(App):
    def dostuff(self, *args):
        print(args)

    def build(self):
        return MyButton()

MyApp().run()

I could put the function into the Widgets class, but that breaks other stuff... 我可以将函数放入Widgets类中,但这会破坏其他内容。

Well, don't let the function do that. 好吧,不要让函数那样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我我做错了什么[暂停] - Can someone tell me what I'm doing wrong [on hold] 索引超出范围错误,有人可以告诉我它出了什么问题 - Index out of range error , Can someone please tell me where Its going wrong 有人可以告诉我这个 python 代码哪里错了吗? - Can someone please tell me where am I wrong in this python code? 有人可以告诉我我做错了什么吗? - Can someone tell me what am I doing wrong? 我无法在Python中正确定义一个函数。 谁能告诉我我要去哪里错了? - I can't properly define a function in Python. Can anyone tell me where I am going wrong? 有人能告诉我为什么这是错误的吗? - can someone tell me why this is wrong? 我们需要找到两条对角线之和之间的绝对差。 请你能告诉我哪里出错了吗 - we need to find the aboslute difference between sum of 2 diagonals. Please can u tell me where i am going wrong 谁能告诉我这个二进制搜索代码哪里出错了? 无法向用户打印索引 - Can anyone please advise me where I'm going wrong with this binary search code? Unable to print index to user 有人能告诉我出了什么问题,当我运行它时,浏览器说“无法访问此站点” - Can someone tell me what's wrong, when I run it the browsers says "This site can’t be reached" 有人可以告诉我如何解决此错误,在这里我正在使用tensorflow fashion_mnish数据集 - Can someone tell me how to fix this error , here I'm working with tensorflow fashion_mnish dataset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM