Kivy Checkbox restrict click - python

I am trying to create a checkbox in my float layout area. I am getting the checkbox to generate but every time I click it goes from True to False even if it not on the box. Could someone provide some insight?
My code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.checkbox import CheckBox
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
#######``Windows``#######
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
def add(self, *args):
a = self.ids["a"].text
a = int(a)
self.ids["adds"]
plus = 1 + a
print plus
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("GUI_Style.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
My KV File:
#: import NoTransition kivy.uix.screenmanager.NoTransition
ScreenManagement:
transition: NoTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
background_color: 0.5, 1, 0.22, 1
name: "main"
FloatLayout:
Button:
text: "Quit"
font_size: 50
color: 0,1,0,1
font_size: 25
size_hint: 0.3,0.2
pos_hint: {"x":0, "y":0}
on_release: app.window().stop()
Button:
on_release: app.root.current = "other"
text: "Next Screen"
font_size: 50
color: 0,1,0,1
font_size: 25
size_hint: 0.3,0.2
pos_hint: {"right":1, "top":1}
<AnotherScreen>:
name: "other"
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
CheckBox:
pos_hint: {"x":0.2, "y":0.2}
on_release: True
TextInput:
id: a
font_size: 25
password: True
pos_hint: {"x":0.5, "y":0.5}
size_hint: 0.3,0.2
text: "Insert Side A here"
Button:
id: adds
background_color: 0,0,1,1
color: 0,1,0,1
font_size: 25
on_release: root.add()
pos_hint: {"x":0, "y":0}
size_hint: 0.3,0.2
text: "plus"
Button:
color: 0,1,0,1
background_color: 0,0,1,1
font_size: 25
size_hint: 0.3,0.2
text: "Back Home"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}

Try placing the CheckBox in a Widget and it works. I mean:
FloatLayout:
Widget:
CheckBox:
pos_hint: {"x":0.2, "y":0.2}
on_release: True
TextInput:
id: a
font_size: 25
password: True
pos_hint: {"x":0.5, "y":0.5}
size_hint: 0.3,0.2
text: "Insert Side A here"
Button:
id: adds
background_color: 0,0,1,1
color: 0,1,0,1
font_size: 25
on_release: root.add()
pos_hint: {"x":0, "y":0}
size_hint: 0.3,0.2
text: "plus"
of course it ruins your layout but it solves the problem you are having.

Related

SOLVED read comments :: AttributeError: 'databaseWindow' object has no attribute 'menu' , in KivyMD

