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

Python使用.kv文件的Kivy中的TextInput

点击下载

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

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

TextInput:

TextInput小部件提供了一个可编辑纯文本框。支持Unicode, 多行, 光标导航, 选择和剪贴板功能。

TextInput使用两种不同的坐标系:

  • (x, y)–像素坐标, 主要用于在屏幕上渲染。
  • (行, 列)–以字符/行为单位的光标索引, 用于选择和光标移动。
Basic Approach:

1) import kivy
2) import kivyApp
3) import widger
4) import Relativelayout
5) import textinput
6) Set minimum version(optional)
7) Create Widget class
8) Create App class
9) create .kv file (name same as the app class):
        1) create textinput
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class

实施方法

#main.py文件

# Program to Show how to use textinput 
# (UX widget) in kivy using .kv file
  
# 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' )
  
# Widgets are elements
# of a graphical user interface
# that form part of the User Experience.
from kivy.uix.widget import Widget
  
# The TextInput widget provides a
# box for editable plain text
from kivy.uix.textinput import TextInput
  
# This layout allows you to set relative coordinates for children. 
from kivy.uix.relativelayout import RelativeLayout
  
# Create the widget class
class textinp(Widget):
     pass
  
# Create the app class
class MainApp(App):
  
     # Building text input
     def build( self ):
         return textinp()
  
     # Arranging that what you write will be shown to you
     # in IDLE
     def process( self ):
         text = self .root.ids. input .text
         print (text)
  
# Run the App
if __name__ = = "__main__" :
     MainApp().run()

#main.kv文件

# .kv file implementation of the code
  
<textinp>:
     title: 'InputDialog'
     auto_dismiss: False
     id : test1
  
     # Using relative layout to arrange properly
     RelativeLayout:
         orientation: 'vertical'
         pos: self .pos
         size: root.size
         id : test2
  
         # Defining text input in .kv
         # And giving it the look . pos and features
         TextInput:
             id : input
             hint_text: 'Enter text'
             pos_hint: { 'center_x' : 0.5 , 'center_y' : 0.705 }
             size_hint: 0.95 , 0.5
             on_text: app.process()

输出如下:

运行应用程序时, 你将看到:

Python使用.kv文件的Kivy中的TextInput1

输入一些信息后, 你将看到:

Python使用.kv文件的Kivy中的TextInput2

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


赞(0)
未经允许不得转载:srcmini » Python使用.kv文件的Kivy中的TextInput

评论 抢沙发

评论前必须登录!