The print
function is used to display information by the standard output (stdout), that is, the terminal / console. Kivy creates a graphical interface, just like if you use Tkinter you need an appropriate widget to display the text (label, textview, textedit, etc.).
The two main widgets for displaying / entering text in Kivy are the labels (Label) and the Text input, you can look at the documentation for more information:
Kivy-Label
Kivy-Text input
An example that implements labels and text inputs in Kivy would be:
main.py:
# config
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'system')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.clock import Clock
from itertools import cycle
import random
Builder.load_file('design.kv')
class MyWidget(BoxLayout):
random_number = StringProperty()
string = StringProperty()
def __init__(self):
super(MyWidget, self).__init__()
self.random_number = str(random.randint(1, 100))+'\n'
self.listText = cycle(('Hola', 'soy', 'una', 'etiqueta'))
Clock.schedule_interval(self.change_label_text, 0.6)
def numero_aleatorio(self):
self.random_number += str(random.randint(1, 100))+'\n'
def change_label_text(self, *args):
self.string = next(self.listText)
class myApp(App):
def build(self):
return MyWidget()
def on_pause(self):
return True
def on_resume(self):
pass
if __name__ in ('__main__', '__android__'):
myApp().run()
design.kv:
<MyWidget>:
BoxLayout:
size: root.size
Button:
id: button1
text: "Nuevo número"
on_release: root.numero_aleatorio()
TextInput:
id: textInp2
text: root.random_number
multiline: True
readonly: True
Label:
id: label5
text: root.string
TextInput:
id: textInp1
hint_text: 'Escribe algo:'
multiline: True
readonly: False
TextInput:
id: textInp2
text: textInp1.text
multiline: True
readonly: True
background_color: 0.92,0.89,0.75,1
on_focus: self.focus = False
The app would look something like this:
It is a BoxLayout that contains 5 witgets, the first one is a button that when pressed adds a new random number that is shown in the Text input
of only read that it has next. The third is a label that shows a text that is automatically changing using a timer, the fourth is another Text input
that if you can enter text, the text entered is automatically copied in the last widget (another Text input
read only). It's just a sample, you can create Text input
of a single line, specific to password entry, change colors, effects, fonts, keyboard shortcuts, etc.
The design.kv
file is a text file and contains the structure of the app defined using Kivy Languaje . To run it on android using the Launcher, just copy it to the same folder as main.py
and android.txt
. Although it seems complicated is really simple and very powerful, for example we can make a widget (like a button) occupy half of the height of his father at all times (restores automatically) with simply:
<MyWidget>:
BoxLayout:
size: root.size
Button:
id: button1
text: "Botón"
size_hint: None, None
height: self.parent.height/2
width: self.parent.widht
If you are just starting and you have mastered English, you might as well see some tutorials that you can find on YouTube or on some websites about creating apps on Kivy, search them on Google if you're interested.
Because of another question I think you are trying to port a Tkinter app to Kivy, keep in mind that Kivy is not an 'emulator' of Python on Android, it is a framework for creating apps with user interfaces such as Tkinter (in a much more basic way). To carry your app you will have to find the way to do what you do with Tkinter and its widget in Kivy and yours. Tkinter will never work on Android, the idea is to replace what Tkinter does with Kivy. If your app is not very complicated graphically it does not have to be very complicated, the logical part of your Python code will not have to modify it in principle.