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.
Related
I'm trying to create a simple login page for this app using Kivy.
I'm new to this, and I'm wondering how I can connect the Email TextInput to a variable (email_catch) in my python code, similar to a normal .get() function.
Python Code
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty
class Login_Window(Screen):
def verify(self):
email_catch = self.root.ids.email.text
print(email_catch)
class MyApp(App):
def build(self):
return Login_Window()
if __name__ == "__main__":
MyApp().run()
.KV File
#:kivy 2.0.0
<Login_Window>:
GridLayout:
cols:1
size: root.width, root.height
GridLayout:
cols:2
Label:
text: 'Email'
TextInput:
multiline: False
id: email
Button:
text: 'Log In'
on_press: root.verify()
Since you are calling root in verify method of Login_Window class (where it (root) hasn't been defined yet) and not in the build method of the App class you are supposed to get an AttributeError .
In order to access an id within that class you should use self.ids. So the change you need:
def verify(self):
email_catch = self.ids.email.text
print(email_catch)
I'm relatively new to programming, little over 8 months or so, but I am learning kivy and python3 and I am attempting to create a password managing application and I would like for it to open a new screen when a button is pressed. but I am getting a Type Error.
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.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
class LoginScreen(Widget):
pass
class PasswordScreen(Widget):
pass
class PasswordApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(LoginScreen(name='login'))
sm.add_widget(PasswordScreen(name='passwords'))
return sm
if __name__ == '__main__':
PasswordApp().run()
heres the .kv code
<LoginScreen>:
GridLayout:
Button:
text: 'Goto passwords'
on_press: root.manager.current = 'passwords'
Button:
text: 'Quit'
<PasswordScreen>:
GridLayout:
Button:
text: 'My settings button'
Button:
text: 'logout'
on_press: root.manager.current = 'login'
Your LoginScreen and PasswordScreen extend Widget, and the Widget __init__() does not support a name property. Both those classes should extend Screen instead of Widget.
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()
I am new to Kivy language. I am trying build a simple program to switch between two screens. First screen with contain a button which on_release it will switch to second screen. On clicking the button on second screen will get to first screen.
Issues i face:
1. Button is placed on the corner and i am expecting its size to be full window but it small
On click and release the button doesnt' show any effect.
Chat.kv
<ChatGUI>:
MainManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text:"to second window"
on_release:app.root.current="second"
<SecondWindow>:
name: "second"
Button:
text:"back to main"
on_release:app.root.current="main"
python code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color, Canvas
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder
class ChatGUI(Widget):
present=Builder.load_file("Chat.kv")
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class MainManager(ScreenManager):
pass
class ChatApp(App):
def build(self):
return ChatGUI()
if __name__=="__main__":
ChatApp().run()
My output# i am not able to add image so posted link of output
I am practicing from youtube tutorial.
I have checked many codes from stack overflow and i don't see issues in my code.
output should display button of size occupying whole window and on_release it should switch to next screen.
Can you let me know what could be issue.
You do not need to add a ScreenManager inside a Widget.
So
class ChatGUI (ScreenManager):
in python file and
<ChatGUI>:
MainWindow:
SecondWindow:
in kv file
that's all I changed to make your example work.
Chat.kv
<ChatGUI>:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text:"to second window"
on_release:app.root.current="second"
<SecondWindow>:
name: "second"
Button:
text:"back to main"
on_release:app.root.current="main"
main.py
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color, Canvas
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder
class ChatGUI(ScreenManager):
present=Builder.load_file("Chat.kv")
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ChatApp(App):
def build(self):
return ChatGUI()
if __name__=="__main__":
ChatApp().run()
Problem 1 - widget # bottom left hand corner & not full window?
Button is placed on the corner and i am expecting its size to be full
window but it small
Root Cause
The Button widget appeared on the bottom left hand corner because the root is a Widget and no position (pos, or pos_hint) was provided. Therefore, the default position of (0, 0) was used.
The size is not a full window because by default the size of a Widget is (100 x 100) or the default size_hint is (1, 1).
Kivy Widget » Default values
A Widget is not a Layout: it will not change the position or the size of its children. If you want control over positioning or
sizing, use a Layout.
The default size of a widget is (100, 100). This is only changed if the parent is a Layout. For example, if you add a Label inside a
Button, the label will not inherit the button’s size or position
because the button is not a Layout: it’s just another Widget.
The default size_hint is (1, 1). If the parent is a Layout, then the widget size will be the parent layout’s size.
Problem 2 - on release button screen not switched?
On click and release the button doesnt' show any effect.
Root Cause
The screen was not switched when button press was released, because the root of the App is not a ScreenManager.
Solution
There are two options to the problems.
Option 1 - use Layout as root
This option use BoxLayout as the root and requires the following enhancements. A Layout can be a GridLayout, BoxLayout, FloatLayout, etc.
Py file
Replace Widget with BoxLayout
Replace present = Builder.load_file(...) with Builder.load_file(...)
Move Builder.load_file(...) out of class ChatGUI() and add pass
kv file
Add id: sm under instantiated object, MainManager:
Replace app.root.current with app.root.ids.sm.current
Snippets - Option 1
main1.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_file("main1.kv")
class ChatGUI(BoxLayout):
pass
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class MainManager(ScreenManager):
pass
class ChatApp(App):
def build(self):
return ChatGUI()
if __name__ == "__main__":
ChatApp().run()
main1.kv
<ChatGUI>:
MainManager:
id: sm
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text: "to second window"
on_release: app.root.ids.sm.current="second"
<SecondWindow>:
name: "second"
Button:
text: "back to main"
on_release: app.root.ids.sm.current="main"
Option 2 - use ScreenManager as root
This option requires the following enhancements:
Py file
Remove import statement, from kivy.uix.widget import Widget
Remove class ChatGUI()
Replace return ChatGUI() with return MainManager()
Replace present = Builder.load_file(...) with Builder.load_file(...)
kv file
Remove class rule, : in the kv file
Replace MainManager: with class rule, :
Snippets - Option 2
main2.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_file("main2.kv")
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class MainManager(ScreenManager):
pass
class ChatApp(App):
def build(self):
return MainManager()
if __name__ == "__main__":
ChatApp().run()
main2.kv
<MainManager>:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text: "to second window"
on_release: app.root.current="second"
<SecondWindow>:
name: "second"
Button:
text: "back to main"
on_release: app.root.current="main"
I'm a beginner with kivy language. I'm writing an app but I have some difficulties. My app is going to consist of a few screens so I decided to use the ScreenManager but each time I'm trying to launch the .py I get an error and this makes the python not responding. I have saved both the .py file and the .kv file in the same folder.
Traceback (most recent call last):
File "C:\Users\Eng. Aladdin Hammodi\Desktop\kivy\main.py", line 15, in <module>
presentation = Builder.load_file(Aladdin.kv)
NameError: name 'Aladdin' is not defined
Python file:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.lang import Builder
presentation = Builder.load_file(Aladdin.kv)
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class ScreenManager(ScreenManager):
pass
class AladdinApp(App):
def build(self):
return presentation
sample_app = AladdinApp()
sample_app.run()
aladdin.kv
<ScreenOne>:
name:screen1
FloatLayout:
canvas:
source:'image1'
Label:
text:'Hello\n Welcome to my app\n'
font_size: 40
Button:
text: 'Next'
pos: 0,1
font_size:20
hint_size:0.1,0.05
on_press:root.manager.current='screen2'
<ScreenTwo>:
name:screen2
FloatLayout:
canvas:
source:'image1'
Label:
text:'Please insert your name'
text:'Please insert your Password'
font_size: 40
Button:
text: 'Next'
pos: 0,1
font_size:20
hint_size:0.1,0.05
on_press:root.manager.current='screen1'
The problem is in the line:
presentation = Builder.load_file(Aladdin.kv)
Aladdin is interpreted as the variable. If you want to pass a string to the method, call it like:
presentation = Builder.load_file("Aladdin.kv")
The files had a couple of issues:
#Artur R. Czechowski already pointed out missing quotation marks Builder.load_file('Aladdin.kv')
you did not define a root widget in the kv file or in your python code. I changed this by returning it in the build method def build(self): return ScreenManager()
it is not hint_size, correct is size_hint
I think you tried having an image as background. This is one way of doing it, do not forget the file ending e.g. .jpg
canvas.before:
Rectangle:
pos:self.pos
size: self.size
source:'image1.jpg'
As a side note: It makes sense to build your app step by step. Write as little code as possible which works and then check. Write some more code, check again. Happy coding with kivy :).
python code:
import kivy
kivy.require("1.9.1")
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.lang import Builder
#### in comment requested to also make cursor visible and not full screen ####
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')
Config.set('graphics','show_cursor','1')
####
Builder.load_file('Aladdin.kv')
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class ScreenManager(ScreenManager):
pass
class AladdinApp(App):
def build(self):
return ScreenManager()
AladdinApp().run()
revised kv file:
<ScreenManager>:
ScreenOne:
ScreenTwo:
<ScreenOne>:
name:'screen1'
FloatLayout:
canvas.before:
Rectangle:
pos:self.pos
size: self.size
source:'image1.jpg'
Label:
text:'Hello\n Welcome to my app\n'
font_size: 40
Button:
text: 'Next'
pos: 0,1
font_size:20
size_hint:0.1,0.05
on_press:root.manager.current='screen2'
<ScreenTwo>:
name:'screen2'
FloatLayout:
canvas.before:
Rectangle:
pos:self.pos
size: self.size
source:'image1.jpg'
Label:
text:'Please insert your name'
#text:'Please insert your Password'
font_size: 40
Button:
text: 'Next'
pos: 0,1
font_size:20
size_hint:0.1,0.05
on_press:root.manager.current='screen1'