kivy unable to load image - python

Kivy is not loading an image for me, its showing '[image][error] error reading file x.png'
My python code (in short) is:
from kivy.core.window import Window
from kivy.properties import NumericProperty
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.theming import ThemeManager
from kivymd.uix.snackbar import Snackbar
import requests
class Home(FloatLayout):
pass
class MainApp(MDApp):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.theme_cls = ThemeManager()
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = ('LightBlue')
Window.size = (400,600)
Kivy file (again part that matters) is:
MDBottomNavigation:
MDBottomNavigationItem:
text:'Home'
name: 'Home'
Home:
Image:
source:'khatam.png'
Image:
source: 'togeather.png'
I have pip installed Kivy with all its dependencies(including GStreamer) and have OpenCV. Also, I am using Pycharm. The rest of the code is working just fine only this part is bugged.
I am relatively new. Thanks in advance.

Related

KIvyMD needs "Boxshadow" how do i use it/import it

in KivyMD there is a module or something called "Boxshadow", I need it to use almost every widget which uses levitation and i couldn't find anything resolving this issue. I am just a beginner with Kivy and KivyMD especially therefore I would be truly thankful for a solution. I as well cant follow simple tutorials because something does not work for me which works for them.
The simplest Application:
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.button import MDRectangleFlatButton
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
import kivy
from kivy.core.window import Window
Window.size = (200, 425)
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "DeepPurple"
return Builder.load_file('Mainapplication.kv')
MainApp().run()
Kivy:
MDScreen:
MDRaisedButton:
text: "Knopf"
pos_hint: { "center_x": 0.5, "center_y": 0.8}
ERROR Raised:
File "C:\KivyMD\virtual\lib\site-packages\kivy\factory.py", line 147, in __getattr__
raise FactoryException('Unknown class <%s>' % name)
kivy.factory.FactoryException: Unknown class <BoxShadow>

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.

when i use screenmanager no thing displayed om my screen kivymd python

When I use screenmanager nothing displayed om my screen kivymd python
I don't get any error so that means no bug in my code, but it doesn't display anything, and anyone of the both screens
this is my code :
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.button import MDFlatButton, MDRectangleFlatButton, MDIconButton, MDFloatingActionButton
from kivymd.uix.label import MDLabel, MDIcon
class firstscreen(Screen):
pass
class secondscreen(Screen):
pass
class windowmanager(ScreenManager):
pass
kv = Builder.load_file('sc.kv')
class Yom(MDApp):
def build(self):
return kv
if __name__ == '__main__':
Yom().run()
and this is the sc.kv file
windowmanager:
firstscreen:
secondscreen:
<firstscreen>:
name:'firstscreen'
MDRectangleFlatButton:
text:"calc moy"
pos_hint:{'center_x':0.5,'center_y':0.5}
on_release:MDApp.root.current='secondscreen'
<secondscreen>:
name:'secondscreen'
MDLabel:
text:'welcome to calcu screen'
halign:'center'
The issue was, that you did not capitalize the first letter of your classes. Kivy is very picky about this and refuses to load the screen correctly when you don't do this.
On a side note:
Classes should always be capitalized and use PascalCase as per PEP 8
Corrected Code:
main.kv:
WindowManager:
FirstScreen:
secondscreen:
<FirstScreen>:
name:'firstscreen'
MDRectangleFlatButton:
text:"calc moy"
pos_hint:{'center_x':0.5,'center_y':0.5}
on_release:MDApp.root.current='secondscreen'
<SecondScreen>:
name:'secondscreen'
MDLabel:
text:'welcome to calcu screen'
halign:'center'
main.py:
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.button import MDFlatButton, MDRectangleFlatButton, MDIconButton, MDFloatingActionButton
from kivymd.uix.label import MDLabel, MDIcon
class FirstScreen(Screen):
pass
class SecondScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
class Yom(MDApp):
def build(self):
kv = Builder.load_file('main.kv')
return kv
if __name__ == '__main__':
Yom().run()

