This code compile correctly.
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.textfield import MDTextFieldRect
from kivy.lang import Builder
class App(MDApp):
def build(self):
self.screen = Screen()
return self.screen
def on_start(self):
l = BoxLayout()
self.screen.add_widget(l)
w = MDTextFieldRect()
l.add_widget(w)
App().run()
But If you just change the widget from MDTextFieldRect to MDTextFieldRound or MDTextField, the application will crash and I don't know why (there is no apparent error message).
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.textfield import MDTextFieldRound
from kivy.lang import Builder
class App(MDApp):
def build(self):
self.screen = Screen()
return self.screen
def on_start(self):
l = BoxLayout()
self.screen.add_widget(l)
w = MDTextFieldRound()
l.add_widget(w)
App().run()
And to confuse me even more, it can work if you use the kv langage !!!
from kivymd.app import MDApp
from kivy.lang import Builder
KV = '''
Screen:
MDTextFieldRound:
icon_left: 'key-variant'
normal_color: app.theme_cls.accent_color
'''
class App(MDApp):
def build(self):
self.screen = Builder.load_string(KV)
return self.screen
App().run()
Related
I always see people creating big structures in .kv files. Sometimes they write the kv in a variable in the .py file.
Here I'm trying to create a widget with kv language in .py file but it doesn't work.
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivymd.uix.textfield import MDTextFieldRect
class App(MDApp):
def build(self):
print("building app")
self.screen = Screen()
return self.screen
def on_start(self):
print("starting app")
kv = '''
MDTextFieldRect:
'''
e = Builder.load_string(kv)
self.screen.add_widget(e)
if __name__ == "__main__":
app = App()
app.run()
And I know that I can do it another way but I want to learn how to do it like this because I have troubles creating some widget with python when it works in kv language.
Here is a working example of how I could do it but I need to learn another way.
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivymd.uix.textfield import MDTextFieldRect
class App(MDApp):
def build(self):
print("building app")
self.screen = Screen()
return self.screen
def on_start(self):
print("starting app")
self.screen.add_widget(MDTextFieldRect())
if __name__ == "__main__":
app = App()
app.run()
I'm working on a project with python and kivymd and i want to display an MDList in my file kv (list.kv)
but it doesn't work , i don't know where is the problem !! any suggestions
this is main.py
from kivy.lang import Builder
from mysql.connector import Error
from baseclass.start import Start
from kivy.lang import Builder
from threading import Thread
from kivy.uix.screenmanager import Screen
from kivy.utils import get_color_from_hex
from spin_load import ProgressSpinner
from kivymd.color_definitions import colors
from kivy.uix.recycleview import RecycleView
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
import mysql.connector
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import OptionProperty
from kivy.properties import ObjectProperty
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem
Window.size = (360, 600)
class Codebarre(Screen):
def on_start(self):
for i in range(20):
self.root.ids.container.add_widget(
OneLineListItem(text=f"Single-line item {i}")
)
class MyApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepPurple"
return Builder.load_file("main.kv")
MyApp().run()
and this is the file where i want to display the MDList
list.kv
#:import utils kivy.utils
<Codebarre>:
name: 'codebarre'
ScrollView:
MDList:
id: container ```
It turns out that the on_start() method was never called. A working version of main.py is below. I didn't change list.kv.
from kivy.lang import Builder
from threading import Thread
import kivy.clock
from kivy.uix.screenmanager import Screen
from kivy.utils import get_color_from_hex
from kivymd.color_definitions import colors
from kivy.uix.recycleview import RecycleView
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import OptionProperty
from kivy.properties import ObjectProperty
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.uix.stacklayout import StackLayout
Window.size = (360, 600)
class Codebarre(Screen):
def __init__(self, **kvargs):
self.app = MDApp.get_running_app()
super().__init__(**kvargs)
kivy.clock.Clock.schedule_once(self.build)
def build(self, *args):
for i in range(20):
self.ids.container.add_widget(
OneLineListItem(text=f"Single-line item {i}")
)
class MyApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepPurple"
Builder.load_file("list.kv")
return Codebarre()
MyApp().run()
I have a custom button. I cannot put it on screen via kv file. I studied a lot topics. No useful info can find. Here is a simple example:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string('''
#:kivy 1.11.0
<MyGrid>:
Button
text: 'hello'
''')
class MyGrid(BoxLayout):
pass
class DropApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
DropApp().run()
Note that the Button in this kv file is native Kivy button. I run this code, I can see this button on screen. But now I have a custom Button:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string('''
#:kivy 1.11.0
<MyGrid>:
customButton:
''')
class MyGrid(BoxLayout):
pass
class customButton(Button):
def __init__(self, **kwargs):
self.text = 'hi'
super(Button, self).__init__(**kwargs)
class DropApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
DropApp().run()
I run this code, I cannot see this customButton on screen. Note that the custom widget is complex. I have to define it in py file. For example:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class DropApp(App):
def build(self):
layout = GridLayout(cols=1, spacing=10)
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
return layout
if __name__ == '__main__':
DropApp().run()
Building this layout need for loop. I cannot find a way to build it in kv file. So I define it in Py file. But if I define it in py file, I cannot work with it in kv file.
Question 1: What's wrong with the second code?
Question 2: If I can't make it work, can I achieve the third code in kvlang(in kv file not in python)?
I am new to Kivy, hope someone can help.
When I run the code on Kivy v1.11.1 , Python v3.7.5:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class DropApp(App):
def build(self):
layout = GridLayout(cols=1, spacing=10)
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
return layout
if __name__ == '__main__':
DropApp().run()
The output is:
If this is the output you require, try running:
pip install --upgrade kivy
For the second code, python is case-sensitive. So instead of customButton, use CustomButton.
If you want to do the 3rd code using .kv file, here is an example:
from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.lang import Builder
Builder.load_string('''
<ExampleRV>:
viewclass: 'Button'
RecycleBoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
''')
class ExampleRV(RecycleView):
def __init__(self, **kwargs):
super(ExampleRV, self).__init__(**kwargs)
self.data = [{'text': str(x)} for x in range(20)]
class RecycleApp(App):
def build(self):
return ExampleRV()
RecycleApp().run()
For reference, go HERE.
i am currently creating an android app that uses the kivy framework and a package from kivy garden. i want to add another screen in it but i cant. thank you
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.properties import ListProperty,ObjectProperty,NumericProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.metrics import dp
from material.flatui.flatui import FloatingAction,RaisedButton,_MaterialButton
from garden import *
from sqlalchemy import *
from kivy.core.text import LabelBase
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class MyTab(GridLayout, AndroidTabsBase):
pass
class MyTabb(FloatLayout,AndroidTabsBase):
thoughts = ObjectProperty()
class MyTabbb(FloatLayout,AndroidTabsBase):
pass
class MyScreenManager(ScreenManager,Screen):
pass
class ShoppingScreen(Screen):
pass
class ExampleApp(App):
thoughts = ObjectProperty()
def build(self):
android_tabs = AndroidTabs()
return android_tabs
if __name__=='__main__':
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
LabelBase.register(name='Modern Pictograms',
fn_regular='modernpics.ttf')
LabelBase.register(name='Heydings',
fn_regular='heydings.ttf')
LabelBase.register(name='Roboto',
fn_regular='Roboto-Thin.ttf')
Window.clearcolor = get_color_from_hex('#008CD4')
ExampleApp().run()
and the code in the kv lang
<FBut#Button>:
font_size: 100
font_name: 'modernpics'
<AndroidTabs>:
tab_indicator_height: '2dp'
anim_threshold: 0
MyTab:
orientation: 'horizontal'
cols: 2
spacing: 5
text: 'BUDGET'
FBut:
text: 'i'
so i want to add another screen to enable me view other widgets
I wanted to trie out the anchor Layout unsing kivy language,but my Label was not affeckted by the AnchorLayout in any way
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
wurzel = Builder.load_string('''
AnchorLayout:
Label:
text: 'test'
anchor_x:'left'
anchor_y:'top'
''')
class TurF(App):
def build(self):
return wurzel
TurF().run()
The AnchorLayout is probably working fine, but your label fills it. Set something like the following instead.
AnchorLayout:
Label:
text: 'test'
anchor_x:'left'
anchor_y:'top'
size_hint: None, None
size: 50, 50