Kivy Does Not Output Plain Text, Only Object Location - python

So I was trying to create a very basic text editor in Kivy. So I had got around the issue of text not showing up (via lambda). However, a new error appeared on the horizon; saving. I want to save automatically to a plain txt file. However, my current code only save an object (is it a pointer?) of the actual text input. Thank you all, SO!
import kivy
import os
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.lang import Builder
class ColdKivyApp(App): # I actually used to call it Zone but changed it to Cold cause it's cold outside ;)
def build(self):
f = BoxLayout(orientation='vertical')
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
txtstr = str(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
with open('testtxt.txt', 'w') as txtwriter:
txtwriter.write("" + txtstr)
txtwriter.close()
return f
if __name__ == '__main__':
ColdKivyApp().run()
Edit: Spelling

TextInput has a text property, that's where the current content is.

Replace txtstr = str(txt) in your code to txtstr = txt.text.

Related

KivyMD without using kv file

I'm trying to make an expense tracker app using KivyMD. I have built it already using kivy but it's design is awful, then i found out KivyMD and now i want to tweak the app using KivyMD but i want to do it without using a kv file because my app has a lot of nested if statements which are too complex to write in the kv file. Anyway, i'm trying to test KivyMD but running into this nasty ValueError
ValueError: KivyMD: App object must be initialized before loading root widget. See https://github.com/kivymd/KivyMD/wiki/Modules-Material-App#exceptions and idk how to fix it without using a kv file. This question is asked plenty times but every answer uses a kv file. Can someone please help me understand this error and tackle it without kv. Thank you... Here is some code
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.app import App
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.stacklayout import MDStackLayout
from kivymd.uix.button import MDRaisedButton, MDRectangleFlatButton
from kivy.metrics import dp,sp
from kivymd.uix.screen import MDScreen
from kivy.uix.textinput import TextInput
from kivymd.uix.textfield import MDTextField
from kivy.uix.screenmanager import ScreenManager
import re
#ALL SCREENS
class MainScreen(MDScreen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
box = MDBoxLayout(orientation="vertical")
b = MDRaisedButton(text="Content",size_hint = (1,0.5))
box.add_widget(b)
t = MDTextField(size_hint=(1,0.5))
box.add_widget(t)
self.add_widget(box)
#ScreenManager
sm = ScreenManager()
sm.add_widget(MainScreen(name="main_screen"))
class MyApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepOrange"
self.theme_cls.accent_palette = "Lime"
return MainScreen()
if __name__ == "__main__":
MyApp().run()
works perfectly fine when i remove the screenmanager and just return the MainScreen.
Any help or guidance is highly appreciated.

Kivy not loading video

I'm using the following code using kivy, trying to load a video. I created a new file that didn't have all the extra things I was doing, as to eliminate all the possible complications. I'm new to stack overflow, so if I've done something wrong don't judge...The video is in the same file, and I've tried everything I found online about these kind of errors, I've also installed pillow and ffpyplayer. The error I receive is [ERROR ] [Image ] Error loading <ironman.mp4>
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.video import Video
from kivy.uix.videoplayer import VideoPlayer
class MyApp(App):
def build(self):
video = Video(source='ironman.mp4')
video.state = "play"
video.options = {'eos': 'loop'}
video.allow_stretch = True
video.loaded = True
return video
if __name__ == "__main__":
MyApp().run()
I had the same problem but with Image. Install pillow 8.4.0.

How do I get the kivy Window.softinput_mode = 'below_target' to move the TextInput box above the virtual keyboard?

I'm using python 3.8.6 and kivy 2.1.0.
When the text input box gets focus, the keyboard pops up and covers the TextInput box. I've added the 2 lines below to force the keyboard to be below the target TextInput box, but it does not work as expected.
Window.keyboard_anim_args = {"d":.2,"t":"linear"}
Window.softinput_mode = "below_target"
I've also tried 'pan' and 'resize'. The code runs with no errors, but none of these settings has any effect on the behavior, so I'm sure I'm missing something obvious, but it's not obvious to me:-(. Any help is greatly appreciated. The full code follows:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
Window.keyboard_anim_args = {"d":.2,"t":"linear"}
Window.softinput_mode = "below_target"
#Window.softinput_mode = 'pan'
#Window.softinput_mode = 'resize'
class ClearApp(App):
def build(self):
self.box = BoxLayout(orientation='horizontal', spacing=10)
self.txt = TextInput(hint_text='Write here',
keyboard_mode='auto',
size_hint=(.5,.1))
self.btn = Button(text='Clear All',
on_press=self.clearText, size_hint=(.1,.1))
self.box.add_widget(self.txt)
self.box.add_widget(self.btn)
return self.box
def clearText(self, instance):
self.txt.text = ''
'''
if Config:
_is_desktop = Config.getboolean('kivy', 'desktop')
_keyboard_mode = Config.get('kivy', 'keyboard_mode')
if _is_desktop:
Config.set('kivy', 'keyboard_mode','system')
Config.write()
else:
Config.set('kivy', 'keyboard_mode','systemanddock')
Config.write()
'''
Config.set('kivy', 'keyboard_mode','dock')
Config.write()
ClearApp().run()
Use import Config before import App
which help you to focus and show your keyboard in the UI
for eg.
from kivy.config import Config
from kivy.app import App
use these these line in your code
This function is available only in the master kivy version. so in your buildozer.spec file in the requirements put kivy==master when you are adding kivy to the requirements

First character not getting printed in kivy

I am learning to code in Kivy using python
But my Label is not showing the first character
Can anyone help me
The kv File is as follows:
<MyGrid>
Label:
text: "Techy Matanhelia"
The python file is as Follows:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class MyGrid(Widget):
pass
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
MyApp().run()
Also Can anyone tell me how to edit kivy files on pycharm2020.2.2
That is because your MyGrid class extends Widget. A simple Widget has no capability to handle positioning or sizing of its children, so the Label gets assigned the default size of (100,100) and the default position of (0,0). which results in the first letter of the Label being off the screen. Try making MyGrid extend a Layout, perhaps like this:
class MyGrid(FloatLayout):
pass

Kivy TextInput background problems

I don't like the default gradient background for the unfocused TextInput so I edited the default texture in Paint. But it doesn't seem to recognize it. It's in a separate file and looks like this:
The texture I get is just plain white. Do I need a specific file with the texture or to move the texture to the place where it is in the default texture?
This is a test code:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class App1(App):
def build(self):
b = BoxLayout()
tx1 = TextInput()
tx = TextInput(background_normal = "E:\textinput_unfocused.png")
b.add_widget(tx1)
b.add_widget(tx)
return b
App1().run()
Oh, by the way. The source code is located in the E: disk, as well as the textinput_unfocused.png.
the path for Your background should be specified relative to the app file. So if they are both in the same directory just put:
tx = TextInput(background_normal = "textinput_unfocused.png")

Categories