is there a way to write Persian in python kivy

i try to write Persian in python kivy but it is not working.
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang import Builder
from kivy import Config
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class MainApp(App):
def build(self):
return Label(text= "فارسی")
if __name__ == "__main__":
MainApp().run()
You need to use some Persian font.
I have done it with Arabic text
You can download the font from here
Then use arabic_reshaper library to get it in shape.
pip install arabic-reshaper
You will also need python-bidi as well to flip the letters
pip install python-bidi
Refer to this https://github.com/mpcabd/python-arabic-reshaper
Code
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang import Builder
from kivy import Config
from kivy.uix.label import Label
from kivy.uix.widget import Widget
import arabic_reshaper
from bidi.algorithm import get_display
class MainApp(App):
def build(self):
reshaped_text = arabic_reshaper.reshape("فارسی")
bidi_text = get_display(reshaped_text)
return Label(text= bidi_text, font_name='Amiri-Regular.ttf', font_size=30)
if __name__ == "__main__":
MainApp().run()
Output

Is it possible to place all the Kivy code in a python user function, called from Main?

I'm using Kivy for a user interface to a control application. As such, the UI is subsidiary to the essential functionality. For modularity/tidyness, I've been trying to place all the Kivy code in a separate source file and to call it as the last step in Main.
However, if I do this, I find that some things don't work properly (for example, creating labels programatically and updating their text by scheduled events).
Is this something which should be possible?
Is there a 'trick' of which I should be aware?
If the detail of my question is not clear, I can put up some test code to illustrate.
Monolithic code, which works as expected:
cat test.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.properties import StringProperty
from kivy.clock import Clock
import random
Builder.load_string('''
<YourWidget>:
BoxLayout:
id: Box1
size: root.size
Button:
id: button1
text: "Change text"
on_release: root.change_text()
''')
class YourWidget(Screen):
random_number = StringProperty()
def __init__(self, **kwargs):
super(YourWidget, self).__init__(**kwargs)
label = Label(
id= 'label1',
text = self.random_number
)
self.ids.Box1.add_widget(label)
self.random_number = 'ini'
Clock.schedule_interval(self.change_text,1)
def change_text(self, *largs):
self.random_number = str(random.randint(1, 100))
class updatetestapp(App):
def build(self):
sm = ScreenManager(transition=NoTransition())
sm.add_widget(YourWidget(name='d_analogs'))
return YourWidget()
if (True):
updatetestapp().run()
If I then attempt to modularise my code, placing all the Kivy processing in a user function,everything works except the display of the ransom number variable. The variable persists through successive callbacks, as shown by calls to print, but it simply doesn't display. I'm guessing that this is something to do with the scope or context of the variable - Kivy isn't displaying the same variable which is being updated in the callback.
cat Main.py
#from mytest import mytest
import threading
import time
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.properties import StringProperty
from kivy.clock import Clock
from display import *
def main():
Display()
if __name__ == "__main__":
main()
cat display.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.properties import StringProperty
from kivy.clock import Clock
import random
Builder.load_string('''
<YourWidget>:
BoxLayout:
id: Box1
size: root.size
Button:
id: button1
text: "Change text"
on_release: root.change_text()
''')
class YourWidget(Screen):
random_number = StringProperty()
def __init__(self, **kwargs):
super(YourWidget, self).__init__(**kwargs)
label = Label(
id= 'label1',
text = self.random_number
)
self.ids.Box1.add_widget(label)
self.random_number = 'ini'
Clock.schedule_interval(self.change_text,1)
def change_text(self, *largs):
self.random_number = str(random.randint(1, 100))
class test(App):
def build(self):
sm = ScreenManager(transition=NoTransition())
sm.add_widget(YourWidget(name='d_analogs'))
return YourWidget()
def Display():
test().run()
You don't have any code that would change the text of your Label. Although you update self.random_number, you don't update the Label's text property, therefore you don't see any change.

Categories