Clearing inputText with a button release in Kivy - python

Let's imagine we have a form where the person wrote some stuff on the inputs but need to clear them all, how would I do that in Kivy?
I tried this but it didn't work:
main.py:
class CreateUra(GridLayout):
def clear_inputs(self, *args):
for item in args:
item = ''
class UraApp(App):
def build(self):
return CreateUra()
if __name__ == "__main__":
UraApp().run()
Ura.kv:
<CreateUra>:
cols: 1
padding: 10
spacing: 10
row_force_default: True
row_default_height: '32dp'
BoxLayout:
Label:
text: 'Contexto:'
TextInput:
hint_text: "Contexto da ura"
focus: True
multiline: False
cursor_color: (0, 0, 0, 0.8)
id: context_input
BoxLayout:
Label:
text: 'Nome da ura:'
TextInput:
hint_text: "Nome do arquivo da ura"
multiline: False
cursor_color: (0, 0, 0, 0.8)
id: ura_file_name
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: root.clear_inputs(context_input.text, ura_file_name.text)
*Ignore the uraConfig on the on_press
I tried this way and it worked:
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: context_input.text = ''
I'm quite new to kivy and Python, so I'm not sure if this was the best way to clear the texts, but what I did wrong? Or if you guys could give some light in the best way to do it.

The problem in your case is passing the text, but not text property but a copy of the text, so even if you set it to empty is not reflected in the textinput. You have to pass the textinput as a list, iterate and set the property with an empty string:
*.kv
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: root.clear_inputs([context_input, ura_file_name]) # <-- add brackets
*.py
class CreateUra(GridLayout):
def clear_inputs(self, text_inputs):
for text_input in text_inputs:
text_input.text = ""

I tried this and it work for me:
*kv
TextInput:
id: my_text_input
hint_text: "Your name..."
size_hint: None, None
height: "50dp"
width: "100dp"
multiline: False
on_text_validate:root.on_text_validate(self)
Label:
id: txt
text: "Your name is: " + root.text_input_str
Button:
text: "Reset details"
on_press: root.on_button_reset_details()
size_hint: None, None
width: "100dp"
height: "50dp"
*py
class WidgetGalaxy(GridLayout):
text_input_str = StringProperty("")
def on_text_validate(self, widget):
self.text_input_str = widget.text
def on_button_reset_details(self):
self.text_input_str = ""
self.ids.my_text_input.text = ""
if self.ids.my_text_input.text == "":
self.ids.my_text_input.hint_text = self.ids.my_text_input.hint_text
with all that it should work for your code...
If having issue check your code preference and some predefined local or global variable.

Related

How to get the selected value from my kivy dropdown menu

