Setting kivy window size not working - python

I follwed this stackoverflow question, but neither alternatives worked.
This is my code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
class MyApp(App):
def build(self):
return Label(text='text')
if __name__ == '__main__':
Window.size = (1366, 768)
MyApp().run()
Sometimes the size works, Kivy creates a screen with size 800x600 then changes it to 1366x768. And sometimes Kivy creates a screen with size 800x600 then changes it to 1366x768, but then back to 800x600.
And if I change my code to:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.config import Config
Config.set('graphics', 'width', '200')
Config.set('graphics', 'height', '200')
class MyApp(App):
def build(self):
return Label(text='text')
if __name__ == '__main__':
MyApp().run()
With this code, nothing happens on my screen. I'm using Kivy v1.9.2-dev0. What I should do to fix it?

Put the Config settings before all the other imports - it's too late after importing Window, as the config has already been accessed to determine its initial size and your new settings are ignored.

Make your window non-resizable
from kivy.config import Config
Config.set('graphics','resizable',0)
set the window size
from kivy.core.window import Window
Window.size = (600, 500)
see example at https://kivyapps.wordpress.com/video-streaming-using-kivy-and-python/

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.

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

Image not showing when created from a thread kivy/kivymd

I'm working on an app, and I need the images to display independently at a specific timing. I have set up a thread using python's stock threading module, it runs and works normally instead of the image it displays a black square. Does anyone know how to fix it?
Here is my code to reproduce the issue:
import threading
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class TestApp(App):
def build(self):
self.fl = FloatLayout()
self.fl.add_widget(Button(text="show image", on_press=self.start_thread))
return self.fl
def insertfunc(self):
self.fl.add_widget(Image(source="HeartIcon.png"))
def start_thread(self, instance):
threading.Thread(target=self.insertfunc).start()
TestApp().run()
Any help will be appreciated!
The add_widget() must be done on the main thread. I assume that you are using threading because you have additional things to do on the Thread aside from just the add_widget(). Based on that assumption, here is a modified version of your code that does what I think you want:
import threading
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class TestApp(App):
def build(self):
self.fl = FloatLayout()
self.fl.add_widget(Button(text="show image", on_press=self.start_thread))
return self.fl
def insert_image(self, dt):
self.fl.add_widget(Image(source="HeartIcon.png"))
def insertfunc(self):
# do some calculations here
Clock.schedule_once(self.insert_image)
def start_thread(self, instance):
threading.Thread(target=self.insertfunc).start()
TestApp().run()
If you are not doing anything else in the new thread, then you don't actually need another thread. The start_thread() method can just do the:
self.fl.add_widget(Image(source="HeartIcon.png"))

Kivy App size fixed in full screen windowed

I've made a windows app with Kivy and I want to set the size of the window to a fixed size which is full-screen windowed and I can't figure it out.
i've tried this method:
from kivy.core.window import Window
Window.size = (1920, 1080)
Window.fullscreen = True
It makes the app full screen but not full screen windowed. Does anyone have a solution for this please?
Config.set determines the size of the window and should be indicated at the very beginning of the script. For more information check https://kivy.org/doc/stable/api-kivy.config.html
from kivy.config import Config
Config.set('graphics', 'width', '1920')
Config.set('graphics', 'height', '1080')

Kivy image not showing

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖
Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

Categories