Kivy Cover Whole Screen - python

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.

Related

BoxLayout 'cols' doens't work in AnchorLayout

Here is my kivyfile.kv
WindowManager:
FirstWindow:
SecondWindow:
<FirstWindow>:
name: "first"
AnchorLayout:
anchor_x: "left"
anchor_y: "top"
BoxLayout:
cols:2
orientation: 'vertical'
size_hint: None, None
size: self.minimum_size
Button:
text: "Button 1"
size_hint: (None,None)
width:200
height:30
Button:
text: "Button2"
size_hint: (None,None)
width:200
height:30
TextInput:
id: url
multiline: False
size_hint: (None,None)
width:200
height:30
TextInput:
id: url2
multiline: False
size_hint: (None,None)
width:200
height:30
Here is my .py file:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class FirstWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv=Builder.load_file('kivyfile.kv')
class Downloader(App):
def build(self):
return kv
if __name__ == '__main__':
Downloader().run()
What I am trying to achive, is to get a BoxLayout at the top left corner, which has two columns - Button and TextInput NEXT TO it. However, it only does all components one under each other. I have also a future question on how to add another BoxLayout NEXT TO the previous BoxLayout that I have. As in - how to make it be on the right side of existing BoxLayout, how to position it.

Kivy function scope on button release (widget tree)

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'

Save checkbox data and textinput data to .Json file

Im trying to save data from one page of my app into a .json file. There are 2 checkboxes and one text input area. I only wish to save one or the other as not all of them need to be saved. Just the selected one.
Ive tried reading the kivy docs, and ive tried various examples from here and on the web, and i still cannot save my data as intended into a json file.
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.storage.jsonstore import JsonStore
from kivy.clock import Clock
class MyScreenManager(ScreenManager):
pass
class FirstScreen(Screen):
pass
class Who(Screen):
def __init__(self, **kwargs):
super(Who, self).__init__(**kwargs)
self.store = JsonStore("who.json")
Clock.schedule_once(lambda *args: self.load())
def save(self):
self.store.put('who',
mum = self.mum.checkbox,
dad = self.dad.checkbox,
other = self.other.text)
def load(self):
if self.store.exists('who'):
who = self.store.get('who')
w = [("mum", self.mum), ("dad", self.dad), ("other", self.other)]
for key, ti in v:
val = who.get(key)
if val:
ti.text = val
root_widget = Builder.load_file("test.kv")
runTouchApp(root_widget)
and my kv doc looks like this:
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import StringProperty kivy.properties.StringProperty
#:import ObjectProperty kivy.properties.ObjectProperty
#:import Label kivy.uix.label.Label
#:import Window kivy.core.window.Window
#:import ListProperty kivy.properties.ListProperty
#:import CheckBox kivy.uix.checkbox.CheckBox
#:import TextInput kivy.uix.textinput.TextInput
#:import Button kivy.uix.button.Button
#:import BoxLayout kivy.uix.boxlayout.BoxLayout
#:import GridLayout kivy.uix.gridlayout.GridLayout
#:import CheckBox kivy.uix.checkbox.CheckBox
MyScreenManager:
transition: FadeTransition()
Who:
FirstScreen:
<Who>:
name: 'who'
mum: mum
dad: dad
other: other
GridLayout:
cols: 2
row_force_default: True
row_default_height: '50dp'
Label:
text: 'Mum'
font_size: 30
CheckBox:
id: mum
on_press: root.save(mum.active, mum.text)
Label:
text: 'Dad'
font_size: 30
CheckBox:
id: dad
on_press: root.save(dad.active, dad.text)
Label:
text: 'Someone Else'
font_size: 30
TextInput:
id: other
multiline: False
font_size: 30
Button:
text: 'Book'
font_size: 30
valign: 'middle'
on_press: root.save()
Button:
text: 'Cancel'
font_size: 30
valign: 'middle'
on_press: root.manager.current = 'first'
<FirstScreen>:
name: 'first'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Go Back'
font_size: 40
BoxLayout:
Button:
text: 'Back To Who'
font_size: 40
on_press: root.manager.current = 'who'
The outcome usually revolves around this:
File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.getattr
AttributeError: 'CheckBox' object has no attribute 'text'
Expected outcome is obviously saved json data. I need to be able to select one or the other or even all at once. I have tried all I can find. My cat is scared of me. I need help. Thank you.

Kivy AnchorLayout .Kv file

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

Extremely oversized/large buttons inside Kivy Screen class

For the sake of brevity I'll explain my problem before posing the code. Here it goes:
I have a Screen class in Kivy that holds two widgets, a GridLayout and an Image. The latter is fine, but the buttons are extremely oversized:
And here's my main.py:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.factory import Factory
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.config import Config
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
import configparser
Builder.load_file('kv\MainMain.kv')
#read configurations
config = configparser.RawConfigParser()
config.read('config.ini')
#read config values
width = config.getint('Default', 'MAX_WINDOW_WIDTH')
height = config.getint('Default', 'MAX_WINDOW_HEIGHT')
border = config.getint('Default', 'BORDERLESS')
#apply config values
Config.set('graphics','width', width)
Config.set('graphics', 'height', height)
Config.set('graphics', 'borderless', border)
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
#create screen manager
class ScreenManager(ScreenManager):
pass
#create background widget
class BackGround(Image):
pass
#image buttons
class MainButtons(GridLayout):
pass
#create main screen:
class MainScreen(Screen):
pass
#create main app
class MainMainApp(App):
def build(self):
return MainScreen()
#register class
Factory.register(MainScreen, cls=MainScreen)
#run
if __name__ == '__main__':
MainMainApp().run()
And here's my kv file:
<ScreenManager>:
id: screen_manager
MainScreen:
id: main_screen
name: 'MainScreen'
manager: screen_manager
ReadScreen:
id: read_screen
name: 'ReadScreen'
manager: screen_manager
<MainScreen>:
BackGround:
id: back_ground
source: 'images\\app_background.jpg'
size: root.width, root.height
MainButtons:
cols: 1
pos: root.width / 2 - 100, root.height / 4
size: 20, 10
Button:
id: button_read
text: "Read"
on_press: root.callback_read()
Button:
id: button_add
text: "Add"
Button:
id: button_manage
text: "Manage"
I'm really swamped on this one. Thanks for your help.
You can use size_hint for this.
Here is an example
The pyhton file:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
Builder.load_file("my.kv")
class MainButtons(GridLayout):
pass
class MainScreen(Screen):
pass
class MainMainApp(App):
def build(self):
self.sm = ScreenManager()
self.sm.add_widget(MainScreen(name="main"))
return self.sm
if __name__ == '__main__':
MainMainApp().run()
and kivy file:
<MainScreen>:
MainButtons:
cols: 1
rows: 4
Label:
font_size: "40sp"
text: "Something"
Button:
size_hint: (1,0.1)
text: "Read"
Button:
size_hint: (1,0.1)
text: "Add"
Button:
size_hint: (1,0.1)
text: "Manage"
And output will be:
And if you want the buttons to be smaller in width, you add ´size_hint_x: None´ to your kv file, like this.
<MainScreen>:
MainButtons:
cols: 1
rows: 4
Label:
font_size: "40sp"
text: "Something"
Button:
size_hint: (1,0.1)
size_hint_x:None
text: "Read"
Button:
size_hint: (1,0.1)
size_hint_x:None
text: "Add"
Button:
size_hint: (1,0.1)
size_hint_x:None
text: "Manage"
Output will now be:

Categories