I have a dropdown menu in which I would like to know which value the user has selected so I can use it later in my program. This is my .kv code:
BoxLayout:
orientation: 'horizontal'
size_hint_x: 1
Button:
pos_hint:{'center_x': .5, 'center_y': .5}
id: units_num_btn
text: '0'
size_hint_y: None
height: 44
on_parent: drop_player_numbers_units.dismiss()
on_release: drop_player_numbers_units.open(self)
DropDown:
id: drop_player_numbers_units
on_select: units_num_btn.text = '{}'.format(args[1])
on_select: app.return_player_numbers()
Button:
id: units_num_btn_1
text: '1'
size_hint_y: None
height: 35
on_release: drop_player_numbers_units.select('1')
Button:
id: units_num_btn_2
text: '2'
size_hint_y: None
height: 35
on_release: drop_player_numbers_units.select('2')
and so on.
My .py code is here:
class drop_content(DropDown):
pass
class PlayerScreen(Screen):
pass
class TheApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(PlayerScreen(name='player_setup'))
sm.current = 'player_setup'
return sm
def main():
Builder.load_file('menu.kv')
app = TheApp()
app.run()
if __name__ == '__main__':
main()
I have previously used a function such as this:
# .py example
def return_text(self):
text = self.root.get_screen('screen').ids.specific_id.text
print(text)
# .kv example
TextInput:
id: input
text: "2"
on_text: app.return_text()
which did return text using a Textinput type in my .kv file. I know it doesn't work for the dropdown menu since the text is not inputted in the same way. Do you know how I would do this?
Thanks in advance
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.lang.builder import Builder
class TestScreen(Screen):
def return_player_numbers(self,play_number):
print('Test : ',play_number)
kv = '''
ScreenManager:
TestScreen:
<TestScreen>:
BoxLayout:
orientation: 'horizontal'
size_hint_x: 1
Button:
pos_hint:{'center_x': .5, 'center_y': .5}
id: units_num_btn
text: '0'
size_hint_y: None
height: 44
on_parent: drop_player_numbers_units.dismiss()
on_release: drop_player_numbers_units.open(self)
DropDown:
id: drop_player_numbers_units
on_select:
units_num_btn.text = '{}'.format(args[1])
app.root.current_screen.return_player_numbers(args[1])
app.return_player_numbers(args[1])
Button:
id: units_num_btn_1
text: '1'
size_hint_y: None
height: 35
on_release: drop_player_numbers_units.select('1')
Button:
id: units_num_btn_2
text: '2'
size_hint_y: None
height: 35
on_release: drop_player_numbers_units.select('2')
'''
class TheApp(App):
def return_player_numbers(self,play_number):
print('App : ',play_number)
def build(self):
return Builder.load_string(kv)
if __name__ == '__main__':
TheApp().run()
I have found a way that I think is simpler than the current answer provided. This method uses that fact that the text on the button used to initiate the drop down changes as another button is selected. This is to show the user what they have selected. Since I want to know the text the user has selected from the drop down menu, and this is in fact the same thing that is updated on the initial button, I can just read the text from the button to obtatin my result like so:
(in .kv file)
<player_setup>
BoxLayout:
orientation: 'horizontal'
size_hint_x: 0.2
pos_hint: {'x':.4, 'y':0}
Button:
pos_hint:{'center_x': .5, 'center_y': .5}
id: tens_num_btn
text: '0'
size_hint_y: None
# size_hint_x: 0.1
height: 44
on_parent: drop_player_numbers_tens.dismiss()
on_release: drop_player_numbers_tens.open(self)
DropDown:
id: drop_player_numbers_tens
on_select:
#######
tens_num_btn.text = '{}'.format(args[1])
# this line here ^ is the one that re-formats the text of the button above to
# the selected text
app.return_player_numbers()
max_height: 120
(.py file)
def return_player_numbers(self):
player_number = self.root.get_screen('player_setup').ids.tens_num_btn.text
return player_number
This also allows me to concatenate multiple dropdown results using a single function, however it is menu specific. In my case, this works better for me

Updating Data from Kivy Python to Excel