Im trying to add a MDDropDownMenu in my databaseWindow, but each time I try to access the menu, the app crashes with an error of "'databaseWindow' object has no attribute 'menu'". Im planning to use the dropdown menu as a list of options for the user to choose from so they can run a query. Im new to KivyMD and any assistance would be appreciated.
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import ObjectProperty
import mysql.connector
class myLoginScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class databaseWindow(Screen):
def dbtable_menu(self):
layout = AnchorLayout()
menu_items = [
{
"text": f"Item {i}",
"right_text": f"R+{i}",
"right_icon": "apple-keyboard-command",
"left_icon": "git",
"viewclass": "Item",
"height": dp(54),
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller = self.manager.get_screen('database1').ids['button'],
items = menu_items,
width_mult=4,
)
self.add_widget(self.menu)
def menu_callback(self, text_item):
print(text_item)
self.manager.get_screen('database1').ids['button'].text = str(text_item)
class MainApp(MDApp):
def build(self):
self.screen = Builder.load_file('login3.kv')
sm = ScreenManager()
sm.add_widget(myLoginScreen(name='main'))
sm.add_widget(databaseWindow(name='database1'))
return sm
if __name__ == "__main__":
MainApp().run()
Here is the kivy file:
#:import Window kivy.core.window.Window
#:import utils kivy.utils
ScreenManager:
myLoginScreen:
databaseWindow:
<myLoginScreen>:
id:main
name:'main'
bg_color: (1,1,1,.3)
highlight_color1jbsidis: [0,0,0,0]
highlight_color2jbsidis: (0,0,0,.3)
canvas.before:
Color:
rgba: utils.get_color_from_hex('#0A609E')
Rectangle:
size: self.size
pos: self.pos
FloatLayout:
#Image:
# source: '/Users/logo.png'
# pos_hint: {'right':.99, 'top':.85}
# size_hint_y: None
# height: dp(40)
# size: self.size
# pos: self.pos
# allow_stretch: False
# keep_ratio: True
TextInput:
id: username
hint_text: "Username"
foreground_color: [1,1,1,1]
background_color: [0,0,0,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
font_size: 40
size_hint: .7 ,.1 #.06
pos_hint: {'center_x':.5, 'center_y':.65} #'center_y':.8
canvas.before:
Color:
rgba: [1,1,1,.5]
RoundedRectangle:
size: self.size
pos: self.pos
radius: 10,
FloatLayout:
MDIconButton:
text: "[u]Reset password[/u]"
icon: 'account'
markup: True
color: Window.clearcolor
opposite_colors: True
pos_hint: {'center_x':.8, 'center_y':.65}
TextInput:
id: password
hint_text: "Password"
foreground_color: [1,1,1,1]
background_color: [0,0,0,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
password: True
size_hint: .7 ,.1 #.06
font_size: 40
pos_hint: {'center_x':.5, 'center_y':.45}
canvas.before:
Color:
rgba: [1,1,1,.5]
RoundedRectangle:
size: self.size
pos: self.pos
radius: 10,
FloatLayout:
MDIconButton:
icon: 'lock-outline'
markup: True
color: Window.clearcolor
opposite_colors: True
pos_hint: {'center_x':.8, 'center_y':.45}
MDRaisedButton:
id: sign_button
text: "Sign In"
pos_hint: {"center_x": .5, "top": .3}
on_release:
app.root.current = "database1"
MDRaisedButton:
id: button
text: "Sign up"
size_hint: .2 ,.05
pos_hint: {"right": .85, "center_y": .1}
opacity: 1
disabled: False
bg_color: (0,0,0,1)
color: [0,0,0,1]
<databaseWindow>:
id: database1
name: 'database1'
canvas.before:
Color:
rgba: utils.get_color_from_hex('#0A609E')
Rectangle:
size: self.size
pos: self.pos
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint:{"center_x":.5, "center_y":0.5}
on_release: root.menu.open()
MDTopAppBar:
title: "Select ONLY 1 Row for Query"
pos_hint: {'top':1}
elevation: 5
MDBoxLayout:
orientation: 'vertical'
padding: 10
MDFloatingActionButton:
icon: 'logout-variant'
on_press:
app.root.current = 'main'
root.log_out()
# GUI for pop up window
<P>:
Label:
text : "Please enter valid information"
size_hint : 0.2, 0.1
pos_hint : {"x" : 0.3, "top" : 0.8}

KivyMD MDRectangleFlatIconButton's text is in two lines when text is updated with a long text

I want to update MDRectangleFlatIconButton's text. It changes with a new text but when the new text is longer than previous text, then the text fits in two lines.
When i use a normal button, the new text fits in one line with proper adjustment.
but Since there is a Icon in MDRectangleFlatIconButton, when a new text is longer than previous text, the text fits in a two line.
To run the program,
Add a new app name which is longer than "Info" inside "Info" button's popup window, then click "Update Top Bar's name". Then, it updated title and the text of "Info" button at the front main App.
I have tried to change this by adding button's text_size: self.width, valign:"center", haling: "center", or manually adding text_size: cm(10), cm(10).
Also, i tried with on_release: "app.root.ids.bt_information.text_size = self.width, None
but nothing works so far.
I greatly appreciate your help.
Python code
'''
from kivy.uix.widget import Widget
'''Setting the size of first window for program'''
from kivy.config import Config #another way of setting size of window
Config.set('graphics', 'width', '600') # from kivy.core.window import Window
Config.set('graphics', 'height', '750') # Window.size = ("600", "750")
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
Builder.load_file('new_window_popup.kv')
class Dex(Popup):
pass
class Remi(Popup):
pass
class Info(Popup):
pass
class MyLayout(Widget):
pass
class AwesomeApp(MDApp):
def build(self):
self.title = "My house"
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()
'''
kivyfile: new_window_popup.kv
'''
#:import Factory kivy.factory.Factory
#:import MDRaisedButton kivymd.uix.button
<Dex>:
auto_dismiss: False
size_hint: 1, 1
title: "Weight-Based Dose Calculator "
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
orientation: "vertical"
size:root.width, root.height
Label:
text: "Dex 1"
Button:
text: "Close"
font_size: 24
on_release: root.dismiss()
<Remi>:
auto_dismiss: False
size_hint: 1, 1
title: "Weight-Based Dose Calculator "
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
orientation: "vertical"
size:root.width, root.height
Label:
text: "Remi"
Button:
text: "Close"
font_size: 24
on_release: root.dismiss()
<Info>:
appName:appName
auto_dismiss: False
size_hint: 1, 1
title: "Change Info"
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
orientation: "vertical"
size:root.width, root.height
Label:
text: "What is your App name?"
BoxLayout:
orientation: "horizontal"
MDTextField:
id: appName
hint_text: "App Name"
color_mode: 'primary'
current_hint_text_color: 1,1,1,1
hint_text_color_focus: 1,1,1,.9
line_color_focus: 1,1,1,1
font_size: '25sp'
text_color_normal: 1,1,1,.9
text_color_focus: 0,0,1,.9
focus: True
write_tab: False
Button:
text: "Update Top Bar\'s name"
font_size: 24
size_hint: .8, .2
# on_release: root.updateName()
on_release:
app.title = appName.text
app.root.ids.bt_information.text = appName.text
Button:
text: "Close"
font_size: 24
on_release: root.dismiss()
<MyLayout>
MDBoxLayout:
orientation:"vertical"
size: root.width, root.height
MDRaisedButton:
text: "Dex"
font_size: 32
text_color: 0,0,0,.9
size_hint: 1,.5
on_press: Factory.Dex().open()
MDRaisedButton:
text: "Remi"
font_size: 32
size_hint: 1,.5
on_press: Factory.Remi().open()
MDRectangleFlatIconButton:
id: bt_information
text: "Info"
icon: "youtube-studio"
font_size: 32
size_hint: 1,.2
text_size: self.width, None
md_bg_color: 0.95,0.61,0.73,1
on_press: Factory.Info().open()
'''

Kivy - How to add multiple Buttons to ScrollView inside ModalView?

I want to write a mobile app with a KivyMD like Bottom Sheet Menu. My problem with KivyMD Buttom Sheet is, when I click the button to open it, takes very long time (depends on the menu length) because the list is generated by calling the function every time the button was pressed. So I want to write my own solution for this.
In my kv file I manually added 20 buttons to see, it's everything work. But i didn't find the way to do it in python file with loop.
Can anyone help me please to add more than 30 buttons to modalview to be scrollable?
Here is my python file:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.window import Window
Builder.load_file('mylayout.kv')
Window.size = (350, 700)
class MyLayout(BoxLayout):
pass
class MainApp(App):
def build(self):
return MyLayout()
MainApp().run()
an my kv file:
#:import Factory kivy.factory.Factory
<MyPopup#ModalView>
auto_dismiss: True
size_hint: 1, 0.5
pos_hint: {'x': 0, 'top': 0.5}
background_color: 0,0,0,0
background_normal: ''
canvas.before:
Color:
rgba: 48/150,84/150,150/150,1
Rectangle:
size: self.size
pos: self.pos
ScrollView:
#do_scroll_x: False
GridLayout:
id: container1
cols: 1
size_hint: None, None
size: root.width, 1200
pos_hint: {'center_x': .5, 'center_y': .5}
MyButton:
text: '1'
on_press:
root.dismiss()
print(1)
MyButton:
text: '2'
on_press:
root.dismiss()
print(2)
MyButton:
text: '3'
on_press:
root.dismiss()
print(3)
MyButton:
text: '4'
on_press:
root.dismiss()
print(4)
MyButton:
text: '5'
on_press:
root.dismiss()
print(5)
MyButton:
text: '6'
on_press:
root.dismiss()
print(6)
MyButton:
text: '7'
on_press:
root.dismiss()
print(7)
MyButton:
text: '8'
on_press:
root.dismiss()
print(8)
MyButton:
text: '9'
on_press:
root.dismiss()
print(9)
MyButton:
text: '10'
on_press:
root.dismiss()
print(10)
MyButton:
text: '11'
on_press:
root.dismiss()
print(11)
MyButton:
text: '12'
on_press:
root.dismiss()
print(12)
MyButton:
text: '13'
on_press:
root.dismiss()
print(13)
MyButton:
text: '14'
on_press:
root.dismiss()
print(14)
MyButton:
text: '15'
on_press:
root.dismiss()
print(15)
MyButton:
text: '16'
on_press:
root.dismiss()
print(16)
MyButton:
text: '17'
on_press:
root.dismiss()
print(17)
MyButton:
text: '18'
on_press:
root.dismiss()
print(18)
MyButton:
text: '19'
on_press:
root.dismiss()
print(19)
MyButton:
text: '20'
on_press:
root.dismiss()
print(20)
<MyLayout>
orientation: 'vertical'
size: root.width, root.height
Label:
size_hint: 1, 0.9
text: 'main'
font_size: 24
Button:
size_hint: 1, 0.1
text: 'menu'
font_size: 24
on_release: Factory.MyPopup().open()
<MyButton#Button>
background_color: 0,0,0,0
background_normal: ''
canvas.before:
Color:
rgba: (48/255,84/255,150/255,1) if self.state == 'normal' else (43/255,108/255,229/255,1)
Rectangle:
size: self.size
pos: self.pos
In order to build the MyPopup filled with MyButtons, you must either define those classes in the python code or use Factory to create the instances. Here is a modified version of your kv to do this:
<MyPopup#ModalView>
auto_dismiss: True
size_hint: 1, 0.5
pos_hint: {'x': 0, 'top': 0.5}
background_color: 0,0,0,0
background_normal: ''
canvas.before:
Color:
rgba: 48/150,84/150,150/150,1
Rectangle:
size: self.size
pos: self.pos
ScrollView:
#do_scroll_x: False
GridLayout:
id: container1
cols: 1
size_hint: None, None
width: root.width
height: self.minimum_height # let the GridLayout set its own height as needeed
pos_hint: {'center_x': .5, 'center_y': .5}
<MyLayout>
orientation: 'vertical'
size: root.width, root.height
Label:
size_hint: 1, 0.9
text: 'main'
font_size: 24
Button:
size_hint: 1, 0.1
text: 'menu'
font_size: 24
# on_release: Factory.MyPopup().open()
on_release: app.open_popup() # call app method to build MyPopup and fill it
<MyButton#Button>
background_color: 0,0,0,0
background_normal: ''
size_hint_y: None
height: 20
canvas.before:
Color:
rgba: (48/255,84/255,150/255,1) if self.state == 'normal' else (43/255,108/255,229/255,1)
Rectangle:
size: self.size
pos: self.pos
Note that the GridLayout height is set to self.minimum_height to allow for any number of MyButton children, and the MyButton height is set to a fixed value (so that GridLayout can calculate the minimum height). Also, the import of Factory is no longer needed in the kv.
The modified python code:
from kivy.app import App
from kivy.factory import Factory
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.window import Window
Builder.load_file('mylayout.kv')
Window.size = (350, 700)
class MyLayout(BoxLayout):
pass
class MainApp(App):
def build(self):
return MyLayout()
def open_popup(self, *args):
# create the popup (must use Factory since MyPopup is defined in kv)
self.popup = Factory.MyPopup()
# fill the GridLayout
grid = self.popup.ids.container1
for i in range(60):
grid.add_widget(Factory.MyButton(text=str(i), on_press=self.myButtPress))
# open popup
self.popup.open()
def myButtPress(self, butt):
print(butt.text)
self.popup.dismiss()
MainApp().run()

kivymd text field stills shows the hint_text in the field after typing

I am having trouble with MDTextField. It carries on displaying the hint inside the textfield while typing. I have tried setting the background color to hide it but that didn't work. I have looked at a few tutorials and everyone seems to be doing it the same way as me and it just works for other people so i feel i have done something really wrong.
Image of what happens this is my first post so i am not aloud to post images :)
Any help will be greatly appreciated. Thanks in advance.
main.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Windowfrom kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.theming import ThemeManager
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.graphics import Rectangle, Color
import pymysql
class WelcomeScreen(Screen):
pass
class LoginScreen(Screen):
pass
class RegisterScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
class MainApp(MDApp):
def build(self):
# theme_cls = ThemeManager()
return Builder.load_file("main.kv")
def change_screen(self, screen, direction):
self.root.transition.direction = direction
self.root.current = screen
if __name__ == "__main__":
MainApp().run()
main.kv
#: import Window kivy.core.window.Window
#: import MDLabel kivymd.uix.label.MDLabel
#: import Rectangle kivy.graphics.Rectangle
#: import Color kivy.graphics.Rectangle
WindowManager:
WelcomeScreen:
LoginScreen:
RegisterScreen:
<WelcomeScreen>:
name: "welcome_screen"
FloatLayout:
orientation: "vertical"
MDToolbar:
id: toolbar
title: "SecureIT 24/7"
pos_hint: {'top': 1}
Button:
text: 'Login'
color: 1, 1, 1, 1
size_hint: 0.25, 0.25
pos_hint: {"right":0.625, "top":0.80}
on_release:
app.root.current = "login_screen"
Button:
text: 'Register'
color: 1,1,1,1
size_hint: 0.25, 0.25
pos_hint: {"right":0.625, "top":0.55 }
on_release:
app.root.current = "register_screen"
<LoginScreen>:
name: "login_screen"
FloatLayout:
MDBanner:
id: banner
text: "Login"
# The widget that is under the banner.
# It will be shifted down to the height of the banner.
MDToolbar:
id: toolbar
title: "Login"
elevation: 10
pos_hint: {'top': 1}
left_action_items: [['arrow-left', lambda screen: app.change_screen('welcome_screen', 'right')]]
FloatLayout:
padding: [50,50,50,50]
spacing: 50
FloatLayout:
orientation: 'vertical'
Label:
text: 'Login'
font_size: 18
halign: 'center'
text_size: root.width-20, 20
color: 0,0,0,1
pos_hint: {"right":1, "top":1.25}
TextInput:
id: login
multiline:False
font_size: 28
size_hint: 0.50, 0.10
pos_hint: {"right":0.75, "top":0.70}
FloatLayout:
orientation: 'vertical'
Label:
text: 'Password'
halign: 'center'
font_size: 18
text_size: root.width-20, 20
color: 0,0,0,1
TextInput:
id: password
multiline:False
password:True
font_size: 28
size_hint: 0.50, 0.10
pos_hint: {"right":0.75, "top":0.45}
Button:
text: 'Login'
size_hint: 0.25, 0.25
font_size: 24
pos_hint: {"right":0.625, "top":0.30}
<WhiteLabel#MDLabel>
height: self.texture_size[1]
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
<CustomInput#MDTextField>
multiline:False
# required: True
mode: "line"
pos_hint: {"right": 0.456}
current_hint_text_color: [0,0,0,0.5]
<RegisterScreen>:
name: "register_screen"
FloatLayout:
MDBanner:
id: banner
text: "ioshrdioaoisdhf"
MDToolbar:
id: toolbar
title: "Register"
elevation: 10
pos_hint: {'top': 1}
left_action_items: [['arrow-left', lambda screen: app.change_screen('welcome_screen', 'right')]]
BoxLayout:
orientation: "vertical"
height: self.minimum_height * 2
size_hint_y: None
pos_hint: {"top": 2}
CustomInput:
id: firstname
hint_text: "color_mode = 'custom'"
CustomInput:
id: surname
hint_text: 'Surname'
CustomInput:
id: email
hint_text: 'Email'
By naming your kv file as main.kv, you are taking advantage of the automatic loading of correctly named kv files as described in the documentation. However, you are also loading the same file using Builder.load_file("main.kv"). Loading the same kv file more than once can cause the sort of problems you are seeing. You can fix your problem by simply eliminating the call to Builder.load_file("main.kv"), or removing the entire build() method, or by changing the name of either the kv file or the MainApp class.

Using jsonstore in Kivy

I created a GUI based off this question while trying to teach myself how to use jsonstore. I don't have the reputation points to add a comment so I'm asking my question here. I think I have the basic idea down but for some reason I can't save the data to a json file. I get the following error:
AttributeError: 'NoneType' object has no attribute 'text'
I've tried following the documentation but I can't see anywhere where it would explain what I'm doing wrong.
main.py
from kivy.storage.jsonstore import JsonStore
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
Window.clearcolor = (0.02745098, 0.074509804, 0.121568627, 1)
Window.size = (2000, 900)
class TitleScreen(Screen):
pass
class MainScreen(Screen):
pass
class CreateProfile(Screen):
First = ObjectProperty()
Middle = ObjectProperty()
Last = ObjectProperty()
def __init__(self, **kwargs):
super(CreateProfile, self).__init__(**kwargs)
self.store = JsonStore("bco.json")
self.load()
def save(self):
self.store.put('profile', first = self.label.text)
self.store.put('profile', middle = self.label.text)
self.store.put('profile', last = self.label.text)
def load(self):
try:
self.Last.text = self.store.get('profile')['score']
except KeyError:
pass
class CreatePacket(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("customwidget.kv")
class CustomWidgetApp(App):
def build(self):
return presentation
if __name__ == "__main__":
CustomWidgetApp().run()
customwidget.kv
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import hex kivy.utils.get_color_from_hex
#: import FocusBehaviors kivy.uix.behaviors.focus
ScreenManagement:
transition: FadeTransition()
TitleScreen:
MainScreen:
CreateProfile:
CreatePacket:
<MainLabel#Label>:
font_size:50
bold: True
size_hint_x: 1
size_hint_y: 1.85
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
<SubLabel#Label>:
font_size: 35
bold: True
halign: "center"
size_hint_x: 1
size_hint_y: 1.5
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
<OtherLabel#Label>:
font_size: 12
bold: True
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
text_size: self.size
<Button#Button>:
font_size: 20
bold: True
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
background_color: 0.02745098, 0.074509804, 0.121568627, .01
canvas.before:
Color:
rgba: 0.396078431, 0.803921569, 0.807843137, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
on_press: self.background_color = (0.396078431, 0.803921569, 0.807843137, 1)
on_release: self.background_color = (0.02745098, 0.074509804, 0.121568627, .01)
# TODO: Create a focus behavior to "Tab" between widgets
<TextInput#TextInput>:
font_size: 12
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
padding_x: 10
padding_y: 10
focus_next: None
focus_previous: None
unfocus_on_touch: True
background_color: 0.02745098, 0.074509804, 0.121568627, .01
canvas.before:
Color:
rgba: 0.396078431, 0.803921569, 0.807843137, 1
Line:
width: 1
rectangle: self.x, self.y, self.width, self.height
<TitleScreen>:
id: "title"
FloatLayout:
MainLabel:
text: "Easy Button"
size_hint_x: 1
size_hint_y: 1.25
SubLabel:
text: 'Test'
size_hint_x: 1
size_hint_y: 1
Button:
text: "Click Here To Continue"
on_release: app.root.current = "main"
size_hint: (.75, .15)
pos_hint: {'x': .12, 'y': .2}
<MainScreen>:
name: "main"
MainLabel:
text: "Home Page"
BoxLayout:
Button:
on_release: app.root.current = "create_profile"
text: "Create Profile"
size_hint: (.5, .15)
Button:
on_release: app.root.current = "create_packet"
text: "Create Packet"
size_hint: (.5, .15)
<CreateProfile>:
name: "create_profile"
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
MainLabel:
text: "Create Profile"
size_hint: (1, .15)
BoxLayout:
size_hint: (.95, .2)
pos_hint: {'x': 0, 'y': .85}
spacing: 10
padding: 10
halign: "left"
OtherLabel:
text: "First"
OtherLabel:
text: "Middle"
OtherLabel:
text: "Last"
BoxLayout:
size_hint: (.95, .07)
pos_hint: {'x': 0, 'y': .8}
spacing: 20
padding: 10
halign: "right"
text_size: self.size
TextInput:
id: 'First'
TextInput:
id: "Middle"
TextInput:
id: 'Last'
BoxLayout:
Button:
on_release: app.root.current = "main"
text: "back Home"
size_hint: (.5, .15)
Button:
on_release: root.save()
text: "Save Profile"
size_hint: (.5, .15)
<CreatePacket>:
name: "create_packet"
MainLabel:
text: "Select Packet"
FloatLayout:
Button:
on_release: app.root.current = "main"
text: "back Home"
size_hint: (1, .15)
Your code has several problems but the main one is that you do not understand how to expose any .kv widget to .py, one of the simplest ways is to use an ObjectProperty as you try to do but that property is not linked to the widget, I prefer to do the creation in the .kv for its simplicity.
On the other hand I recommend you avoid the abuse of try-except since it hides errors, the best thing is to verify.
Another error is that you are overwriting the value of profile in the .json, the idea is to save everything in one.
Considering the above, the solution is:
*.py
from kivy.app import App
from kivy.storage.jsonstore import JsonStore
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.clock import Clock
Window.clearcolor = (0.02745098, 0.074509804, 0.121568627, 1)
Window.size = (2000, 900)
class TitleScreen(Screen):
pass
class MainScreen(Screen):
pass
class CreateProfile(Screen):
def __init__(self, **kwargs):
super(CreateProfile, self).__init__(**kwargs)
self.store = JsonStore("bco.json")
Clock.schedule_once(lambda *args: self.load())
def save(self):
self.store.put('profile',
first = self.first.text,
middle = self.middle.text,
last = self.last.text)
def load(self):
if self.store.exists('profile'):
profile = self.store.get('profile')
v = [("first", self.first), ("middle", self.middle), ("last", self.last)]
for key, ti in v:
val = profile.get(key)
if val:
ti.text = val
class CreatePacket(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("customwidget.kv")
class CustomWidgetApp(App):
def build(self):
return presentation
if __name__ == "__main__":
CustomWidgetApp().run()
*.kv
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import hex kivy.utils.get_color_from_hex
#: import FocusBehaviors kivy.uix.behaviors.focus
ScreenManagement:
transition: FadeTransition()
TitleScreen:
MainScreen:
CreateProfile:
CreatePacket:
<MainLabel#Label>:
font_size:50
bold: True
size_hint_x: 1
size_hint_y: 1.85
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
<SubLabel#Label>:
font_size: 35
bold: True
halign: "center"
size_hint_x: 1
size_hint_y: 1.5
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
<OtherLabel#Label>:
font_size: 12
bold: True
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
text_size: self.size
<Button#Button>:
font_size: 20
bold: True
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
background_color: 0.02745098, 0.074509804, 0.121568627, .01
canvas.before:
Color:
rgba: 0.396078431, 0.803921569, 0.807843137, 1
Line:
width: 2
rectangle: self.x, self.y, self.width, self.height
on_press: self.background_color = (0.396078431, 0.803921569, 0.807843137, 1)
on_release: self.background_color = (0.02745098, 0.074509804, 0.121568627, .01)
# TODO: Create a focus behavior to "Tab" between widgets
<TextInput#TextInput>:
font_size: 12
color: 0.396078431, 0.803921569, 0.807843137, 1
font_name: '/home/jarren/PycharmProjects/BCO_Form_Filler/practice/pirulen rg.ttf'
padding_x: 10
padding_y: 10
focus_next: None
focus_previous: None
unfocus_on_touch: True
background_color: 0.02745098, 0.074509804, 0.121568627, .01
canvas.before:
Color:
rgba: 0.396078431, 0.803921569, 0.807843137, 1
Line:
width: 1
rectangle: self.x, self.y, self.width, self.height
<TitleScreen>:
id: "title"
FloatLayout:
MainLabel:
text: "Easy Button"
size_hint_x: 1
size_hint_y: 1.25
SubLabel:
text: 'Test'
size_hint_x: 1
size_hint_y: 1
Button:
text: "Click Here To Continue"
on_release: app.root.current = "main"
size_hint: (.75, .15)
pos_hint: {'x': .12, 'y': .2}
<MainScreen>:
name: "main"
MainLabel:
text: "Home Page"
BoxLayout:
Button:
on_release: app.root.current = "create_profile"
text: "Create Profile"
size_hint: (.5, .15)
Button:
on_release: app.root.current = "create_packet"
text: "Create Packet"
size_hint: (.5, .15)
<CreateProfile>:
name: "create_profile"
first: first
middle: middle
last: last
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
MainLabel:
text: "Create Profile"
size_hint: (1, .15)
BoxLayout:
size_hint: (.95, .2)
pos_hint: {'x': 0, 'y': .85}
spacing: 10
padding: 10
halign: "left"
OtherLabel:
text: "First"
OtherLabel:
text: "Middle"
OtherLabel:
text: "Last"
BoxLayout:
size_hint: (.95, .07)
pos_hint: {'x': 0, 'y': .8}
spacing: 20
padding: 10
halign: "right"
text_size: self.size
TextInput:
id: first
TextInput:
id: middle
TextInput:
id: last
BoxLayout:
Button:
on_release: app.root.current = "main"
text: "back Home"
size_hint: (.5, .15)
Button:
on_release: root.save()
text: "Save Profile"
size_hint: (.5, .15)
<CreatePacket>:
name: "create_packet"
MainLabel:
text: "Select Packet"
FloatLayout:
Button:
on_release: app.root.current = "main"
text: "back Home"
size_hint: (1, .15)
I found a work around that still allows me to append information into a JSON by using TinyDB. Here is the updated code:
from kivy.app import App
from kivy.storage.jsonstore import JsonStore
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from tinydb import TinyDB, Query
from kivy.uix.listview import ListItemButton
Window.clearcolor = (0.02745098, 0.074509804, 0.121568627, 1)
Window.size = (2000, 900)
db = TinyDB('bcodb.json')
class ProfileListButton(ListItemButton):
pass
class TitleScreen(Screen):
pass
class MainScreen(Screen):
pass
class CreateProfile(Screen):
def __init__(self, **kwargs):
super(CreateProfile, self).__init__(**kwargs)
self.store = JsonStore("bcodb.json")
def save(self):
db.insert({'first': self.first.text, 'middle': self.middle.text, 'last': self.last.text})
def update(self):
db.update({'first': self.first.text, 'middle': self.middle.text, 'last': self.last.text})
class CreatePacket(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("customwidget2.kv")
class CustomWidgetApp(App):
def build(self):
return presentation
if __name__ == "__main__":
CustomWidgetApp().run()
It's pretty easy to figure out with a simple database-like program I'm trying to develop. The .kv file is the same.

Categories

Resources