Kivy tutorial 001: Say Hello

Kivy tutorial 001: Say Hello – Kivy Blog

Central themes: Starting an App, getting Kivy running

中心主题: 开始一个App, 让Kivy跑起来

It’s essentially compulsory that the introduction to any programming project should be a “Hello World!” application. Since Kivy is a GUI framework, that means opening a window and displaying the words on the screen. Start by adding each of the following lines to your program:
介绍任何项目对象应该是一个"Hello World!"应用程序时 有必要并且重要的。 因为Kivy时一个GUI框架,这意味着打开一个窗口, 在屏幕上展示文字。通过在你的项目中添加下面的行开始:

from kivy.app import App

This imports the App class, which you’ll use as the core object of any Kivy application. Your instance of this class will create the Kivy window and serve as the top level of your application
这导入了App类, 这类你将使用作为任何app应用的核心对象。 你的这个类的实例将创建kivy窗口,并且作为你应用程序的顶级层次服务。

from kivy.uix.label import Label

This next import introduces one of Kivy’s most important components; the Widget. Your entire application will be built with Widgets, each of which does a single (relatively) small task. For instance, Label is a Widget that displays some text, Button is (obviously) a button, and Layouts are Widgets that contain other Widgets and control their positions according to certain rules.
下一步导入kivy最重要的介绍部分;组件。 你的整的应用程序将同组件一起被建立 每个组件赶一个简单的(相对来说)小的任务。 作为一个实例,Label标签是一个展示一些text文本的组件, Button按钮是(显然)是一个按钮,并且布局也是组件,用来包含其它组件和根据特定的规则控制它们位置的组件。

You can find the documentation for Label here. We’ll need this later!.
你可以找到Label的文档。 我们等会需要它!

In every Kivy application, your first task is to create an App subclass as follows:
在每个Kivy应用程序中,你的第一个任务是像下面一样创建一个APP子类:

class YourApp(App):
    def build(self):
        root_widget = Label(text='Hello world!')
        return root_widget

The build method is the only important addition you have to make, and is your application’s entry point. This method must instantiate and return what will be your root widget, the top level widget of your Kivy application, which will in turn contain all your other gui objects.
build方法是唯一你不得不做的重要的添加,并且这是你应用程序的入口。 这方法必须实例化,和返回  什么是你的  根组件, 这根组件 是你应用程序的top level 上层, 这将按次序包含所有你其他的gui对象。

The root widget will automatically fill the window, so in this case the Label text will appear right in the middle.
根部件 将自动地填充窗口,因此在这个案例中 标签文本将表现在中右边。

In our case, the application only needs a single Widget; the Label displaying our text. We set the text by simply passing it as an argument. This works automatically because text is a Kivy property of the Label widget…more on that later.
在我们的案例中,应用程序只需要一个简单地组件; 标签Label 展示着我们的text文本。 我们设置text文本通过作为一个变量简单地传递给它。 这自动地运行因为text是Label标签组件的kivy属性。。。。你不懂的话等会再说。

Finally, add a line of code to start the app:
终于,添加了一条行代码来开始程序:

YourApp().run()

This instantiates and runs the instance of your App. Any Kivy application is created and started with some variation of these basic steps.
这实例化 并且运行你app的实例。 任何kivy应用程序被创造 并且同一些这样简单的步骤的变动开始。

Now…run the code!  现在,, 运行代码

python your_filename.py

You should see a Window something like the following image. Congratulations, you’ve written and run your first Kivy application.
你应该看到一个窗口像下面图片这样。 祝贺, 你已经写完 并且运行了你的第一个kivy应用程序。

Hello world application

Next tutorial: Improving appearances, customising widgets using Kivy Properties

Full code

your_filename.py:

from kivy.app import App
from kivy.uix.label import Label

class YourApp(App):
    def build(self):
      root_widget = Label(text='Hello world!')
      return root_widget

YourApp().run()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xinzheng新政

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值