有人能告诉我这个结构哪里出了问题吗?
Can someone tell me where I'm going wrong with this structure?
我正在尝试从我的 .kv 中调用一个函数,但我找不到在绘制小部件的函数之外引用函数的正确方法。我试过 root.dostuff parent...self...MyApp...App...我可以将函数放入 Widgets class,但这会破坏其他东西...
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:
Button:
text: "Press Me"
on_press: dostuff(1)
你有两个问题。第一个是函数 dostuff
没有在 kv 文件中定义。您可以使用 #:import dostuff MyApp.dostuff
导入它或使其成为例如应用 class 并使用 app.dostuff()
.
调用它
此外,您的 kv 文件实际上并未加载。要加载它并且你不显示它会产生的按钮,所以你的例子实际上不会证明你的问题。将文件命名为 my.kv
以使其自动加载,并且不要 return 构建方法中的任何内容以将 Button 用作根小部件。
the correct way to reference a function outside the function that
draws the widgets.
你也可以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:
<MyButton>:
text: "Press Me"
或者,on_press() inside the kv file
:
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
和 app
在 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()
或者,您可以 put the function inside the App class
:
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...
好吧,不要让函数那样做。
我正在尝试从我的 .kv 中调用一个函数,但我找不到在绘制小部件的函数之外引用函数的正确方法。我试过 root.dostuff parent...self...MyApp...App...我可以将函数放入 Widgets class,但这会破坏其他东西...
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:
Button:
text: "Press Me"
on_press: dostuff(1)
你有两个问题。第一个是函数 dostuff
没有在 kv 文件中定义。您可以使用 #:import dostuff MyApp.dostuff
导入它或使其成为例如应用 class 并使用 app.dostuff()
.
此外,您的 kv 文件实际上并未加载。要加载它并且你不显示它会产生的按钮,所以你的例子实际上不会证明你的问题。将文件命名为 my.kv
以使其自动加载,并且不要 return 构建方法中的任何内容以将 Button 用作根小部件。
the correct way to reference a function outside the function that draws the widgets.
你也可以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:
<MyButton>:
text: "Press Me"
或者,on_press() inside the kv file
:
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
和 app
在 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()
或者,您可以 put the function inside the App class
:
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...
好吧,不要让函数那样做。