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

Python在Kivy中添加图像小部件示例

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

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

图像小部件:

图像小部件用于显示图像。要使用图像小部件, 你必须导入:

from kivy.uix.image import Image, AsyncImage

因为模块kivy.uix.image具有与图像相关的所有功能。

可以通过两种类型将图像加载到应用程序:

1)从系统:wimg = Image(source =’mylogo.png’)2)异步加载:要异步加载图像(例如, 从外部Web服务器), 请使用AsyncImage子类:aimg = AsyncImage(source =’http: //mywebsite.com/logo.png’)

注意:默认情况下, 图像居中并适合窗口小部件边界框。如果你不想这样做, 可以设置allow_stretch到True和keep_ratio为假。

Basic Approach to create multiple layout in one file:
1) import kivy
2) import kivyApp
3) import image
4) set minimum version(optional)
5) create App class
6) return Image/layout/widget
7) Run an instance of the class

以下是代码, 如何使用代码中的图像:

代码1:

来自系统的简单映像(必须位于保存.py文件的文件夹中)

# Program to explain how to add image 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' )
  
# The Image widget is used to display an image
# this module contain all features of images
from kivy.uix.image import Image
  
# creating the App class
class MyApp(App):
  
     # defining build()
      
     def build( self ):
          
         # return image
         return Image(source = 'download.jpg' )
  
# run the App
MyApp().run()

输出如下:

Python在Kivy中添加图像小部件1

代码2:

我们如何从网络服务器(外部)添加AsyncImage

# Simple program to show how we add AsyncImage in kivy App
  
# 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' )
  
# The Image widget is used to display an image
# this module contains all features of images
from kivy.uix.image import AsyncImage
  
  
# creating the App class
class MyApp(App):
  
     # defining build()
      
     def build( self ):
          
         # return image
         return AsyncImage(source = 'http://kivy.org/logos/kivy-logo-black-64.png' )
  
# run the App
MyApp().run()

输出如下:

Python在Kivy中添加图像小部件2

现在想到了一件事情, 如何更改图像的大小, 位置等, 以下代码也将对此进行说明:

代码3:

# Program to Show how to use images 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' )
  
# The Image widget is used to display an image
# this module contain all features of images
from kivy.uix.image import Image
  
# The Widget class is the base class required for creating Widgets
from kivy.uix.widget import Widget
  
# to change the kivy default settings we use this module config
from kivy.config import Config
   
# 0 being off 1 being on as in true /false
# you can use 0 or 1 && True or False
Config. set ( 'graphics' , 'resizable' , True )
   
  
# creating the App class
class MyApp(App):
  
     # defining build()
      
     def build( self ):
          
         # loading image
         self .img = Image(source = 'download.jpg' )
  
         # By default, the image is centered and fits
         # inside the widget bounding box.
         # If you don’t want that, # you can set allow_stretch to
         # True and keep_ratio to False.
         self .img.allow_stretch = True
         self .img.keep_ratio = False
  
         # Providing Size to the image
         # it varies from 0 to 1
         self .img.size_hint_x = 1
         self .img.size_hint_y = 1
  
         # Position set
         self .img.pos = ( 200 , 100 )
  
         # Opacity adjust the fadeness of the image if
         # 0 then it is complete black
         # 1 then original
         # it varies from 0 to 1
         self .img.opacity = 1
          
  
         # adding image to widget
         s = Widget()
         s.add_widget( self .img)
  
         # return widget
         return s
  
# run the app
MyApp().run()

输出如下:

Python在Kivy中添加图像小部件3

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


赞(1)
未经允许不得转载:srcmini » Python在Kivy中添加图像小部件示例

评论 抢沙发

评论前必须登录!