zoukankan      html  css  js  c++  java
  • 『Python Kivy』什么是Kivy,以及Hello world 丛峻峰 博客园

    『Python Kivy』什么是Kivy,以及Hello world - 丛峻峰 - 博客园

    由于近期突然想要搞一个跨平台的小项目,用于在手机与电脑上使用,所以在网上找了一些开发类似跨平台项目比较好的解决方案。而跟据Kivy官网上对其理念的说明,让我有了一些心动,所以就此写下自己在Kivy上的学习。既算是记笔记,也算是再次开始写博客的一个借口吧。

    1. Kivy是一套Python在UI上的框架。其主要用于现在越来越火的新型用户接口的开发。(新型用户接口指多点触控)
    2. Kivy是完全开源的,包括商业许可。
    3. Kivy标榜自己是Fresh、Fash、Flexiable、Focused、Funded、Free的。

    Kivy的安装


    在此,我只介绍有关Windows的安装,在其他平台下的安装由于我还没有具体的环境,所以也不知道具体会遇到什么问题,就介绍出来了.

    首先,我们要先下载在Windows安装包,里面包含了我们之后所需要的全部的三方类库.

    然后,就是解压到你想要它在的任何地方.

    最后,将解压后的一个批处理文件加到你的右键菜单中.

    1. 找到的解压目录
    2. 找到Kivy.bat文件
    3. 复制这个文件
    4. 在你的资源浏览器中的地址栏中,输入shell:sendto,这会打开一个文件夹
    5. 把你黏贴你复制的Kivy.bat文件的快捷方式在这里
    6. 找到一个 .py 文件,右键→发送到,你应该就可以看到一个Kivy*的选项了

    Hello World


    安装完成Kivy之后,下面让我们按照国际惯例来写一个Hello World程序,以标明从此之后,我们就要开始Kivy之旅了!

    
    import kivy
    kivy.require('1.4.1')
    
    from kivy.app import App 
    from kivy.uix.button import Button 
    
    class MyApp(App):
    	"""docstring for MyApp"""
    	def build(self):
    		return Button(text='Hello World')
    
    if __name__ == '__main__':
    	MyApp().run()

    运行代码


    我们将上面的代码保存为kivy_helloworld.py,然后右键→发送到Kivy*,我们就会看到下面的结果了!

    image

    解释


    关于上面代码的解释,我也只是一知半解的,所以我就直接用官网上面对这段代码的解释了.

    First, we import Kivy, and check if the current installed version will be enough for our application. If not, an exception will be automatically fired, and prevent your application to crash in runtime. You can read the documentation of kivy.require() function for more information.首先,我们导入Kivy,然后检查最近被安装的版本是否支持我们的应用程序。如果不支持,一个异常江北自动抛出,然后保护你的应用程序在运行时崩溃。你可以阅读有关kivy.requier()函数的文档来获得更多的信息。 We import the App class, to be able to subclass it. By subclassing this class, your own class gains several features that we already developed for you to make sure it will be recognized by Kivy.我们引入App类,以便能够继承它。通过继承这个类,你自己的类获得几个我们已经为你开发出来用于确认已经被Kivy认识到得特性。

    Next, we import the Button class, to be able to create an instance of a button with a custom label.下一步,我们导入Button类,以能够创建一个伴随有一个label的按钮的实例。

    Then, we create our application class, based on the App class. We extend the build() function to be able to return an instance of Button. This instance will be used as the root of the widget tree (because we returned it).然后,我们创建我们的基于App类的应用程序类。我们扩展build()方法以便返回一个Button的实例.这个实例被用于作为widget树的根(因为我们返回它).

    Finally, we call run() on our application instance to launch the Kivy process with our application inside.最后,我们再我们的应用程序实例中调用run()以启动Kivy在内部处理我们的应用程序.

  • 相关阅读:
    Spinal Tap Case
    Sorted Union
    Search and Replace
    Boo who
    Missing letters
    DNA Pairing
    Pig Latin
    Where art thou
    Roman Numeral Converter
    Redis高级客户端Lettuce详解
  • 原文地址:https://www.cnblogs.com/lexus/p/2845139.html
Copyright © 2011-2022 走看看