I am new to Kivy. I want my Label to be on the left side, however it stays always in the centre, what I'm doing wrong?
.py
`
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.label import Label
class Display(BoxLayout):
pass
class Screen_One(Screen):
pass
class Screen_Two(Screen):
pass
class Screen_Three(Screen):
pass
class DemoApp(App):
icon = 'icon.png'
title = 'control panel'
def build(self):
return Display()
if __name__ == '__main__':
DemoApp().run()
Here is a litte snippet of the .kv file.
<Screen_Two>:
name: 'screen_two'
# BoxLayout:
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
Label:
text: 'test box position'
Thanks for any answer that is usefull.
The problem is that the default size for a child widget is to fill its parent, so your anchor_x and anchor_y have no effect. To fix it, just set the size of your Label to something reasonable:
<Screen_Two>:
name: 'screen_two'
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
Label:
text: 'test box position'
size_hint: (None, None)
size: self.texture_size
Related
I want to keep the Navigationbar on all my screen, I am able to switch screen and display what I want but I don't seem to keep the Navigationbar.
Also for some reason my side menu seems to appear twice, overlapping each other and I'm not sure why, I've tried refactoring the code and it just seems to appear again
Can anyone offer any help?
*.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
class FirstWindow(Screen):
pass
class ThirdWindow(Screen):
def on_pre_enter(self):
anchor = AnchorLayout(size=(1,1))
test = "This is the new window"
card = MDCard(orientation='vertical',padding="8dp",size_hint=(1,0.5),pos_hint={'top': 0.1,'right':1},radius=[5, ])
card.test = test
card.add_widget(MDLabel(text=test, halign="center"))
anchor.add_widget(card)
self.anchorID.add_widget(anchor)
class WindowManager(ScreenManager):
pass
class NearMeApp(MDApp):
def build(self):
self.theme_cls.theme_style ="Dark"
self.theme_cls.accent_palette = "Red"
self.theme_cls.primary_palette = "Purple"
self.root = Builder.load_file('NearMe.kv')
if __name__ == '__main__':
NearMeApp().run()
*.kv
#:import hex kivy.utils.get_color_from_hex
#:kivy 1.10.1
WindowManager:
FirstWindow:
ThirdWindow:
<FirstWindow>:
name:"FirstWindow"
Screen:
MDNavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "NearMe Application"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]
Widget:
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation:'vertical'
Button:
text:"ThirdWindow"
on_release: root.manager.current = "ThirdWindow"
<ThirdWindow>:
name:"ThirdWindow"
anchorID:anchorID
AnchorLayout:
id:anchorID
canvas.before:
Color:
rgba: .2, .2, .2, 1
Rectangle:
pos: self.pos
size: self.size
Your md toolbar needs to be above the main windowmanager.
With your mdtoolbar as a child of firstwindow (child of windowmanager), your windowmanager acts on it and the other children of firstwindow at same during change in current window. By moving the mdtoolbar out of firstwindow and above windowmanager the mdtoolbar will remain present.
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import Screen
class FirstWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = """
Screen:
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation:'vertical'
Button:
text:"ThirdWindow"
on_release: root.ids.manager.current = "ThirdWindow"
StackLayout:
MDToolbar:
title: "NearMe Application"
size_hint: (1, 0.1)
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]
WindowManager:
id: manager
FirstWindow:
ThirdWindow:
<FirstWindow>:
name:"FirstWindow"
Screen:
BoxLayout:
orientation: 'vertical'
Widget:
<ThirdWindow>:
name:"ThirdWindow"
anchorID:anchorID
AnchorLayout:
id:anchorID
canvas.before:
Color:
rgba: .2, .2, .2, 1
Rectangle:
pos: self.pos
size: self.size
"""
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(kv)
def build(self):
return self.screen
the_app = Test()
the_app.run()
The above removes the toolbar from your FirstWindow and places it on its own. In your example the toolbar is a child of FirstWindow so when you change the current window (screen) to ThirdWindow the toolbar disappears with its parent (FirstWindow). Now the toolbar is not a child of either window.
This is my kivy code:
Manager:
StartMenu:
SetupMenu:
<StartMenu>
name: "start"
size: root.width, root.height
GridLayout:
Image:
source: "Logo.png"
keep_ratio: True
size_hint: 1,1
AnchorLayout:
anchor_x: "center"
anchor_y: "bottom"
Button:
background_normal: ''
background_color: 242, 242, 242, .50
width: root.width/5
text: "Begin"
size_hint: None, None
on_release:
app.root.current: "setUp"
root.manager.transition.direction: "left"
</StartMenu>
<SetupMenu>
name: "setUp"
Button:
text: "Go Back"
on_release:
app.root.current: "startMenu"
root.manager.transition.direction: "left"
This is my Python code:
import kivy
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
=from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
class StartMenu(Screen):
pass
class SetupMenu(Screen):
pass
class Manager(ScreenManager):
pass
class StartMenu(Screen):
pass
kv = Builder.load_file("lordofthe90.kv")
class lordOfThe90(App):
def build(self):
return kv
lordOfThe90.run()
How do I make both screens cover the entire window? I force a full screen within my game so I'd require their size to be the same as the screen resolution of the device they're on.
Also, does my code roughly look good? I'm very new to kivy as a whole.
I had to define at least rows: 1 in GridLayout: to display it correctly.
I am new to Kivy and need some help in understanding function scope. I have built a simple app with two screens. The first screen has two buttons and the second screen has a text label at the centre. On the first screen, I have used app.root.current='new_screen_name' with the on_release attribute for one button and it works fine. It takes me to the next screen which is the intended function. For the second button, I have used a function call which has been defined in the Python file under the class definition of the first screen (the root widget of the button). However, this method does not work and the app window simply closes. I guess I am making a mistake in the function scope and call But I cannot figure out what. Any help would be greatly appreciated.
Python file:
from kivy.config import Config
# Config.set should be used before importing any other Kivy module.
Config.set('kivy','window_icon','sivaicon.png')
# Config set for resizing image button
Config.set('graphics', 'resizable', True)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang.builder import Builder
class SivaLoginScreen(Screen):
def func_authentication(self):
app.root.current='tabbed_screen'
class SivaTabbedScreen(Screen):
pass
class SivaScreenManager(ScreenManager):
pass
class ImageButton(ButtonBehavior, Image):
pass
# Tell Kivy to directly load a file. If this file defines a root widget, it will be returned by the method.
root_widget = Builder.load_file('siva.kv')
class SivaApp(App):
def build(self):
# Initialize root widget
return root_widget
if __name__ == '__main__':
# Run application
SivaApp().run()
Kivy file (.kv):
SivaScreenManager:
SivaLoginScreen:
SivaTabbedScreen:
<ImageButton>:
keep_ratio: True
<SivaLoginScreen>:
name: 'login_screen'
canvas.before:
Color:
rgba: 195/255, 60/255, 35/255, 1
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
size: root.width, root.height
Image:
id: login_logo_siva
source: 'images/sivalogo4.png'
keep_ratio: True
size_hint: 0.2, 0.2
pos_hint: {'center_x':0.5, 'center_y':0.75}
Label:
id: login_label_siva
pos: self.x*0.5-4, self.y*0.5+15
markup: True
font_name: 'roboto/Roboto-Medium.ttf'
text: '[color=#FDFD98]S.[/color][color=#B29DD9]I[/color][color=#FDFD98].[/color][color=#77DD77]V[/color][color=#FDFD98].[/color][color=#779ECB]A[/color]'
font_size: '50sp'
Label:
id: login_label_slogan1
pos: self.x*0.5-3, self.y*0.5-6
markup: True
font_name: 'roboto/Roboto-Regular.ttf'
text: '[color=#FDFD98]SLOGAN TEXT[/color]'
font_size: '15sp'
Label:
id: login_label_slogan2
pos: self.x*0.5-3, self.y*0.5-20
markup: True
font_name: 'roboto/Roboto-Regular.ttf'
text: '[color=#FDFD98]HEADLINE TEXT[/color]'
font_size: '15sp'
BoxLayout:
id:login_button_layout
orientation: 'horizontal'
size_hint: 0.2, 0.2
pos_hint: {'center_x':0.5, 'center_y':0.25}
ImageButton:
id: first_button
source: {'normal': 'images/first.png', 'down': 'images/first-down.png'} [self.state]
on_release: app.root.current='tabbed_screen'
ImageButton:
id: second_button
source: {'normal': 'images/second.png', 'down': 'images/second-down.png'} [self.state]
on_release: app.root.func_authentication()
<SivaTabbedScreen>:
name: 'tabbed_screen'
FloatLayout:
size: root.width, root.height
Label:
pos: self.x*0.5, self.y*0.5
text: 'SECOND SCREEN'
font_size: '50sp'
In your case, app.root link to SivaScreenManager which is the root widget of your application. And in these class, there is not a func_authenticationfunction, why you app crashed.
To refer a class itself in a KV definition, you must just use root, so right code must be :
on_release: root.func_authentication()
see Kivy Language - Reserved Keywords
Definition of func_authentication is not correct also, app is unknown. Use either :
App.get_running_app().root.current='tabbed_screen' or
self.manager.current='tabbed_screen'
i tried to show a plot in second screen after i clicked the button in the first screen.
this is the code that i tried.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.uix.widget import Widget
from pandas import read_excel
import matplotlib.pyplot as plt
plt.plot([1, 23, 2, 4])
plt.ylabel('some numbers')
class MainWindow(Screen):
def btn(self):
print("name: ",self.name2.text)
self.ids.destination.add_widget(FigureCanvasKivyAgg(plt.gcf()))
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("my.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__=="__main__":
MyMainApp().run()
#:kivy 1.9.1
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
name2: name2
GridLayout:
cols:1
size: root.width, root.height
GridLayout:
cols:2
Label:
text:"name: "
TextInput:
id: name
multinline:False
Label:
text:"name2: "
TextInput:
id: name2
multinline:False
Button:
text:"Submit"
on_release:
root.btn()
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
GridLayout:
cols:1
BoxLayout:
id: destination
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "right"
but i got error says 'super' object has no attribute '__getattr__' and i don't know what it means. please help me.
The problem is that you are trying to use an id that is defined in a different Screen. The destination is defined in SecondWindow, so MainWindow knows nothing about it. So, you can reference that id by going through SecondWindow like this:
class MainWindow(Screen):
def btn(self):
print("name: ",self.name2.text)
self.manager.get_screen('second').ids.destination.add_widget(FigureCanvasKivyAgg(plt.gcf()))
I basically have no clue on why the ScrollView isn't scrolling
Here is the python code:
from kivy.app import App
from kivy.config import Config
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import *
from kivy.uix.scrollview import ScrollView
class ScrollButton(Button):
pass
class DropperScrollView(ScrollView):
layout=ObjectProperty()
class MainWindow(FloatLayout):
mainbox=ObjectProperty()
dropper=ObjectProperty()
mainbutton=ObjectProperty()
dropper_button_1=ObjectProperty()
dropper_button_2=ObjectProperty()
dropper_button_3=ObjectProperty()
dropper_button_4=ObjectProperty()
scroll_list=ObjectProperty()
def open_dropper(self,dt):
self.dropper.open(self.mainbutton)
def btn_1(self,a):
if self.scroll_list.layout.children==[]:
btn_1=ScrollButton(text='Button 1')
btn_2=ScrollButton(text='Button 2')
self.scroll_list.layout.add_widget(btn_1)
self.scroll_list.layout.add_widget(btn_2)
class BioWikiaApp(App):
ratio=1/7
window_width=360
window_height=640
squared_ratio=NumericProperty(ratio)
squared_dropper_size_hint=ListProperty([ratio,ratio*9/16])
squared_dropper_size=ListProperty([window_width*ratio,window_height*ratio*9/16])
def build(self):
Window.size=(self.window_width,self.window_height)
Window.clearcolor=(155/255,220/255,160/255,1)
return MainWindow()
if __name__=='__main__':
app=BioWikiaApp()
app.run()
And the kivy file:
#:import Clock kivy.clock.Clock
#:import App kivy.app.App
#:import Window kivy.core.window.Window
#:import NoTransition kivy.uix.screenmanager.NoTransition
<DropperScrollView>:
layout:scroll_layout
size_hint_x:app.squared_ratio
pos_hint:{'x':app.ratio,'y':0}
GridLayout:
id:scroll_layout
cols:1
size_hint_y:None
<ScrollButton>:
size_hint_y:None
height:400
<MainWindow>:
id:mainwindow
mainbox:mainbox
dropper:dropper
dropper_button_1:dropper_button_1
dropper_button_2:dropper_button_2
dropper_button_3:dropper_button_3
dropper_button_4:dropper_button_4
mainbutton:mainbutton
scroll_list:scroll_list
BoxLayout:
id:mainbox
Label:
text:'This will hold the title'
Button:
id:mainbutton
text:'Home'
size_hint:app.squared_dropper_size_hint[0],app.squared_dropper_size_hint[1]
pos_hint:{'x':0,'y':1-app.squared_dropper_size_hint[1]}
on_parent:
dropper.dismiss()
Clock.schedule_once(root.open_dropper,-1)
on_release:dropper.open(self)
DropDown:
id:dropper
dismiss_on_select:False
on_select: mainbutton.text = '{}'.format(args[1])
Button:
id:dropper_button_1
text:'1'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
Button:
id:dropper_button_2
text:'2'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
Button:
id:dropper_button_3
text:'3'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
Button:
id:dropper_button_4
text:'4'
size_hint_y:None
height:mainbutton.height
on_release:root.btn_1(self)
DropperScrollView:
id:scroll_list
Although what really matters for me in the moment is making this damned ScrollView scroll, feel free to correct me on anything else I might have done wrong (like making Drop_Down List a child of the mainwindow cause I couldn't make it work otherwise)
Many thanks in advance
Solution
Please refer to the example for details.
DropDown is a special widget just like Popup. Don't try to add it as a child to any other widget. If you do, DropDown will be handled like an ordinary widget and dropdown list will be opened i.e. won't be closed in the background. In kv file, create a dynamic class <CustomDropDown#DropDown>: and add widgets to it.
Buttons in the ScrollView are bigger than the ScrollView because size_hint_y: None was not specified.
Set the height to minimum height such that there is something to scroll.
ScrollView » Managing the Content Size and Position
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
ScrollView » bar_width
bar_width
Width of the horizontal / vertical scroll bar. The width is
interpreted as a height for the horizontal bar.
bar_width is a NumericProperty and defaults to 2.
Example
main.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.properties import ListProperty, NumericProperty, ObjectProperty
class ScrollButton(Button):
pass
class DropperScrollView(ScrollView):
layout = ObjectProperty(None)
class MainWindow(FloatLayout):
mainbutton = ObjectProperty(None)
scroll_list = ObjectProperty(None)
def btn_1(self):
if not self.scroll_list.layout.children: # empty list
btn_1 = ScrollButton(text='Button 1')
btn_2 = ScrollButton(text='Button 2')
self.scroll_list.layout.add_widget(btn_1)
self.scroll_list.layout.add_widget(btn_2)
class BioWikiaApp(App):
ratio = 1/7
window_width = 360
window_height = 640
squared_ratio = NumericProperty(ratio)
squared_dropper_size_hint = ListProperty([ratio, ratio*9/16])
squared_dropper_size = ListProperty([window_width*ratio, window_height*ratio*9/16])
def build(self):
Window.size = (self.window_width, self.window_height)
Window.clearcolor = (155/255, 220/255, 160/255, 1)
return MainWindow()
if __name__ == '__main__':
BioWikiaApp().run()
biowikia.kv
#:kivy 1.11.0
#:import Factory kivy.factory.Factory
<DropDownButton#Button>:
size_hint_y: None
height: app.root.mainbutton.height
<CustomDropDown#DropDown>:
on_select: app.root.mainbutton.text = '{}'.format(args[1])
DropDownButton:
id: dropper_button_1
text: '1'
on_release:
root.select(self.text)
app.root.btn_1()
DropDownButton:
id: dropper_button_2
text: '2'
on_release:
root.select(self.text)
app.root.btn_1()
DropDownButton:
id: dropper_button_3
text: '3'
on_release:
root.select(self.text)
app.root.btn_1()
DropDownButton:
id: dropper_button_4
text: '4'
on_release:
root.select(self.text)
app.root.btn_1()
<DropperScrollView>:
layout: scroll_layout
size_hint: (app.squared_ratio, None)
pos_hint: {'x': app.ratio, 'y': 0}
bar_width: 10
bar_color: 0, 1, 0, 1 # green
bar_inactive_color: 1, 0, 0, 1 # red
effect_cls: "ScrollEffect"
scroll_type: ['bars']
GridLayout:
id: scroll_layout
cols: 1
size_hint_y: None
height: self.minimum_height
<ScrollButton>:
size_hint_y: None
height: 400
<MainWindow>:
mainbox: mainbox
mainbutton: mainbutton
scroll_list: scroll_list
BoxLayout:
id: mainbox
Label:
text:'This will hold the title'
Button:
id: mainbutton
text: 'Home'
size_hint: app.squared_dropper_size_hint[0], app.squared_dropper_size_hint[1]
pos_hint: {'x':0, 'y': 1 - app.squared_dropper_size_hint[1]}
on_release: Factory.CustomDropDown().open(self)
DropperScrollView:
id:scroll_list
Output