I want to clear a TextInput's text: when I click on it.
Sample Code:
from kivy.app import App
from kivy.lang import Builder
kv_string = """
ScreenManager:
id: manager
Screen:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
TextInput:
text: 'Write Your Name'
on_touch_down:
self.text = ''
TextInput:
text: 'Write Your Last Name'
on_focus:
self.text = ''
TextInput:
text: 'Write Your Phone Number'
on_touch_down:
self.text = ''
"""
class MyApp(App):
def build(self):
root_widget = Builder.load_string(kv_string)
return root_widget
if __name__ == "__main__":
MyApp().run()
Neither on_touch_down: or on_focus erases JUST the text input that is currently focused. Instead, both get cleared when I touch anywhere on the screen. I would want them cleared individually once the cursor is on a text input. I also tried on_cursor but that didn't work either. What am I missing? Thank you in advance!
The on_touch_down event is received by all the widgets until one returns True telling the event-loop that it is using it and thus not send it to other widgets as indicated by the docs:
on_touch_down(touch) Added in 1.0.0
Receive a touch down event.
Parameters:
touch: MotionEvent class Touch received.
The touch is in parent coordinates. See relativelayout for a discussion on coordinate
systems.
Returns:
bool If True, the dispatching of the touch event will stop.
If False, the event will continue to be dispatched to the rest of the
widget tree.
The classic use of on_touch_down is in python since kv language is limited in the overwriting of methods:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
class MyTextInput(TextInput):
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.text = ""
return True
return super(MyTextInput, self).on_touch_down(touch)
kv_string = """
ScreenManager:
id: manager
Screen:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
MyTextInput:
text: 'Write Your Name'
MyTextInput:
text: 'Write Your Last Name'
MyTextInput:
text: 'Write Your Phone Number'
"""
class MyApp(App):
def build(self):
root_widget = Builder.load_string(kv_string)
return root_widget
if __name__ == "__main__":
MyApp().run()
Or something equivalent in .kv but the devestaja is that you can not return True.
kv_string = """
ScreenManager:
id: manager
Screen:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
TextInput:
text: 'Write Your Name'
on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
TextInput:
text: 'Write Your Last Name'
on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
TextInput:
text: 'Write Your Phone Number'
on_touch_down: if self.collide_point(*args[1].pos): self.text = ""
"""
So you should use on_focus which is an event associated to FocusBehavior that overwrites on_touch_down verifying using self.collide_point(*touch.pos).
from kivy.app import App
from kivy.lang import Builder
kv_string = """
ScreenManager:
id: manager
Screen:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
TextInput:
text: 'Write Your Name'
on_focus: self.text = ""
TextInput:
text: 'Write Your Last Name'
on_focus: self.text = ""
TextInput:
text: 'Write Your Phone Number'
on_focus: self.text = ""
"""
class MyApp(App):
def build(self):
root_widget = Builder.load_string(kv_string)
return root_widget
if __name__ == "__main__":
MyApp().run()
All you need to do is to just add this:
on_focus: self.text = '' if args[1] else self.text
on_focus is a function that gets called when the TextInput is selected or unselected, and the function gives you two arguments instance which is the thing got selected or unselected and value which is if that instance got selected or unselected, these two arguments get put in a list called args, and since we're doing this in the widget itself in the kv file we don't have to worry about instance, so we check if the value is True, and if it is that means that the TextInput got clicked, so we clear it, otherwise we set it to itself.
So this is how the script would look like:
from kivy.app import App
from kivy.lang import Builder
kv_string = """
ScreenManager:
id: manager
Screen:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Why does it clear multiple inputs? And why do they get cleared after touch_up?'
TextInput:
text: 'Write Your Name'
on_focus: self.text = '' if args[1] else self.text
TextInput:
text: 'Write Your Last Name'
on_focus: self.text = '' if args[1] else self.text
TextInput:
text: 'Write Your Phone Number'
on_focus: self.text = '' if args[1] else self.text
"""
class MyApp(App):
def build(self):
root_widget = Builder.load_string(kv_string)
return root_widget
if __name__ == "__main__":
MyApp().run()
Related
in my login system I want to create a popup box that will be displayed when there is an error in the username, password, or verification password. Can someone help me in how to make the popup show with different labels everytime an error happens
here is the code for main file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class LoginWindow(Screen):
def signIn(self):
pass
def signUp(self):
# db=open('QuizData.txt','r')
username=self.ids.uname.text
password=self.ids.passw.text
repassword=self.ids.repassw.text
if password!=repassword:
pass #i want a popup box to appear if this is true
def popup(self):
pass
class QuizWindow(Screen):
pass
class QuizScreenManager(ScreenManager):
pass
kv = Builder.load_file('loginLayoutCSS.kv')
class Login(App):
def build(self):
return kv
if __name__=='__main__':
Login().run()
here is the code for .kv file
#:kivy 2.1.0
#:import utils kivy.utils
#:import Factory kivy.factory.Factory
<MyPopup#Popup>
auto_dismiss: True
size_hint:0.6,0.3
pos_hint:{'center_x':0.5}
title: 'Error'
Button:
text:'close'
fonr_size: 20
on_release: root.dismiss()
<MyTextInput#TextInput>
font_size:25
# size_
background_normal:''
background_color: utils.get_color_from_hex('#8c8c8c')
QuizScreenManager:
LoginWindow:
QuizWindow:
<LoginWindow>:
name:'login'
BoxLayout:
orientation:'vertical'
size: root.width,root.height
spacing: 20
padding: 20
Label:
id:heading
text:'Welcome back, sign in'
font_size:32
MyTextInput:
id:uname
text:'username'
MyTextInput:
id:passw
text:'password'
MyTextInput:
id:repassw
text:'re-enter Password'
multiline:False
size_hint_y: None
height:0
opacity:0
Button:
text:'Sign In'
id:signin
size_hint_y:0.6
on_release:
app.root.current='quizwindow'
root.manager.transition.direction='up'
Button:
text:'New here? Sign up Now! click here'
id:signup
on_press:
# signin.size_hint_y = None
repassw.size_hint_y = 0.6
repassw.opacity = 1
repassw.background_color = utils.get_color_from_hex('#8c8c8c')
root.signUp()
root.popup()
signup.text = 'Sign Up'
signin.opacity = 0
<QuizWindow>:
name:'quizwindow'
Button:
text:'go back'
on_release:
app.root.current='login'
root.manager.transition.direction='down'
You can add an id to the MyPopup class to allow you to access a Label that can contain your message. Here is a modified version of that portion of your kv file:
<MyPopup#Popup>
auto_dismiss: True
size_hint:0.6,0.3
pos_hint:{'center_x':0.5}
title: 'Error'
BoxLayout:
orientation: 'vertical'
Label:
id: message
Button:
text:'close'
fonr_size: 20
size_hint: None, None
size: self.texture_size
on_release: root.dismiss()
Then you can access that Label in your python code like this:
def signUp(self):
# db=open('QuizData.txt','r')
username = self.ids.uname.text
password = self.ids.passw.text
repassword = self.ids.repassw.text
if password != repassword:
popup = Factory.MyPopup()
popup.ids.message.text = 'passwords do not match'
popup.open()
There are multiple screens in my kivy app:
1. main screen, screen1, screen2, screen3 and screen4.
main screen has main-dropdown and sub-dropdown list. main-dropdown shows groups and on selecting a particular group, sub-dropdown shows screens associated with that group.
I want to navigate to a particular screen when selected from sub-dropdown list. My code doesn't show any error but I am not able to navigate to the selected screen. Please help.
doca.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ListProperty, DictProperty
class windowManager(ScreenManager):
pass
class MainScreen(Screen):
values_dict = {'Group1':['Screen1', 'Screen2'],
'Group2':['Screen3', 'Screen4']}
sub_values = ListProperty()
def values_update(self,text):
self.sub_values = self.values_dict[text]
if text != 'Select group type':
self.ids.sub_drop.text = 'select ' + text + ' screen'
return 'select ' + text + ' screen'
def open_screen(self, text):
if text != 'select ' + self.ids.main_drop.text + ' screen':
sm = windowManager()
sm.current = text
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
class Screen4(Screen):
pass
class appln(App):
def build(self):
return windowManager()
if __name__=="__main__":
appln().run()
appln.kv
<WindowManager>:
MainScreen:
Screen1:
Screen2:
Screen3:
Screen4:
<MainScreen>:
name: 'main'
GridLayout:
cols:2
Spinner:
id: main_drop
size_hint: None, None
size: 200, 100
pos_hint:{'top': 1}
text: 'Select group type'
values: root.values_dict.keys()
on_text:
root.values_update(self.text)
Spinner:
id: sub_drop
size_hint: None, None
size: 230, 100
pos_hint:{'top': 1}
values: root.sub_values
on_text: root.open_screen(self.text)
<Screen1>:
name: 'Screen1'
Label:
text: 'This is screen 1'
<Screen2>:
name: 'Screen2'
Label:
text: 'This is screen 2'
<Screen3>:
name: 'Screen3'
Label:
text: 'This is screen 3'
<Screen4>:
name: 'Screen4'
Label:
text: 'This is screen 4'
In your MainScreen code, the line:
sm = windowManager()
is creating a new instance of windowManager, so any use of that instance will have no effect on the windowManager that is actually managing your Screens. To fix this, just use a reference to the correct windowManager by replacing that line with:
sm = self.manager
I found this code that showed me how to use a spinner to change screens. It works well in allowing me to switch screens, but I found that I also need to switch screens by using buttons on an individual screen.
Here is an example of what I mean:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner
KV = """
<MainScreen>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Edit'
on_press: root.goEdit()
Label:
text: 'Main Screen'
<HelpScreen>:
Label:
text: 'Help Screen'
<SettingsScreen>:
Label:
text: 'Settings Screen'
<EditScreen>:
name: 'edit_Screen'
text: 'Edit Screen'
<ScreenMenu>:
text: 'main'
values: ('main', 'help', 'settings')
size_hint: None, None
size: 200, 44
"""
class MainScreen(FloatLayout):
def goEdit(self):
MyApp.build.screen_layout.remove_widget(MyApp.screen)
screen = EditScreen()
MyApp.screen = screen
MyApp.screen_layout.add_widget(MyApp.screen)
class HelpScreen(FloatLayout):
pass
class SettingsScreen(FloatLayout):
pass
class EditScreen(FloatLayout):
pass
class ScreenMenu(Spinner):
pass
class MyApp(App):
def build(self):
Builder.load_string(KV)
self.screen = None
self.root = FloatLayout()
self.screen_layout = FloatLayout()
self.menu = ScreenMenu()
self.root.add_widget(self.screen_layout)
self.root.add_widget(self.menu)
self.menu.bind(text=self.select_screen)
self.show('main')
return self.root
def select_screen(self, *args):
self.show(self.menu.text)
def show(self, name='main'):
if self.screen is not None:
self.screen_layout.remove_widget(self.screen)
self.screen = None
if name == 'main':
screen = MainScreen()
elif name == 'help':
screen = HelpScreen()
elif name == 'settings':
screen = SettingsScreen()
else:
raise Exception('Invalid screen name')
self.screen = screen
self.screen_layout.add_widget(screen)
if __name__ == "__main__":
MyApp().run()
As you can see, this is almost the exact code as found in the link above, the only difference is that here, I create an "Edit" button that when clicked, refers to the goEdit() method of the "MainScreen" class.
on_press: root.goEdit()
My problem is I don't know how to create the goEdit() method so that, when called, it goes to the "EditScreen" while also having the spinner work ( it goes to the "MainScreen", "HelpScreen" and "SettingScreen" screens). The code I tried in goEdit() clearly doesn't work.
I have also tried changing the inherited classes from FloatLayout to Screen:
class MainScreen(Screen):
def goEdit(self):
self.parent.current = 'edit_Screen'
class HelpScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class EditScreen(Screen):
pass
class ScreenMenu(Spinner):
pass
Here I tried to switch screens using this code:
self.parent.current = 'edit_Screen'
but when clicking the 'edit' button, nothing happens, I don't even get error messages.
Essentially what I want is for the spinner to work as it does in the example link, but I also need the 'edit' button to also change screens. Any help would be greatly appreciated.
Solution
Please refer to the explanations, example and output for details.
kv String
Replace root.edit() with app.show(name='edit')
Replace name: 'edit_screen' with Label:
Indent text: 'Edit Screen'
Python Script
Replace all the codings in MainScreen with pass
Add elif name == 'edit': screen = EditScreen() statement in method show
Example
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner
KV = """
<MainScreen>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Edit'
on_press: app.show(name='edit')
Label:
text: 'Main Screen'
<HelpScreen>:
Label:
text: 'Help Screen'
<SettingsScreen>:
Label:
text: 'Settings Screen'
<EditScreen>:
Label:
text: 'Edit Screen'
<ScreenMenu>:
text: 'main'
values: ('main', 'help', 'settings')
size_hint: None, None
size: 200, 44
"""
class MainScreen(FloatLayout):
pass
class HelpScreen(FloatLayout):
pass
class SettingsScreen(FloatLayout):
pass
class EditScreen(FloatLayout):
pass
class ScreenMenu(Spinner):
pass
class MyApp(App):
def build(self):
Builder.load_string(KV)
self.screen = None
self.root = FloatLayout()
self.screen_layout = FloatLayout()
self.menu = ScreenMenu()
self.root.add_widget(self.screen_layout)
self.root.add_widget(self.menu)
self.menu.bind(text=self.select_screen)
self.show('main')
return self.root
def select_screen(self, *args):
self.show(self.menu.text)
def show(self, name='main'):
if self.screen is not None:
self.screen_layout.remove_widget(self.screen)
self.screen = None
if name == 'main':
screen = MainScreen()
elif name == 'help':
screen = HelpScreen()
elif name == 'settings':
screen = SettingsScreen()
elif name == 'edit':
screen = EditScreen()
else:
raise Exception('Invalid screen name')
self.screen = screen
self.screen_layout.add_widget(screen)
if __name__ == "__main__":
MyApp().run()
Output
Here is a very simple example using the ScreenManager:
Python
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
class MyScreenManager(ScreenManager):
def changescreen(self, value):
try:
if value!='Choose a Value...':
self.current = value
except:
print('No screen named ' + value)
#Main application
class TestApp(App):
def build(self):
self.sm = MyScreenManager()
return self.sm
if __name__ == '__main__':
TestApp().run()
kv
<MyScreenManager>:
Screen1:
name: 'screen1'
Screen2:
name: 'screen2'
Screen3:
name: 'screen3'
<Screen1>:
GridLayout:
rows: 2
padding: 20
spacing: 20
Button:
text: 'Go to Screen 2'
on_press: root.manager.current = 'screen2'
Button:
text: 'Go to Screen 3'
on_press: root.manager.current = 'screen3'
Spinner:
text: 'Choose a Value...'
values: ['screen2', 'screen3']
on_text:
root.manager.changescreen(self.text)
self.text = 'Choose a Value...'
Label:
text: 'You are on ' + root.name
<Screen2>:
GridLayout:
rows: 2
padding: 20
spacing: 20
Button:
text: 'Go to Screen 1'
on_press: root.manager.current = 'screen1'
Button:
text: 'Go to Screen 3'
on_press: root.manager.current = 'screen3'
Spinner:
text: 'Choose a Value...'
values: ['screen1', 'screen3']
on_text:
root.manager.changescreen(self.text)
self.text = 'Choose a Value...'
Label:
text: 'You are on ' + root.name
<Screen3>:
GridLayout:
rows: 2
padding: 20
spacing: 20
Button:
text: 'Go to Screen 1'
on_press: root.manager.current = 'screen1'
Button:
text: 'Go to Screen 2'
on_press: root.manager.current = 'screen2'
Spinner:
text: 'Choose a Value...'
values: ['screen1', 'screen2']
on_text:
root.manager.changescreen(self.text)
self.text = 'Choose a Value...'
Label:
text: 'You are on ' + root.name
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'
In my RootWidget I have a label and two buttons. I want to dynamically change the text of the label whenever one of the buttons is clicked.
Here's a minimal working example of how I do it at the moment.
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
w = Builder.load_string('''
<RootWidget>:
id: root_widget
Label:
id: label
text: 'Push a button'
Button:
text: '1'
on_press: label.text = self.text
Button:
text: '2'
on_press: label.text = self.text
''')
class RootWidget(BoxLayout):
pass
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
MainApp().run()
Obviously I want to refactor the line on_press: label.text = self.text. My first tries ended in
<RootWidget>:
id: root_widget
Label:
id: label
text: 'Push a button'
MyButton:
text: '1'
MyButton:
text: '2'
<MyButton>:
on_press: label.text = self.text
But obviously the MyButton-class doesn't know the property label of the RootWidget-class. And class rules inside class rules is also not allowed.
Is there a way of accomplishing binding the on_press-action dynamically?
You can refer to the Label like this:
<MyButton#Button>:
on_press: self.parent.ids.label.text = self.text
<RootWidget>:
id: root_widget
Label:
id: label
text: 'Push a button'
MyButton:
text: '1'
MyButton:
text: '2'
It's quite simple actually, because through kv you can access parent of a widget/rule easily with self.parent, so your code would look like:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
w = Builder.load_string('''
<RootWidget>:
id: root_widget
Label:
id: label
text: 'Push a button'
But:
text: '1'
But:
text: '2'
<But>:
on_press: self.parent.label.text = self.text
''')
class RootWidget(BoxLayout):
pass
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
MainApp().run()