Python如何使用Kivy中的Textinput小部件?

2021年3月19日17:08:58 发表评论 1,143 次浏览

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

👉🏽Kivy教程–通过示例学习Kivy。

TextInput:

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

要创建多行TextInput(" enter"键添加新行)。

若要创建单行TextInput, 请将TextInput.multiline属性设置为False。

TextInput(text='Hello world', multiline=False)

要使用Textinput, 必须通过以下命令将其导入-从kivy.uix.textinput导入TextInput

Basic Approach:
1) import kivy
2) import kivyApp
3) import Label
4) import Scatter
5) import Floatlayout
6) import Textinput
7) import BoxLayout
8) Set minimum version(optional)
9) create App class
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class

现在执行该方法:

# Program to Show how to use textinput (UX widget) 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 Label widget is for rendering text.  
from kivy.uix.label import Label 
    
# module consist the floatlayout  
# to work with FloatLayout first  
# you have to import it  
from kivy.uix.floatlayout import FloatLayout 
  
# 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
  
# The TextInput widget provides a
# box for editable plain text
from kivy.uix.textinput import TextInput
  
# BoxLayout arranges widgets in either
# in vertical fashion that
# is one on top of another or in
# horizontal fashion that is one after another.
from kivy.uix.boxlayout import BoxLayout
  
# Create the App class
class TutorialApp(App):
      
     def build( self ):
  
         b = BoxLayout(orientation = 'vertical' )
  
         # Adding the text input
         t = TextInput(font_size = 50 , size_hint_y = None , height = 100 )
          
         f = FloatLayout()
  
         # By this you are abel to move the
         # Text on the screen to anywhere you want
         s = Scatter()
  
         l = Label(text = "Hello !" , font_size = 50 )
  
         f.add_widget(s)
         s.add_widget(l)
  
         b.add_widget(t)
         b.add_widget(f)
  
         # Binding it with the label
         t.bind(text = l.setter( 'text' ))
  
          
         return b
  
# Run the App
if __name__ = = "__main__" :
     TutorialApp().run()

输出如下:

Python | Kivy中的Textinput小部件1

输入一些信息后–

Python | Kivy中的Textinput小部件2

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


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: