个性化阅读
专注于IT技术分析

Python Kivy Scatter用法示例介绍

Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。

??Kivy教程–通过示例学习Kivy。

Scatter

Scatter用于构建交互式小部件, 这些小部件可以在多点触摸系统上用两个或更多个手指进行翻译, 旋转和缩放。

它包含自己的矩阵转换。 Scatter类允许用户在其控制下拖动, 缩放和旋转任何窗口小部件。

就像在Relativelayout中一样, 子项相对于散点定位。

因此, 拖动散点图时, 孩子的位置不会改变, 只有散点图的位置会改变。

Scatter大小对其子项的大小没有影响。如果要调整其大小, 则可以使用缩放, 缩放既可以更改散点图及其子级, 又可以更改大小。

Basic Approach:
1) import kivy
2) import kivyApp
3) import scatter
4) import Relativelayout
5) import widget
6) Set minimum version(optional)
7) create Widget class
8) create layout class
9) create App class
10) create the, kv file
11) return Layout/widget/Class(according to requirement)
12) Run an instance of the class

要使用散点图, 首先必须通过命令将其导入:from kivy.uix.scatter import散点图

#方法的实现:

.py文件

# Program to explain how to use Scatter in kivy  
       
# import kivy module     
import kivy   
         
# base Class of your App inherits from the App class.     
# app:always refers to the instance of your application    
from kivy.app import App  
       
# this restrict the kivy version i.e   
# below this kivy version you cannot   
# use the app or software   
kivy.require( '1.9.0' )  
  
# Scatter is used to build interactive
# widgets that can be translated, # rotated and scaled with two or
# more fingers on a multitouch system.
from kivy.uix.scatter import Scatter
  
# Widgets are elements of a
# graphical user interface that
# form part of the User Experience.
from kivy.uix.widget import Widget
  
# This layout allows you to set relative coordinates for children. 
from kivy.uix.relativelayout import RelativeLayout
  
  
# Creating widget class
class SquareWidget(Widget):
     pass
  
# Creating Scatter Class
class ScatterWidget(Scatter):
     pass
  
# Create the layout class
class Scatter_App(RelativeLayout):
     pass
            
class ScatterApp(App):
     def build( self ):
         return Scatter_App()
  
if __name__ = = '__main__' :
     ScatterApp().run()

.kv文件

# .kv file implementation
  
# Create the square to show scatter
<SquareWidget>:
     size: 100 , 100
     canvas:
         Color:
             rgb: [ 0.345 , 0.85 , 0.456 ]
         Rectangle:
             size: self .size
             pos: self .pos
  
  
# Create the scatter properties           
<Scatter_App>:
      
     canvas:
         Color:
             rgb: . 8 , . 5 , . 4
         Rectangle:
             size: self .size
             pos: self .pos
  
     ScatterWidget:
         id : square_widget_id
         SquareWidget:
  
     # Showing the current position of the box
     Label:
         text: 'Position: ' + str (square_widget_id.pos)
         size_hint: . 1 , . 1
         pos: 500 , 300

输出如下:

Python Kivy Scatter用法示例介绍

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » Python Kivy Scatter用法示例介绍

评论 抢沙发

评论前必须登录!