errors in results after launching program - python

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

Related

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.

How do i add a signature/paint method to a screen in my app in either kv lang or normal kivy?

how do i add buttons to my paintScreen class without the on_touch_down/move interfering and making it so that i draw within a BoxLayout canvas and separate the buttons in a different box so that it wont draw over the buttons? This is the current problem i am having. Also if that problem is fixed then would i be able to save that drawing as an img if i use export_to_png
from kivy.app import App
from kivy.graphics.context_instructions import Color
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner
from kivy.uix.checkbox import CheckBox
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Line, Rectangle, Ellipse, Triangle
Builder.load_string("""
<firstScreen>
BoxLayout:
orientation: "vertical"
ScrollView:
size: root.size
GridLayout:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height #<<<<<<<<<<<<<<<<<<<<
row_default_height: 90
cols: 2
Label:
text: "Name: "
TextInput:
multiline: False
Label:
text: "Address: "
TextInput:
multiline: False
Label:
text: "Contact: "
TextInput:
multiline: False
Label:
text: "Telephone: "
TextInput:
multiline: False
Label:
text: "Order Number: "
TextInput:
multiline: False
Label:
text: "Transporter's Name: "
TextInput:
multiline: False
Label:
text: "Vehicle Reg. No.: "
TextInput:
multiline: False
Label:
text: "Driver's Name: "
TextInput:
multiline: False
Label:
text:"Hazchem Requirements: "
Spinner:
id: spinner_1
text: "< Select >"
values: root.pick_opt
Spinner:
id: spinner_2
text: "Waste Details"
values: root.pick_classification
GridLayout:
size_hint: 1, None
rows:2
Label:
text: "Non-Hazardous"
Label:
text: "Hazardous"
CheckBox:
on_active: root.chk_tick
CheckBox:
on_active: root.chk_tick
Spinner:
id: spinner_3
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Spinner:
id: spinner_4
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Spinner:
id: spinner_5
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Spinner:
id: spinner_6
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Button:
text: "Next Screen"
size_hint: 1,.1
on_press: root.manager.current = "screen_2"
<secondScreen>
BoxLayout:
orientation: "horizontal"
ScrollView:
size: root.size
GridLayout:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height #<<<<<<<<<<<<<<<<<<<<
row_default_height: 120
cols: 1
BoxLayout:
orientation: 'vertical'
Spinner:
id: spinner_7
text: "details"
width: self.parent.width * 2
values: root.pick_dets
Spinner:
id: spinner_8
text: "details"
values: root.pick_dets
Spinner:
id: spinner_9
text: "details"
values: root.pick_dets
GridLayout:
cols: 2
rows: 3
Label:
text: "Start Time"
Label:
text: "End Time"
TextInput:
multiline: False
TextInput:
multiline: False
GridLayout:
cols: 2
Label:
text: 'Special Instructions/Remarks'
TextInput:
hint_text: 'Enter Remarks'
Button:
id: signature
text: 'Signature'
on_press: root.manager.current = "screen_signature"
BoxLayout:
orientation: 'vertical'
Button:
text: "Previous Screen"
size_hint: 1,.1
on_press: root.manager.current = "screen_1"
Button:
text: "Next Screen"
size_hint: 1,.1
on_press: root.manager.current = "screen_2"
<paintScreen>
""")
class paintScreen(Screen):
# On mouse press how Paint_brush behave
def on_touch_down(self, touch):
color = (1, 1, 1)
with self.canvas:
Color(*color, mode='hsv')
d = 30.
Line(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
touch.ud['line'] = Line(points=(touch.x, touch.y))
# On mouse movement how Paint_brush behave
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
pass
class firstScreen(Screen):
def __init__(self, **kwargs):
self.pick_opt = ["yes", "no"]
self.pick_classification = ["Classification (SANAS 10228)", "HR (1,2,3,4 - Minimum Requirements)",
"Emergency Response Guide Reference"]
self.sog = ['Oil', "Effluent", "Sludge", "Other"]
super(firstScreen, self).__init__(**kwargs)
def chk_tick(self, instance, value):
if value:
print("ticked")
else:
print('unticked')
pass
class secondScreen(Screen):
def __init__(self, **kwargs):
self.pick_dets = ["HP Cleaning", "Chemicals", "Soil Treatment",
"Minor Civils & Plumbing", "Effluent Treatment", "Other"]
super(secondScreen, self).__init__(**kwargs)
pass
class MyScreenManager(ScreenManager, RecycleView):
pass
screen_manager: ScreenManager = ScreenManager()
screen_manager.add_widget(firstScreen(name="screen_1"))
screen_manager.add_widget(secondScreen(name="screen_2"))
screen_manager.add_widget(paintScreen(name="screen_signature"))
class Myapp(App):
def build(self):
return screen_manager
def clear_canvas(self, obj):
self.painter.canvas.clear()
if __name__ == "__main__":
Myapp().run()

adding two Values in two kivy screens and getting the result

I'm trying to build an app that can calculate the sum of two values. I have a screen called Therdwindow that has three text input widgets.
from kivy.app import App
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen`
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class Therdwindow(Screen):
pass
class Fourwindow(Screen):
pass
class FunfthWindow(Screen):
def calculate3(self,bibi,mimi):
kiki = str(float(bibi) + float(mimi))
if kiki:
try :
self.result_1.text = str(eval(kiki))
except Exception:
self.result_1.text = 'Error'
class Sixwindow(Screen):
pass
class WindowManager(ScreenManager):
psss
kv = Builder.load_file("layout.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
A person is supposed to enter the Password 'gin' than click on a button 'NEXT' to go to the next screen named SecondWindow , then click on a button 'NEXT' to go to the next screen named TherdWindow ,
Then enter a First Value in the Box then click on a button 'NEXT' to go to the next screen named fourWindow ,
than enter the second Value and click on a button 'NEXT' to go to the next screen named funfthWindow .
there should have the 'Result' if he click on a button result. In this screen there is a Label that should print the volume of the sum that the person specified.
layout.kv
<CustButton#Button>:
font_size: 40
WindowManager:
MainWindow:
SecondWindow:
Therdwindow:
Fourwindow:
FunfthWindow:
Sixwindow:
<MainWindow>:
name: "main"
<MainWindow>:
name: "main"
GridLayout:
cols:1
GridLayout:
cols: 2
orientation: 'vertical'
Label:
text: "Password: "
font_size: "40sp"
background_color: (1,1,0,1)
font_name: 'RobotoMono-Regular.ttf'
TextInput:
id: passw
multiline: False
font_size: "40sp"
CustButton:
text: "Submit"
background_color: (0.8,0.8,0,1)
on_press:
app.root.current = "second" if passw.text == "gin" else "six"
root.manager.transition.duration = 1
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
GridLayout:
cols: 1
spacing: 10
CustButton:
text: "Go Back"
on_press:
app.root.current = "main"
root.manager.transition.direction = "right"
CustButton:
text: "next"
on_press:
app.root.current = "therd"
root.manager.transition.direction = "left"
<Therdwindow>:
id:lulu
name: "therd"
nani:feras1
rows: 20
padding: 0
spacing: 2
GridLayout:
spacing: 10
cols:2
Label:
text: "Enter The First Value : "
font_size: "30sp"
TextInput:
id:first_Value
font_size: 40
multiline: True
CustButton:
text: "go back"
on_press:
app.root.current = "second"
root.manager.transition.direction = "right"
CustButton:
text: "Next"
on_press:
app.root.current = "four"
root.manager.transition.direction = "left"
<Fourwindow>:
id:lala
name: "four"
nani21:feras2
rows: 20
padding: 0
spacing: 2
GridLayout:
spacing: 10
cols:2
Label:
text: "Enter The second Value : "
font_size: "30sp"
TextInput:
id: second_Value
font_size: 40
multiline: True
CustButton:
text: "go back"
on_press:
app.root.current = "therd"
root.manager.transition.direction = "right"
CustButton:
text: "NEXT"
on_press:
app.root.current = "funfth"
root.manager.transition.direction = "left"
<FunfthWindow>:
id:CalcmGridLayout
name: "funfth"
result_1:label_id
rows: 20
padding: 0
spacing: 2
GridLayout:
spacing: 10
cols:2
CustButton:
text: "Result : "
font_size: "30sp"
on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)
Label:
id: label_id
font_size: 40
multiline: True
CustButton:
text: "go back"
on_press:
app.root.current = "four"
root.manager.transition.direction = "right"
CustButton:
text: "NEXT"
on_press:
app.root.current = "main"
root.manager.transition.direction = "left"
<Sixwindow>:
name: "six"
GridLayout:
cols: 1
spacing: 10
Label:
text: 'das Password ist falsch'
font_size: "40sp"
CustButton:
text: "nochmal"
on_press:
app.root.current = "main"
root.manager.transition.direction = "right"
When I click of 'Result' I get this Error NameError : first_Value is not defined
please help. I would really appreciate any advice.
Well, there's a lot of ways to do that, for the simplicity I'll do all operations in the MyMainApp class:
from kivy.properties import ObjectProperty
from Kivy.clock import Clock
...
class Therdwindow(Screen):
first_Value = ObjectProperty(None)
def getvalue(self):
return self.first_Value
class Fourwindow(Screen):
second_Value = ObjectProperty(None)
def getvalue(self):
return self.second_Value
class FunfthWindow(Screen):
label_id = ObjectProperty(None)
def getlabel(self):
return self.label_id
class WindowManager(ScreenManager):
pass
class MyMainApp(App):
def build(self):
with open('layout.kv', encoding='utf-8', errors='ignore') as f:
Builder.load_string(f.read())
# creating objects of screens
self.mainwindow = MainWindow()
self.secondwindow = SecondWindow()
self.therdwindow = Therdwindow()
self.fourwindow = Fourwindow()
self.funfthwindow = FunthWindow()
# creating object of screen manager
self.sm = WindowManager()
# getting all object properties
self.first_Value = self.therdwindow.getvalue()
self.second_Value = self.fourwindow.getvalue()
self.label_id = self.funfthwindow.getlabel()
# connecting object properties to widgets from kv file
# sometimes we need to delay that, because the app can't load so quickly, so let's make a method for it
Clock.schedule_once(self.getids)
# adding screens to screen manager
self.sm.add_widget(self.mainwindow)
self.sm.add_widget(self.secondwindow)
self.sm.add_widget(self.therdwindow)
self.sm.add_widget(self.fourwindow)
self.sm.add_widget(self.funfthwindow)
# in case you want screen manager be able to work you should return it
return self.sm
def getids(self):
self.first_Value = self.therdwindow.ids.first_Value
self.second_Value = self.fourwindow.ids.second_Value
self.label_id = self.funfthwindow.ids.label_id
# method that does what you want
def count(self):
self.label_id.text = str(int(self.first_Value.text) + int(self.second_Value.text))
if __name__ == "__main__":
MyMainApp().run()
And you need to edit your kv file, change all that
on_press:
app.root.current = "second"
to that:
on_press:
app.sm.current = "second"
And change this:
CustButton:
text: "Result : "
font_size: "30sp"
on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)
to this:
CustButton:
text: "Result : "
font_size: "30sp"
on_press: app.count
Also you need to add that lines in classes:
<Therdwindow>:
...
first_Value: first_Value.__self__
<Fourwindow>:
...
second_Value: second_Value.__self__
<FunfthWindow>:
...
label_id: label_id.__self__
And delete this:
WindowManager:
MainWindow:
SecondWindow:
Therdwindow:
Fourwindow:
FunfthWindow:
Sixwindow:
You are trying to use ids from one rule inside another rule, but ids are only available for the current rule. So, this line in your kv will not work:
on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)
A way to fix this, is to access the ids by first getting the Screen where that id is defined, and that is more easily done outside of kv. So, I would recommend replacing the above line with:
on_press:root.calculate3()
And then rewrite your calculate3() method slightly to access the other values:
class FunfthWindow(Screen):
def calculate3(self):
bibi = App.get_running_app().root.get_screen('therd').ids.first_Value.text
mimi = App.get_running_app().root.get_screen('four').ids.second_Value.text
kiki = str(float(bibi) + float(mimi))
if kiki:
try :
self.ids.label_id.text = str(eval(kiki))
except Exception:
self.ids.label_id.text = 'Error'

Python Kivy TextInput not working?

I have created a simple login and registration screen using kivy, but the TextInput do not let me type inside of it? This error is occuring on both the LoginScreen and the RegistrationScreen. I tried looking up past questions on here, but i couldn't find anything.
The Python file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class Login_Screen(Screen):
def login(self):
if self.ids.username.text == "root" and\
self.ids.passwrd.text == "123":
print("Direct Entry")
self.manager.current = "Login_Successful"
else:
print("Wrong Password")
self.manager.current = "Login_Failed"
def registration(self):
self.manager.current = "Registration_Screen"
class LoginConfirmationScreen(Screen):
pass
class Registration_Screen(Screen):
def MainScreen(self):
self.manager.current = "Login_Screen"
class Login_Failed(Screen):
def MainScreen(self):
self.manager.current = "Login_Screen"
class RootWidget(ScreenManager):
pass
Builder.load_file("MainApp.kv")
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
MainApp().run()
And the MainApp.kv file:
<RootWidget>:
id: Main
Login_Screen:
id: login
name: "Login_Screen"
Login_Failed:
id: Failed
name: "Login_Failed"
Registration_Screen:
id: register
name: 'Registration_Screen'
<Login_Screen>:
GridLayout:
rows:3
cols:2
Label:
text: "Username:"
font_size: 20
TextInput:
id: username
multiline: False
hint_text: 'Enter your Username'
Label:
text: "Password"
font_size: 20
TextInput:
id: passwrd
multiline: False
hint_text: 'Enter your Password'
password: True
Button:
text: "Register"
on_press: root.registration()
Button:
text: "Sign In"
on_press: root.login()
<Registration_Screen>:
GridLayout:
rows:3
cols:2
Label:
text: 'First Name:'
font_size: 20
TextInput:
id: FirstName
multiline: False
hint_text: 'Enter your First Name'
Label:
text: 'Surname'
font_size: 20
TextInput:
id: Surname
multiline: False
hint_text: 'Enter your Surname'
Button:
text: "Create Account"
on_press: root.MainScreen()
<Login_Failed>:
BoxLayout:
orientation: "vertical"
Label:
text: "Login Failed"
Button:
text: "Try again"
on_press: root.MainScreen()
Thanks for help :)
Remove this:
Builder.load_file("MainApp.kv")
from your py file because you're App instance, ie:
class MainApp(App):
Loads it automatically already.

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