kivy中带有验证按钮的文本输入框
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
在本文中,我们将学习如何在 kivy 中添加带有文本输入的按钮,就像我们在输入和提交按钮中一样。在继续之前,您必须了解 kivy 中的 Textinput 小部件和 Button。
TextInput: The TextInput widget provides a box for editable plain text. Unicode, multiline, cursor navigation, selection and clipboard features are supported.
Button: The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). We can add functions behind the button and style the button.
要使用 Textinput 和按钮,您必须通过命令导入它 -
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
Basic Approach:
1) import kivy
2) import kivyApp
3) import Button
4) import Boxlayout
5) import Textinput
6) import BoxLayout
7) Set minimum version(optional)
8) create App class
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class
Kivy Tutorial – Learn Kivy with Examples.
方法的实施——
main.py 文件
## Sample Python application demonstrating that
## how to create Text Input with Button 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.1')
# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button
# 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
# 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)
# Create the App class
class BTNTEXTApp(App):
# defining build()
def build(self):
# Telling orientation
b = BoxLayout(orientation ='vertical', )
# Adding the text input
t = TextInput(font_size = 30,
size_hint_y = None,
height = 100)
# Adding Button and styling
f = Button(text ="Push Me !",
font_size ="20sp",
background_color =(.67, 1, .33, 1),
color =(1, 1, 1, 1) )
b.add_widget(t)
b.add_widget(f)
return b
# Run the App
if __name__ == "__main__":
BTNTEXTApp().run()
输出:
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。