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
Related
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()
This is the output I am getting.I am trying to place a label at the very top and centre of the top area(centre top), but when I try to do it it just goes out of the screen.Please help by changing the only code I am writing.
Thanks.
What I tried.
.py file:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.uix.widget import Widget
from kivy.graphics import Line, Color
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
class kivyclass(Widget):
pass
class MainApp(App):
def build(self):
return kivyclass()
if __name__ == '__main__':
MainApp().run()
.kv file:
#:import utils kivy.utils
<kivyclass>:
canvas.before:
Color:
rgba: utils.get_color_from_hex('#d6fffc')
Rectangle:
size: self.size
pos: self.pos
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
Label:
id: labcovid
text: 'Covid-19 Uttarakhand'
font_size: root.width/15
font_name: 'vollkorn.ttf'
color: utils.get_color_from_hex('#4a4a4a')
this is the output I am getting
Your problem is two-fold
Your root widget is a simple widget that does not handle its children in any way.
The Label that has the text gets as big as the whole window.
The solution to the first is to make a FloatLayout as root widget
class kivyclass(FloatLayout):
pass
and to the second, to add this to your label on the kv part
size_hint: None, None
size: self.texture_size
I'm making an app to rename files that come from a scanner in a specific format.
The idea is to scan the directory and load a list of files, which are renamed according to the text in the textinput box. I've stripped down the app to come directly to the problem
import csv, os, datetime, string, shutil
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.properties import ObjectProperty, ListProperty, StringProperty, DictProperty
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from functools import partial
from kivy.uix.scrollview import ScrollView
class TestScreen(Screen):
def update_text(self, text):
print(text)
class ScreenManagement(ScreenManager):
pass
class TestApp(App):
default_font_size = 15
def build(self):
compiled_kv_file = Builder.load_file("TestApp.kv")
return compiled_kv_file
if __name__ == "__main__":
print("Main loop")
TestApp().run()
and the kv file is
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition:FadeTransition()
TestScreen:
<TestScreen>:
name: "TestScreen"
Button:
text:"Test Screen"
size_hint: 0.2,0.1
pos_hint: {"x":0.2,"y":0.8}
font_size: 20
TextInput:
id: my_textinputa
text:"File Screen"
size_hint: 0.2,0.1
pos_hint: {"x":0.4,"y":0.8}
font_size: 20
on_text:
root.update_text(self.text)
Label:
size_hint: 1,0.5
pos_hint: {"x":0,"y":0}
text: my_textinputa.text
font_size: 20
I'm using windows 10 and eclipse with pydev, if thats relevant. The textinput returns the entered text to the .py file. But the textinput does not update the entered text. Furthermore, There seem to be two labels rather than 1.
My gut says that I'm somehow adding two sets of widgets on top of each other. Since only the top one is changed, the text does not change on the bottom one. The output in the console is correct. Yet the textinput box doesnt update itself.
Can someone suggest how to get the textinput to update correctly?
Having multiple .kv files defining the root widget can produce this issue. One solution would be to drop the Builder.load_file("TestApp.kv")
class TestApp(App):
default_font_size = 15
def build(self):
pass
and rename the .kv file to the name of the App instance (test.kv in this case). In this way, kivy will only load one .kv file as the root widget.
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
kindly need to know how to change or control the size of the special buttons inside a kivy spinner. note: my spinner is in the kv language and not in python and looks like this:
Spinner:
id:some_id
text:"some text"
values:("1","2","3")
size_hint:(None,None)
size: root.width/4,root.height/12
Buttons inside of Spinner are of type passed to option_cls property. The default one is SpinnerOption class, which is actually a subclass of Button. You can change class passed to this property (it must have text property and on_release event) or modify SpinnerOption class globally:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.base import runTouchApp
Builder.load_string('''
<SpinnerOption>:
size_hint: None, None
size: 20, 20
<MyWidget>:
Spinner:
id:some_id
text:"some text"
values:("1","2","3")
size_hint:(None,None)
size: root.width/4,root.height/12
''')
class MyWidget(BoxLayout):pass
runTouchApp(MyWidget())
Using custom buttons:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
from kivy.base import runTouchApp
from kivy.properties import ObjectProperty
Builder.load_string('''
<MyButton>:
size_hint: None, None
size: 20, 20
<MyWidget>:
MySpinner:
id:some_id
text:"some text"
values:("1","2","3")
size_hint:(None,None)
size: root.width/4,root.height/12
''')
class MyButton(Button):
pass
class MySpinner(Spinner):
option_cls = ObjectProperty(MyButton) # setting this property inside kv doesn't seem to work
class MyWidget(BoxLayout):
pass
runTouchApp(MyWidget())