📜  Python| kivy中的文本输入小部件

📅  最后修改于: 2022-05-13 01:55:18.267000             🧑  作者: Mango

Python| kivy中的文本输入小部件

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。

文本输入:

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

创建多行文本输入(“回车”键添加新行)。
要创建单行 TextInput,请将 TextInput.multiline 属性设置为 False。

TextInput(text='Hello world', multiline=False)
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 able 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()

输出:

经过一些输入 -