i am making a form screen in which a user can input data and upload it to database. I want them to be able to add multiple textfields on a button press if they still want to add something. it should be scrollable since the amount of textfields is based on how many the user really wants. i have this code right here but it doesn't really do what i had in mind.
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.textfield import MDTextField
class TextField(MDTextField):
pass
class Tab(MDFloatLayout, MDTabsBase):
pass
class FormScreen(Screen):
pass
class DemoApp(MDApp):
#function to add text field
def add_textfield(self):
self.help.get_screen('form').ids.box.add_widget(
TextField(hint_text= 'adsf',
))
def upload(self):
name = self.help.get_screen('form').ids.input_1.text
def build(self):
self.help = Builder.load_file('form.kv')
# screen.add_widget(self.help)
return self.help
Here is my kv file:
ScreenManager:
FormScreen:
<FormScreen>
name: 'form'
MDBoxLayout:
orientation: "vertical"
MDToolbar:
# md_bg_color:app.dark2
title: "Upload Data"
type_height: "small"
left_action_items: [["arrow-left", lambda x : app.swtchScreen('collections')]]
right_action_items: [["eraser", lambda x : root.eraser()],["plus", lambda x : app.add_textfield()]]
MDTabs:
id: tabs
background_color: rgba(0,0,0,0)
tab_hint_x: True
Tab:
title: "Passport Data"
MDTextField:
id: input_1
hint_text: "Name"
pos_hint: {"center_x": 0.5, "center_y": 0.95}
size_hint: .75,0.09
color_mode: 'accent'
mode: "rectangle"
#additional textfieldss
MDTextField:
id: box
Thank you
You can modify the Tab to customize the layout you need to hold all necessary fields as follows,
MDTabs:
id: tabs
background_color: rgba(0,0,0,0)
tab_hint_x: True
Tab:
title: "Passport Data"
MDBoxLayout: # Add main container.
orientation: "vertical"
padding: dp(10)
spacing: dp(5)
MDTextField:
id: input_1
hint_text: "Name"
# pos_hint: {"center_x": 0.5, "center_y": 0.95}
# size_hint: .75,0.09 # "size_hint_y" will be set automatically.
pos_hint: {"center_x": 0.5}
size_hint_x: .75
color_mode: 'accent'
mode: "rectangle"
#additional textfields
ScrollView:
MDBoxLayout: # Add all text fields in this container.
id: box
orientation: "vertical"
adaptive_height: True # Grow vertically.
Alternatively you could've inherited Tab from MDBoxLayout,
class Tab(MDBoxLayout, MDTabsBase):
pass
Then the kvlang would be,
MDTabs:
id: tabs
background_color: rgba(0,0,0,0)
tab_hint_x: True
Tab:
title: "Passport Data"
orientation: "vertical"
padding: dp(10)
spacing: dp(5)
MDTextField:
id: input_1
hint_text: "Name"
# pos_hint: {"center_x": 0.5, "center_y": 0.95}
# size_hint: .75,0.09 # "y" will be set automatically.
pos_hint: {"center_x": 0.5}
size_hint_x: .75
color_mode: 'accent'
mode: "rectangle"
#additional textfieldss
ScrollView:
MDBoxLayout: # Add all text field here.
id: box
orientation: "vertical"
adaptive_height: True # Grow vertically.
Related
from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivymd.app import MDApp
class ScrollViewExample(ScrollView):
pass
class FirstWindow(Screen):
def set_text(self):
my_title_input = self.ids.note_title
my_details = self.ids.note_details
fo = open("notes.txt", "a")
fo.write('\n'+'\n'+my_title_input.text)
fo.write('\n'+my_details.text)
fo.close()
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
def on_toggle_button_state(self):
dark_mode = self.ids.dark_mode
dark_mode.text = 'Feature not available'
pass
class FourthWindow(Screen):
#This class displays notes from a file in buttons
#works but only the notes showup
def build(self):
root = FourthWindow()
root.add_widget(FourthWindow(name='StackLayout'))
class StackLayoutExample(StackLayout):
class ScrollView(ScrollView):
pass
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = StackLayout(orientation='lr-tb')
with open('notes.txt', 'r') as fp:
data = fp.read()
splat = data.split("\n\n")
notes = []
for i, note in enumerate(splat, 0):
notes.append(note)
for i in range(0, len(notes)):
b = Button(text=notes[i], size_hint=(.25, .4))
self.add_widget(b)
class WindowManager(ScreenManager):
pass
class RelativeLayoutExample(RelativeLayout):
pass
class ScreeManager(ScreenManager):
pass
class LineMaker(Widget):
pass
class Noteit(App):
pass
Noteit().run()
Noteit.kv <---The kv file begins here
WindowManager:
FirstWindow:
SecondWindow:
ThirdWindow:
FourthWindow:
<FirstWindow>:
id: first_window
name: "Notes"
RelativeLayout:
Label:
text: "Notes"
size_hint: None,None
pos_hint: {'top':1,'center_x':.5}
font_size: 35
Button:
text: 'Account'
size_hint: None,None
pos_hint: {'top':1,'right':1}
on_release:
app.root.current = "Account"
root.manager.transition.direction= "left"
Button:
text: 'Preferences'
size_hint: None,None
pos_hint: {'top':1,'left':1}
on_release:
app.root.current = "Preferences"
root.manager.transition.direction= "right"
Label:
text: "Title:"
size_hint: None,None
pos_hint: {'top':.85,'center_x':.5}
font_size: 20
TextInput:
id: note_title
size_hint: .45,.08
pos_hint: {'top':.75,'center_x':.5}
multiline: False
Label:
text: "Details:"
size_hint: None,None
pos_hint: {'top':.65,'center_x':.5}
font_size: 20
TextInput:
id: note_details
size_hint: .45,.3
pos_hint: {'top':.55,'center_x':.5}
multiline: True
Button:
text: "Submit"
size_hint: .25,.1
length: "100dp"
pos_hint: {'top':.25,'center_x':.5}
on_release:
root.set_text()
LineMaker:
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
width: 2.
points: (self.width,self.height*.83,0,self.height*.83)
Button:
text: 'View All Notes'
size_hint:.25,.15
pos_hint: {'bottom':1,'center_x':.5}
on_release:
app.root.current = "View"
root.manager.transition.direction= "up"
<SecondWindow>:
name: "Account"
RelativeLayout:
Label:
text: "Account"
size_hint: None,None
pos_hint: {'top':1,'center_x':.5}
font_size: 35
Button:
text: 'Notes'
size_hint: None,None
pos_hint: {'top':1,'left':1}
on_release:
app.root.current = "Notes"
root.manager.transition.direction= "right"
Button:
text: 'Preferences'
size_hint: None,None
pos_hint: {'top':1,'right':1}
on_release:
app.root.current = "Preferences"
root.manager.transition.direction= "left"
Label:
text: "Username:"
size_hint: None,None
pos_hint: {'top':.85}
font_size: 20
TextInput:
size_hint: .5,.1
pos_hint: {'top':.55,'left':.3}
multiline: False
Label:
text: "Password:"
size_hint: None,None
pos_hint: {'top':.65}
font_size: 20
TextInput:
size_hint: .5,.1
pos_hint: {'top':.75,'left':.3}
multiline: False
Button:
text: "Log In"
size_hint: .25,.1
length: "100dp"
pos_hint: {'top':.45}
Button:
text: "Sign Up"
size_hint: .25,.1
length: "100dp"
pos_hint: {'top':.45,'center_x':.375}
LineMaker:
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
width: 2.
points: (self.width,self.height*.83,0,self.height*.83)
<ThirdWindow>:
name: "Preferences"
RelativeLayout:
Label:
text: "Preferences"
size_hint: None,None
pos_hint: {'top':1,'center_x':.5}
font_size: 35
Button:
text: 'Notes'
size_hint: None,None
pos_hint: {'top':1,'right':1}
on_release:
app.root.current = "Notes"
root.manager.transition.direction= "left"
Button:
text: 'Account'
size_hint: None,None
pos_hint: {'top':1,'left':1}
on_release:
app.root.current = "Account"
root.manager.transition.direction= "right"
ToggleButton:
text: 'Dark Mode'
size_hint: .25,.1
pos_hint: {'top':.75,'center_x':.25}
on_state: root.on_toggle_button_state()
Label:
id: dark_mode
text: ''
LineMaker:
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
width: 2.
points: (self.width,self.height*.83,0,self.height*.83)
<FourthWindow>:
name: "View"
RelativeLayout:
Button:
text: 'Note Submission'
size_hint:.25,.15
pos_hint: {'top':1,'center_x':.5}
on_release:
app.root.current = "Notes"
root.manager.transition.direction= "down"
Label:
text: 'All Notes'
size_hint: None,None
pos_hint: {'top':1,'right':.9}
font_size: 35
How do I make the stacklayout code showup with the kv file code? Or at least make the stacklayout part of the screen.
Right now the only option I have is either take the screen as output or the stack layout as output.
Replacing the def build(self) inside the class FourthWindow(Screen) will show the whole application. The code inside the def build(self) also works but they do not work together.
I am new to kivy and object oriented programming so I do not know much about this topic so I could be missing something really basic.
I think this problem is easy to solve, but i dont have experience enough to do this.
So, I want to reference the input text (MDTextField on kv file) for the variable "name" that is inside the press() function, so that I can write inside the popup.
I'm learning kivymd for a college project, so, discount the errors and unnecessary informations :/
Here is my kv file
Screen:
id: screen1
NavigationLayout:
id: navigation
ScreenManager:
id: screen_manager
Screen:
id: fact
name: 'fact'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: "Fact Checker"
font_size: 56
font_name: "MontserratBold"
pos_hint: {'center_x': 0.75, 'center_y': 0.7}
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 1)
MDTextField:
id: input_box
hint_text: "Type here"
font_size: 18
pos_hint: {'center_x': 0.5, 'center_y': 0.45}
size_hint_x: None
width: 500
multiline: True
helper_text: "Type the title of the news"
helper_text_mode: "on_focus"
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 1)
line_color_normal: 1, 1, 1, 1
MDRectangleFlatButton:
text: "Check"
pos_hint: {'center_x': 0.5, 'center_y': 0.25}
theme_text_color: "Custom"
text_color: (1, 1, 1, 1)
on_press: app.press()
MDLabel:
markup: True
text: "<Developed by: [color=#cfd0d1]Caio Barreto and Gabriel Queiroz[/color]>"
font_name: "Montserrat"
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 0.5)
pos_hint: {'center_x': 0.74, 'center_y': 0.1}
Screen:
name: 'discord'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: 'teste1'
Screen:
name: 'add'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: 'teste2'
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
ScrollView:
DrawerList:
id: md_list
MDList:
OneLineIconListItem:
text: "Menu"
on_press: screen_manager.current = "fact"
IconLeftWidget:
icon: "home"
OneLineIconListItem:
text: "Discord"
on_press: screen_manager.current = "discord"
IconLeftWidget:
icon: "discord"
OneLineIconListItem:
text: "Add more news"
on_press: screen_manager.current = "add"
IconLeftWidget:
icon: "circle-edit-outline"
Here is my python code:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton, MDFlatButton
from kivymd.uix.navigationdrawer import NavigationLayout
from kivymd.uix.list import MDList
from kivymd.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivy.core.text import LabelBase
from kivy.uix.widget import Widget
from helper import username_input
from kivymd.uix.screen import Screen
LabelBase.register(name="MontserratBold", fn_regular="font/Montserrat-Bold.ttf")
LabelBase.register(name="Montserrat", fn_regular="font/Montserrat-Medium.ttf")
class FactCheckerApp(MDApp):
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
pass
def build(self):
self.file = Builder.load_file('projeto.kv')
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "LightBlue"
print(self.file.Screen.ids.)
return self.file
def press(self):
name = self.screen1.navigation.screen_manager.fact.input_box.text
close_button = MDFlatButton(text='Close', on_release=self.close_dialog)
self.dialog = MDDialog(title='Answer', text=name, size_hint=(0.5, 1), buttons=[close_button])
self.dialog.open()
def close_dialog(self, obj):
self.dialog.dismiss()
if __name__ == '__main__':
FactCheckerApp().run()
To access the MDTextField, you can just reference it through its id, which is defined in the widget that is the root of the kv rule where the id is defined (in this case, in the Screen). Try changing:
name = self.screen1.navigation.screen_manager.fact.input_box.text
to:
name = self.root.ids.input_box.text
in the press() method.
So I'm working on making a fully functional login screen for my first serious kivy app and I want to be able to change windows/screens when the user presses on a button. I know normally in the KV file i'd just use on release but I want the on release to call to a method to verify the users credentials, and if those credentials are correct then change screens. So how in python itself would I be able to call to screen manager to change the screen?
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
class LoginLayout(Screen):
def login(self, **kwargs):
print("Login function working")
userEmail = self.ids.username.text
userPassword = self.ids.password.text
print(userEmail)
print(userPassword)
ScreenManager.current('emailWindow')
class EmailWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('loginScreen.kv')
class LoginScreen(App):
def build(self):
return kv
app = LoginScreen()
app.run()
KV
ScreenManager:
LoginLayout:
EmailWindow:
<LoginLayout>:
name: 'loginWindow'
canvas.before:
Color:
rgba: (28/255, 102/255, 137/255, 1)
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'Username'
font_name: 'Framd.ttf'
font_size: 20
TextInput:
id: username
multiline: False
size_hint: (.5, .3)
pos_hint: {'center_x' : .5}
Label:
text: 'Password'
font_name: 'Framd.ttf'
font_size: 20
TextInput:
id: password
multiline: False
size_hint: (.5, .3)
pos_hint: {'center_x' : .5}
Button:
text: 'Login'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
on_release: root.login()
Button:
text: 'Create Account'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
Button:
text: 'Forgot login Info'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
<EmailWindow>:
name: 'emailWindow'
canvas.before:
Color:
rgba: (28/255, 102/255, 137/255, 1)
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'To:'
font_name: 'Framd.ttf'
TextInput:
multiline: False
pos_hint: {'center_x': 0.5}
size_hint: (.5, .3)
font_name: 'Framd.ttf'
Label:
text: 'Subject'
TextInput:
multiline: False
pos_hint: {'center_x': 0.5}
size_hint: (.5, .3)
font_name: 'Framd.ttf'
Label:
text: 'Body'
font_name: 'Framd.ttf'
TextInput:
size_hint: (.5, .7)
pos_hint: {'center_x': 0.5}
multiline: True
Button:
text: 'send'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
When a Screen gets added to a ScreenManager, it gets a manager attribute set to the ScreenManager. So in your login() method you can do:
self.manager.current = 'emailWindow'
instead of:
ScreenManager.current('emailWindow')
Note that using ScreenManager, as you do above, references the ScreenManager class, not the ScreenManager instance that is in your App.
I have started creating small app and I'm stuck. I tried many different ideas from this forum but nothing fit my app. I'm already confused how should I do this.
All I want is to take the input from MDTextField (which is provided by user) and show it in the Label's text on the Second Screen.
My .kv file:
ScreenManager:
First:
Second:
<First#Screen>:
name: 'screen1'
MDTextFieldRound:
id: getpath
text: "Enter player name"
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint_x: None
width: 300
mode: "rectangle"
icon_right: "scissors-cutting"
MDRectangleFlatButton:
text: "Submit"
pos_hint: {'center_x': .5, 'center_y': 0.4}
on_release: root.manager.current = 'screen2'
<Second#Screen>:
name: 'screen2'
canvas:
Rectangle:
source: 'image.jpg'
pos: self.pos
size: self.size
Label:
id: lab
text: ''
My main.py:
class PKNApp(MDApp):
def build(self):
pass
PKNApp().run()
If you add an id for First Screen in your kv, like this:
ScreenManager:
First:
id: first
Second:
Then you can use that id to access the TextInput, like this:
Label:
id: lab
text: root.manager.ids.first.ids.getpath.text
My application is simple, the ScreenTwo is showing a form 'Parametreur' with various options to be entered.
What I'm trying to do is to set a 'save' button at the end of this form that will register into a list called 'resultat' all the options furfilled so far leaving '0' in the empty inputs.
The global variable 'resultat' would look like this ['blabla','15/06/2018','31/12/1999','6'].
File.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_file("HadrianRunningApp.kv")
class ScreenMenu(Screen):
pass
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class Parametreur(FloatLayout):
def build(self):
pass
def submit_input(self):
# Get the student name from the TextInputs
label_name = self.label_text_input.text
resultat.insert(0,label_name)
def submit_input2(self):
date_debut_name = self.date_debut_input.text
resultat.insert(1,date_debut_name)
def submit_input3(self):
date_fin_name = self.date_fin_input.text
resultat.insert(2,date_fin_name)
def submit_input4(self):
var_duree_estimation = self.duree_estimation_input.text
resultat.insert(3,var_duree_estimation)
def save_input(self):
print(resultat)
class Manager(ScreenManager):
screen_menu = ObjectProperty(None)
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=WipeTransition())
return m
if __name__=='__main__':
ScreensApp().run()
The Kivy file .kv:
#:kivy 1.9.0
<ScreenMenu>:
BoxLayout:
orientation:'vertical'
size: root.size
padding: "20dp"
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
<ScreenOne>:
BoxLayout:
spacing: 20
padding: 20
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
<ScreenTwo>:
Parametreur
<Parametreur>:
label_text_input: label_text
date_debut_input: date_debut
date_fin_input: date_fin
duree_estimation_input: duree_estimation
resultat: resultat
RelativeLayout:
orientation: 'vertical'
GridLayout:
size_hint: (1., 0.11)
pos_hint: {'right': 1, 'center_y': 0.91}
padding: 6
spacing: "4dp"
cols:2
rows:1
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
GridLayout:
size_hint: (1., 0.8)
pos_hint: {'right': 1, 'center_y': 0.45}
padding: "7dp"
spacing: 5
cols:2
rows:7
Button:
text: "Liste Actif"
Label:
text: " "
BoxLayout:
Label:
text: "Date du début"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input2()
TextInput:
id: date_debut
text: 'dd/mm/YYYY'
BoxLayout:
Label:
text: "Date de fin"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input3()
TextInput:
id: date_fin
text: 'dd/mm/YYYY'
Label:
text: "Pourcentage de séparation \n de la base (validation/test)"
Button:
text: "Open to Close"
Button:
text: "Close to Close"
Button:
text: "les 3 (6 en tout) frontières des VSs"
BoxLayout:
Label:
text: "Durée pour la réestimation \n des modèles (en jours)"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input4()
TextInput:
id: duree_estimation
text: 'dd/mm/YYYY'
Label:
text: "variable selection NMF/Entropy"
Label:
text: "Kernel/damiers/buntcher/\n neurone/XGBoost/Gradient boosting"
# We create the widgets
BoxLayout:
spacing: "0dp"
Label:
id: label_text
text: "0"
font_size: "30dp"
BoxLayout:
orientation: "vertical"
spacing: "3dp"
Button:
text: "Add"
on_release: label_text.text = str(int(label_text.text) + 1)
Button:
text: "Subtract"
on_release: label_text.text = str(int(label_text.text) - 1)
Button:
text: "Submit"
size_hint_x: None
on_press: root.submit_input()
BoxLayout:
padding: "2dp"
spacing: "2dp"
size_hint: (1., 0.50)
pos_hint: {'right': 1, 'center_y': 0.09}
Button:
id: resultat
text:"Save"
size_hint_x: None
on_press: root.save_input()
<Manager>:
id: screen_manager
screen_menu: screen_menu
screen_one: screen_one
screen_two: screen_two
ScreenMenu:
id: screen_menu
name: 'screen0'
manager: screen_manager
ScreenOne:
id: screen_one
name: 'screen1'
manager: screen_manager
ScreenTwo:
id: screen_two
name: 'Parametreur'
manager: screen_manager
Where and How should I set my global variable 'resultat' (which is a list)?
I've solved my problem but I still don't understand how does it work. Setting a variable is Kivy doesn't need to set it in the Python code. But how do Kivy and Python know about its type? Right now, it works as a list without me defining it such as (I intended it to be a list anyway).
How could I set a variable as tuple for example?