[英]How do I create a button, and assign a function to it? Python, Kivy
I am a beginner in Python and Kivy. I want to create a simple program that should have an exit button on a window that must exit the app when pressed.我是 Python 和 Kivy 的初学者。我想创建一个简单的程序,它应该在 window 上有一个退出按钮,按下时必须退出应用程序。 Hi there, please help me.你好,请帮助我。 It's a request, please keep the code simple for a beginner.这是一个请求,请为初学者保持代码简单。 ^_^ ^_^
Okay, I don't know how your code is, but this code could be copy-pasted as is and it will work (just import the necessary modules from kivy though):好吧,我不知道你的代码是怎样的,但这段代码可以按原样复制粘贴并且它会工作(不过只需从 kivy 导入必要的模块):
.py .py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class yourscreen(FloatLayout):
def __init__(self, **kwargs):
#NEEDED
super(yourscreen, self).__init__(**kwargs)
self.button = Button(
text='exit',
size_hint=(0.5, 0.2)
)
self.button.bind(on_release= lambda x:self.exit())
self.add_widget(self.button)
def exit(self):
return MyApp().stop()
class MyApp(App):
def build(self):
return yourscreen()
if __name__ == '__main__':
MyApp().run()
You could pull out what you need which is MyApp().stop()
你可以拿出你需要的东西MyApp().stop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.