《kivy官方教程》由会员分享,可在线阅读,更多相关《kivy官方教程(62页珍藏版)》请在金锄头文库上搜索。
1、QuickstartThis page explains how to create a simple Kivy “Hello world” program. This assumes you already have Kivy installed. If you do not, head over to the Installation section. We also assume basic Python 2.x knowledge throughout the rest of this documentation.Create an applicationThe base code f
2、or creating an application looks like this:import kivykivy.require(1.0.6) # replace with your current kivy version !from kivy.app import Appfrom kivy.uix.button import Buttonclass MyApp(App): def build(self): return Button(text=Hello World)if _name_ = _main_: MyApp().run()Save it as main.py.To run t
3、he application, follow the instructions for your operating system:LinuxFollow the instructions for running Kivy application on Linux:$ python main.pyWindowsFollow the instructions for running Kivy application on Windows:$ python main.py# orC:appdirkivy.bat main.pyMac OS XFollow the instructions for
4、running Kivy application on MacOSX:$ kivy main.pyAndroidYour application needs some complementary files to be able to run on Android. See Kivy on Android for further reference.A window should open, showing a sole button (with the label Hello World) that covers the entire windows area. Thats all ther
5、e is to it.So what does that code do?1 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 f
6、or more information.2 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.3 Next, we import the Button class, to be able to create an instance of a button wit
7、h a custom label.4 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).5 Finally, we call run() on our application instance to laun
8、ch the Kivy process with our application inside.How to read the documentationThe documentation is seperated in 2 parts: the Programming Guide: its a must read to understand the Kivy basics, specially if you never done GUI programming before. the API: all the functions, classes, methods are explained
9、.Importing a classThe API part is autogenerated from the source code. If you take any class, it will be generated like this:It should be read like this: the “Button” class is into the “kivy.uix.button” module. So if you want to import that class in your code, write that:from kivy.uix.button import B
10、uttonControling the environmentMany environment variables are available to control the initialization and behavior of Kivy.For example, for restricting text rendering to PIL implementation:$ KIVY_TEXT=pil python main.pyEnvironment variable can be set before importing kivy:import osos.environKIVY_TEX
11、T = pilimport kivyConfigurationKIVY_USE_DEFAULTCONFIGIf this name is found in environ, Kivy will not read the user config file.KIVY_NO_CONFIGIf set, no configuration file will be read or write, and no user configuration directory too.KIVY_NO_FILELOGIf set, logs will be not print on a fileKIVY_NO_CON
12、SOLELOGIf set, logs will be not print on the consoleKIVY_DPIIf set, the value will be used instead of the value returned by the window.New in version 1.4.0.Path controlNew in version 1.0.7.You can control where is located default directory of modules, extensions, and kivy datas.KIVY_DATA_DIRLocation
13、 of the Kivy data, default to /dataKIVY_EXTS_DIRLocation of the Kivy extensions, default to /extensionsKIVY_MODULES_DIRLocation of the Kivy modules, default to /modulesRestrict core to specific implementationkivy.core try to select the best implementation available for your platform. For testing or
14、custom installation, you might want to restrict the selector to a specific implementation.KIVY_WINDOWImplementation to use for creating the WindowValues: pygameKIVY_TEXTImplementation to use for rendering textValues: pil, pygameKIVY_VIDEOImplementation to use for rendering videoValues: gstreamer, py
15、glet, ffmpegKIVY_AUDIOImplementation to use for playing audioValues: gstreamer, pygameKIVY_IMAGEImplementation to use for reading imageValues: pil, pygameKIVY_CAMERAImplementation to use for reading cameraValues: gstreamer, opencv, videocaptureKIVY_SPELLINGImplementation to use for spellingValues: enchant, osxappkitKIVY_CLIPBOARDImplementation to use for clipboard managementValues: pygame, dummyConfigure KivyThe configuration file of kivy is named config.ini, following the INI format file.Locati