[英]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.kv
为my.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: root
和app
在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.