when i use screenmanager no thing displayed om my screen kivymd python - 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()

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>

Python/Kivy Multiple screens not working, Code does not recognise the screen manager?

I am trying to make an app that starts in the main menu, you press the play button and it sends you to the game on a different screen.
Problem is i keep getting an error on the:
"kv= Builder.load_file("my.kv")" saying that "WindowManager" is an unknown class.
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.vector import Vector
from kivy.uix.floatlayout import FloatLayout
kv= Builder.load_file("my.kv")
class WindowManager(ScreenManager):
pass
class MenuWindow(Screen):
pass
class Game(Screen):
pass
class MyApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyApp().run()
AND HERE IS THE KV FILE:
WindowManager:
MenuWindow:
Game:
<MenuWindow>:
name: "Menu"
FloatLayout:
Button:
text:"Play"
on_release:
app.root.current= "Game"
<Game>:
name: "Game"

How to build a MDDropdownMenu and its items on pressing a Button?

As my app has many screens and is almost done building the frontend i dont want to change the screen manager(<root widget>:) and class MainApp(MDApp):. Please help me on how i can add a MDDropdownMenu on pressing package weight button on my home screen without changing the layout of my code. Not only the below mentioned code but i have tried different ways to implement it but faced lots of errors. Please help and Thanks in advance.
MY main.py file
from kivymd.app import MDApp
import json
from datetime import datetime
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivymd.uix.button import MDFlatButton ,MDRectangleFlatIconButton
from kivymd.uix.label import MDLabel, MDIcon
from kivymd.uix.textfield import MDTextField
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineListItem
from kivy.core.window import Window
from kivymd.uix.menu import MDDropdownMenu, MDMenuItem
class HomeScreen(Screen):
def drop(self):
self.dropdown= MDDropdownMenu(items= {"viewclass": "MDMenuItem","text":"option1"}, width_mult=4)
self.dropdown.open()
class RootWidget(ScreenManager):
pass
class MainApp(MDApp):
def build(self):
self.theme_cls.primary_palette= "Green"
return RootWidget()
if __name__ == "__main__":
MainApp().run()
My design.kv file
<HomeScreen>:
GridLayout:
cols: 1
padding: 20, 20
spacing: 10,10
size_hint: 1,0.87
MDRectangleFlatIconButton:
icon: 'weight-kilogram'
text: "Package weight"
size_hint: (0.55,1)
on_press: root.drop()
<RootWidget>:
HomeScreen:
name: "home_screen"
This is the image of my app home screen
You need to include the caller in your MDDropdownMenu() call, and the items is expected to be a list of dictionaries.
Try changing your kv to include an id for the MDRectangleFlatIconButton:
<HomeScreen>:
GridLayout:
cols: 1
padding: 20, 20
spacing: 10,10
size_hint: 1,0.87
MDRectangleFlatIconButton:
id: caller # ADDED
icon: 'weight-kilogram'
text: "Package weight"
size_hint: (0.55,1)
on_press: root.drop()
The id is used to identify the caller.
Then create the DropDown in a on_kv_post() method, and the drop() method just opens the DropDown:
class HomeScreen(Screen):
def on_kv_post(self, base_widget):
caller = self.ids.caller
self.dropdown = MDDropdownMenu(caller=caller, items=[{"viewclass": "MDMenuItem", "text": "option1"}], width_mult=4)
def drop(self):
self.dropdown.open()
The on_kv_post() method is executed after the kv rules have been executed, so that the ids are available.
The MDDropdownMenu is not created in the drop() method, as recommended in the documentation.

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.

Kivy - Errant Vertical Splitter

I am attempting to construct a screen with a VERTICAL splitter to separate content; however, I am unsuccessful in identifying a solution even after consulting the kivy docs and looking through the related questions here.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.splitter import Splitter
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import StringProperty, DictProperty
from kivy.uix.screenmanager import ScreenManager, Screen
kv = '''
ScreenManagement:
id: 'manager'
MainScreen:
name: 'main'
manager: 'manager'
<MainScreen>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'New'
Splitter:
sizeable_from: 'top'
Button:
text: 'test'
'''
class ScreenManagement(ScreenManager):
pass
class MainScreen(Screen):
pass
class MyApp(App):
def build(self):
return Builder.load_string(kv)
MyApp().run()
Here is what I am current seeing with this code
As you can see, the splitter is beside the second button rather than between the buttons horizontally; and when the splitter is activated, it shrinks the button horizontally rather than vertically. How do I change the code for the effect that I desire?
Simple misspelling. sizeable_from should be sizable_from.

Categories