I have a problem on updating data that I want to insert to excel. At first, I create the data and it work successfully without error. After I want to insert new data from Kivy, it doesn't work. Error said that the process denied by PC. I had watch tutorial and it seem my problem still unsolved. Here is my code from VSCode:
Window.size = (500, 500)
outWorkbook = xlsxwriter.Workbook("staff.xlsx")
outSheet = outWorkbook.add_worksheet()
class Menu(Screen):
pass
class Enter(Screen):
input1 = ObjectProperty(None)
input2 = ObjectProperty(None)
input3 = ObjectProperty(None)
input4 = ObjectProperty(None)
input5 = ObjectProperty(None)
input6 = ObjectProperty(None)
def clear(self):
self.ids.inherent_input.text = ''
self.ids.inherent2_input.text = ''
self.ids.inherent3_input.text = ''
self.ids.inherent4_input.text = ''
self.ids.inherent5_input.text = ''
self.ids.inherent6_input.text = ''
def btn(self):
self.L = ()
print("Name: " + self.input1.text,
"Activity/Programme: " + self.input2.text,
"Date: " + self.input3.text,
"Place: " + self.input4.text,
"Time from: " + self.input5.text,
"Time to: " + self.input6.text)
staff = ({"Name": [str(self.input1.text)], "Program/Activity": [str(self.input2.text)], "Place" : [str(self.input3.text)], "Date": [str(self.input4.text)], "Time From" : [str(self.input5.text)], "Time To" : [str(self.input6.text)]})
self.L = pd.DataFrame(staff)
self.input1.text = ''
self.input2.text = ''
self.input3.text = ''
self.input4.text = ''
self.input5.text = ''
self.input6.text = ''
print(self.L)
with pd.ExcelWriter('staff.xlsx') as writer:
self.L.to_excel(writer)
class Info(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('window.kv')
class MyLayout(Widget):
class WindowApp(App):
def build(self):
return kv
if name == 'main':
WindowApp().run()
And here is my kv file:
WindowManager:
Menu:
Enter:
Info:
:
name: "MainMenu"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding: 10
spacing: 15
Button:
id: enter
text: "Enter"
on_release: app.root.current = "enter"
Button:
id: info
text: "Information"
on_release: app.root.current = "info"
Button:
id: exit
text: "Exit"
on_press: quit()
:
name: "enter"
input1: inherent_input
input2: inherent2_input
input3: inherent3_input
input4: inherent4_input
input5: inherent5_input
input6: inherent6_input
BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding:10
spacing:10
BoxLayout:
spacing: 10
Label:
text: "Name"
font_size: 20
TextInput:
id: inherent_input
multiline: False
BoxLayout:
spacing: 10
Label:
text: "Activity/Programme"
font_size: 20
TextInput:
id: inherent2_input
multiline: False
BoxLayout:
spacing: 10
Label:
text: "Place"
font_size: 20
TextInput:
id: inherent3_input
multiline: False
BoxLayout:
Label:
text: "Date"
font_size: 20
TextInput:
id: inherent4_input
multiline: False
BoxLayout:
padding: 10
spacing: 10
Label:
text: "Time from"
font_size: 20
TextInput:
id: inherent5_input
multiline: False
Label:
text: "to"
font_size: 20
TextInput:
id: inherent6_input
multiline: False
BoxLayout:
padding: 15
spacing: 15
Button:
id: clear
text: "Clear"
font_size: 12
on_press: root.clear()
Button:
id: back
text: "Back to Menu"
font_size: 12
on_release: app.root.current = "MainMenu"
Button:
id: submit
text: "Submit"
font_size: 12
on_press: root.btn()
:
name: "info"
BoxLayout:
orientation: 'vertical'
padding: 15
spacing: 15
Label:
id: information
text: "This is just a test, an Alpha version of Prototype"
font_size: 20
bold: True
italic: False
outline_color: (0,0,0)
Button:
id: returnBack
text: "Return"
on_release: app.root.current = "MainMenu"
Any help will be appreciated.
The error you're getting can mean multiple things.
Are you trying to open a directory?
If so, don't do it. It will most likely result in the error you mentioned.
Is the file you are trying to opened anywhere else?
You may have to close it, for python to open it. You probably have experienced this before when trying to delete a file in the explorer, that is opened somewhere else. Windows won't let you do this. Check whether the file in question is opened before trying to access it.

python kivy - on_press being executed twice

Newbie to Python - I am writing a raspberry pi app with touch screen. Manage to make it work but the Increment function is being executed twice when pressing the + button (same for decrement). These buttons are enabled only while another button is pressed but this is not the problem.
Any help please?
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
Window.show_cursor = False
import serial
import time
port = serial.Serial("/dev/ttyS0", baudrate=19200, timeout=3.0)
btnNum = '0'
b1Tmp = 0; b2Tmp = 0; b3Tmp = 0; b4Tmp = 0; b5Tmp = 0; b6Tmp = 0; b7Tmp = 0; b8Tmp = 0
b1Max =50; b2Max =50; b3Max =50; b4Max =50; b5Max =50; b6Max =50; b7Max =50; b8Max =50;
class Container(Widget):
labR1= ObjectProperty(None)
labR2= ObjectProperty(None)
labR3= ObjectProperty(None)
labR4= ObjectProperty(None)
labR5= ObjectProperty(None)
labR6= ObjectProperty(None)
labR7= ObjectProperty(None)
labR8= ObjectProperty(None)
butUP= ObjectProperty(None)
butDN= ObjectProperty(None)
labM1= ObjectProperty(None)
labM2= ObjectProperty(None)
labM3= ObjectProperty(None)
labM4= ObjectProperty(None)
labM5= ObjectProperty(None)
labM6= ObjectProperty(None)
labM7= ObjectProperty(None)
labM8= ObjectProperty(None)
def bpressed(self,instance):
global btnNum
btnNum = instance.text
self.butDN.disabled = False
self.butUP.disabled = False
def breleased(self,instance):
global btnNum
btnNum = '0'
self.butDN.disabled = True
self.butUP.disabled = True
def increment(self,instance):
global btnNum,b1Max,b2Max,b3Max,b4Max,b5Max,b6Max,b7Max,b8Max
if (btnNum == '1'):
if (b1Max<100): b1Max+=2
self.labM1.text = str(b1Max)
if (btnNum == '2'):
if (b2Max<100): b2Max=b2Max+1
self.labM2.text = str(b2Max)
if (btnNum=='3'):
if (b3Max<100): b3Max+=1
self.labM3.text = str(b3Max)
if (btnNum=='4'):
if (b4Max<100): b4Max+=1
self.labM4.text = str(b4Max)
if (btnNum=='5'):
if (b5Max<100): b5Max+=1
self.labM5.text = str(b5Max)
if (btnNum=='6'):
if (b6Max<100): b6Max+=1
self.labM6.text = str(b6Max)
if (btnNum=='7'):
if (b7Max<100): b7Max+=1
self.labM7.text = str(b7Max)
if (btnNum=='8'):
if (b8Max<100): b8Max+=1
self.labM8.text = str(b8Max)
def decrement(self,instance):
global btnNum,b1Max,b2Max,b3Max,b4Max,b5Max,b6Max,b7Max,b8Max
if (btnNum == '1'):
if (b1Max>20): b1Max-=1
self.labM1.text = str(b1Max)
if (btnNum == '2'):
if (b2Max>20): b2Max-=1
self.labM2.text = str(b2Max)
if (btnNum=='3'):
if (b3Max>20): b3Max-=1
self.labM3.text = str(b3Max)
if (btnNum=='4'):
if (b4Max>20): b4Max-=1
self.labM4.text = str(b4Max)
if (btnNum=='5'):
if (b5Max>20): b5Max-=1
self.labM5.text = str(b5Max)
if (btnNum=='6'):
if (b6Max>20): b6Max-=1
self.labM6.text = str(b6Max)
if (btnNum=='7'):
if (b7Max>20): b7Max-=1
self.labM7.text = str(b7Max)
if (btnNum=='8'):
if (b8Max>20): b8Max-=1
self.labM8.text = str(b8Max)
def serialsend(self,instance):
port.write("\r\nSay something:".encode())
def close(self , obj):
App.get_running_app().stop()
Window.close()
class BoilerApp(App):
def build(self):
self.title = 'Boiler Monitor'
return Container()
if __name__ == "__main__":
from kivy.core.window import Window
Window.fullscreen = True
app = BoilerApp()
app.run()
kv file
<MyButton#Button>:
color: .8,.9,0,1
font_size: 30
<MyLabel#Label>:
color: .8,.9,0,1
font_size: 30
<Container>
labR1: labR1
labR2: labR2
labR3: labR3
labR4: labR4
labR5: labR5
labR6: labR6
labR7: labR7
labR8: labR8
butUP: butUP
butDN: butDN
labM1: labM1
labM2: labM2
labM3: labM3
labM4: labM4
labM5: labM5
labM6: labM6
labM7: labM7
labM8: labM8
GridLayout:
cols: 1
size: root.width, root.height
Label:
text: "Boiler Alarm"
size_hint: .5, .2
font_size: 40
background_color: 1,0,0,1
GridLayout:
cols:10
MyLabel:
text: "Boiler"
MyButton:
id: butB1
text: "1"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB2
text: "2"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB3
text: "3"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB4
text: "4"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB5
text: "5"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB6
text: "6"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB7
text: "7"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB8
text: "8"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyLabel:
text: ""
Label:
text: "Reading"
font_size: 25
MyLabel:
id: labR1
text: "0"
MyLabel:
id: labR2
text: "0"
MyLabel:
id: labR3
text: "0"
MyLabel:
id: labR4
text: "0"
MyLabel:
id: labR5
text: "0"
MyLabel:
id: labR6
text: "0"
MyLabel:
id: labR7
text: "0"
MyLabel:
id: labR8
text: "0"
MyButton:
id: butUP
text: "+"
disabled: True
on_press: root.increment(self)
Label:
text: "Minimum"
font_size: 25
MyLabel:
id: labM1
text: "50"
MyLabel:
id: labM2
text: "50"
MyLabel:
id: labM3
text: "50"
MyLabel:
id: labM4
text: "50"
MyLabel:
id: labM5
text: "50"
MyLabel:
id: labM6
text: "50"
MyLabel:
id: labM7
text: "50"
MyLabel:
id: labM8
text: "50"
MyButton:
id: butDN
text: "-"
disabled: True
on_release: root.decrement(self)
Button:
id: btnexit
text: "Exit"
size_hint: .5, .1
on_press: root.close(self)
I had the same problem and found multiple workarounds after fighting this for a while.
The issue seems to be that kivy interprets both the mouse and the touch events simultaneously. This means when you touch the display both a "mouse" click and a "touch" press event is fired, resulting in double presses.
Solution 1 – change the kivy config.ini
The easiest fix is to edit the [input]-section in ~/.kivy/config.ini in su a way:
[input]
%(name)s = probesysfs,provider=hidinput
mouse = mouse
# mtdev_%(name)s = probesysfs,provider=mtdev
hid_%(name)s = probesysfs,provider=hidinput
Just comment out the line starting with mtdev_ and restart the program.
Solution 2 – manually check the event-kind
If you want to use the mouse somewhere still, there is a different way to do this. Let's assume you create a button somewhere like this:
button = ToggleButton(text="Cool Button")
button.bind(on_release=on_toggle)
self.add_widget(button)
You can then check whether our on_toggle-function is activated by a mouse- or a touch-event as follows:
from kivy.input.providers.mouse import MouseMotionEvent
def on_toggle(button_instance):
if isinstance(button_instance.last_touch, MouseMotionEvent):
print(f"Last Touch was from the Mouse {button_instance.last_touch}")
You should not use timeout with kivy. You should instead use the Clock function. The timeout function would halt all your program which may lead to these issues. Whereas Clock schedules specific events without halting the program altogether.

errors in results after launching program

this is my 1st time posting here and am so amazed how genius people here are.
i have written a python code using kivy and it works perfectly on my computer even on kivy launcher for android, but the problem is that the result on my phone is always ignoring the float numbers and show just the 1s one.
thanks in advance
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.config import Config
import time
Builder.load_string("""
<Main>:
GridLayout:
cols:2
Button:
text: "Engine Consumption"
on_press: root.manager.current = "test1"
Button:
text: "Apu Consumption"
on_press: root.manager.current = "test2"
<Test1>
GridLayout:
cols: 2
Label:
text: "Flight Hours: "
TextInput:
id: FH
multiline: False
Label:
text: "Flight Minutes:"
TextInput:
id: FM
multiline: False
Label:
text: "Added Quantity:"
TextInput:
id: AQ
multiline: False
Button:
text: "get result"
on_press: root.get_result()
Label:
id: result
Button:
text: "To Main Page"
on_release: root.manager.current = "main"
Button:
text: "Apu Consumption:"
on_release: root.manager.current = "test2"
<Test2>
GridLayout:
cols: 2
Label:
text: "Apu hours In Last Refill: "
TextInput:
id: FH
multiline: False
Label:
text: "Current Apu Hours:"
TextInput:
id: FM
multiline: False
Label:
text: "Added Quantity:"
TextInput:
id: AQ
multiline: False
Button:
text: "get result"
on_press: root.get_result()
Label:
id: result
Button:
text: "To main Page"
on_release: root.manager.current = "main"
Button:
text: "Engine Consumption"
on_release: root.manager.current = "test1"
""")
class Main(Screen):
pass
class Test1(Screen):
def get_result(self):
fh = self.ids.FH.text
fm = self.ids.FM.text
ad = self.ids.AQ.text
result = int(ad) / (int(fh) + int(fm) / 60)
self.ids.result.text = str(result)
self.ids.result.color = 1,0,1,1
class Test2(Screen):
def get_result(self):
fh = self.ids.FH.text
fm = self.ids.FM.text
ad = self.ids.AQ.text
result = int(ad) / (int(fm) - int(fh))
self.ids.result.text = "the result is: " + str(float(result))
class Test(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Main(name = "main"))
sm.add_widget(Test1(name = "test1"))
sm.add_widget(Test2(name = "test2"))
return sm
Config.set("graphics", "height", "150")
if __name__ == "__main__":
Test().run()
on computer result is 0,4444
and on phone 0

Button to print the the Text Input is not working

The whole code is working well. But when u go to:
student > Add New student > > Fill all columns of new student > then submit
it's not working and I can't figure out the issue. Here is the following code. Any help will be appreciated
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen ,FadeTransition
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
import csv
from kivy.uix.textinput import TextInput
Builder.load_string("""
<MenuScreen>:
BoxLayout:
Button:
text: 'Teacher'
on_press: root.manager.current = 'screen1'
Button:
text: 'Student '
on_press:root.manager.current = 'screen2'
Button:
text: 'Quit'
<Screen1>:
BoxLayout:
Button:
text: 'Teacher Info'
#on_press:root.manager.current = 'login'
Button:
text: 'Teacher Attandance'
Button:
text: 'Add New Teacher'
on_press:root.manager.current = 'add_teacher'
Button:
text: 'Back'
on_press:root.manager.current ='menu'
<add_new_teacher>:
GridLayout:
cols:2
Label:
text:'Name'
TextInput:
id: name_input
multiline: False
Label:
text:'Father Name'
TextInput:
id: name_input
multiline: False
Label:
text: 'Mother Name'
TextInput:
id: name_input
multiline: False
Label:
text: 'Class'
TextInput:
id: name_input
multine: False
Label:
text:'Roll no.'
text: 'Student Info'
on_press:root.csv_std()
Button:
text: 'Student Attandance'
# on_press:root.manager.current ='login'
Button:
text: 'Add New Student'
on_press:root.manager.current = 'add_student'
Button
text: 'Back'
on_press:root.manager.current = 'menu'
<add_new_student>:
GridLayout:
cols:2
Label:
text:'Name'
TextInput:
id: self.name
multiline: False
Label:
text:'Father Name'
TextInput:
id: self.fname
multiline: False
Label:
text: 'Mother Name'
TextInput:
id: self.mname
multiline: False
Label:
text: 'Class'
TextInput:
id: self.c
multine: False
Label:
text:'Roll no.'
TextInput:
id: self.r
multiline:False
Button:
text:'Print'
Button:
text:'Submit'
on_press:root.print_text()
Button:
text:'Back'
on_press:root.manager.current= 'screen2'
""")
# Declare both screens
class MenuScreen(Screen):
pass
class add_new_teacher(Screen):
pass
class Screen1(Screen):
pass
class Screen2(Screen):
def csv_std(self):
f = open("a.csv", 'r')
reader = csv.reader(f)
for row in reader:
print(" ".join(row))
pass
class add_new_student(Screen):
def print_text(self):
for child in reversed(self.children):
if isinstance(child, TextInput):
print child.text
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(add_new_teacher(name='add_teacher'))
sm.add_widget(add_new_student(name='add_student'))
sm.add_widget(Screen1(name='screen1'))
sm.add_widget(Screen2(name='screen2'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
You code formatting was horrible, but at least you didn't use backticks. For future cases, copy&paste your whole example you want to show here, then select that example(whole) and press Ctrl + K, which will indent all selected lines, so that it'd look ok.
The code works exactly how it supposed to work, because root.print_text() targets add_new_student class and its children - not GridLayout which you want to access.
Edit the line with for to this: for child in reversed(self.children[0].children): and you are good to go. :)
Or more efficient solution would be to get that Screen to behave as a layout too, which you can get with inheritting both from Screen and some layout, but ensure the layout is first:
class add_new_student(GridLayout, Screen):
def print_text(self):
for child in reversed(self.children):
if isinstance(child, TextInput):
print child.text
kv:
<add_new_student>:
cols:2
Label:
text:'Name'

Categories