def check_full_name(self):
toast("You have to enter your full name to use the app correctly ! ")
MDTextFieldRound:
id:user_app
hint_text: "Full Name"
size_hint_x: None
size_hint_y: 0.08
width: 247
font_size: 18
pos_hint: {'center_x':0.5, 'center_y':0.62}
on_release: app.check_full_name() if user_app.text == '' else app.root.current = "accueil"
I don't know why it doesn't work, I want that when a field is empty calls my check_full_name function.Thanks for answering
According to your question, this should fix your problem:
.py file
from kivymd.app import MDApp
from kivymd.toast import toast
class PyApp(MDApp):
def on_start(self):
if self.root.ids.user_app.text == '':
self.check_full_name()
else:
self.root.ids.user_app.current = "accueil"
def check_full_name(self):
toast("You have to enter your full name to use the app correctly ! ")
PyApp().run()
.kv file
FloatLayout:
MDTextFieldRound:
id:user_app
hint_text: "Full Name"
size_hint_x: None
size_hint_y: 0.08
width: 247
font_size: 18
pos_hint: {'center_x':0.5, 'center_y':0.62}
on_text_validate: app.on_start()
class SplashScreenApp(MDApp):
dialog = None
def build(self):
dialog = None
self.title = "ReviewinApp"
global sm
sm = ScreenManager()
sm.add_widget(Builder.load_file("splash.kv"))
sm.add_widget(Builder.load_file("accueil.kv"))
sm.add_widget(Builder.load_file('conditions.kv'))
sm.add_widget(Builder.load_file("register.kv"))
sm.add_widget(Builder.load_file("faq.kv"))
sm.add_widget(Builder.load_file("login.kv"))
sm.add_widget(Builder.load_file("User2.kv"))
self.theme_cls.theme_style = "Light"
return sm
def on_start(self):
if self.root.ids.user_app.text == ' ':
toast("Please register correctly")
else:
toast("It's okay")
```
MDFillRoundFlatButton:
text: "Finish !"
font_size: "20sp"
font_name: "OpenSans"
pos_hint: {'center_x':0.5, 'center_y':0.07}
halign: 'center'
theme_text_color:'Custom'
text_color: 255/255, 255/255, 255/255, 1
size_hint: (0.58, 0.06)
md_bg_color: 62/255, 216/255, 133/255, 1
background_normal:''
on_press: app.on_start()
```
The python file and the kivy files are on different pages. One with extension .kv and one with .py
Related
I want to know the method of loading the second screen when authentication is passed.
I can call the second screen from the .kv file vi on_press or other methods. But I need to call from python code to check the authentication.
Can anyone help with my code?
Here is my code:
app.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty
class my_layout(FloatLayout):
screen_mngr = ObjectProperty(None)
class myapp(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "BlueGray"
return Builder.load_file("app.kv")
def logger(self):
if self.root.ids.user.text == 'admin' and self.root.ids.password.text=='admin':
self.root.ids.welcome_label.text = f'Sup {self.root.ids.user.text}!'
screen = Screen(name='screen2')
else:
self.root.ids.welcome_label.text = 'Wrong credentials'
if __name__ == "__main__":
myapp().run()
And here is my design kv file.
app.kv file:
my_layout:
screen_mngr: screen_mngr
ScreenManager:
id: screen_mngr
home: home
Screen:
id: home
name: 'home'
MDCard:
size_hint: None, None
size: 450, 600
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
MDLabel:
id: welcome_label
text: "WELCOME"
font_size: 40
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextField:
id: user
hint_text: "username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
MDTextField:
id: password
hint_text: "password"
icon_right: "eye-off"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
MDRoundFlatButton:
text: "LOG IN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: screen2
Screen:
id: screen2
name: 'screen2'
MDRoundFlatButton:
text: "This is Second Screen\nGo to Screen1"
size_hint: 0.2,0.1
pos_hint: {"center_x":0.5,"y":0.5}
on_press: screen_mngr.current = "home"
A couple small problems:
First, in your kv you need to have your LOG IN Button call the code to handle the log in, like this:
MDRoundFlatButton:
text: "LOG IN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: app.logger() # call the method that handles login
Then, in that method, you can change to the screen2 by using the current property of the ScreenManager, like this:
def logger(self):
if self.root.ids.user.text == 'admin' and self.root.ids.password.text=='admin':
self.root.ids.welcome_label.text = f'Sup {self.root.ids.user.text}!'
self.root.ids.screen_mngr.current = 'screen2' # go to screen2
# screen = Screen(name='screen2')
else:
self.root.ids.welcome_label.text = 'Wrong credentials'
I'm working on a project using Kivy with ScreenManager (and KivyMD).
The reason for using this is my need of making an application with multiple screens. My goal is to get User Input from an existing MDTextField by (a string) ID and then Print it out.
However, I keep getting this Error: File "<string>", line 37, in <module> File "c:/Users/admin/Desktop/myApp/Main.py", line 75, in loginFunction username = mainScreenInstance.ids["input_username"].text KeyError: 'input_username'.
I have searched for answers all over the internet and StackOverflow, but no solution seems to work for me. Maybe there's someone who can help me with my problem and make my code work.
Anyway, here's my short & simple Code:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
import sys
screen_helper = """
ScreenManager:
MenuScreen:
name: 'menu'
SecondScreen:
name: 'second'
<MenuScreen>:
MDToolbar:
title: "Menu Screen"
pos_hint: {"top": 1}
anchor_title: "center"
md_bg_color: (0/255, 0/255, 0/255, 1)
Image:
source: "Logo_h_black.png"
pos_hint: {"center_x": 0.5, "center_y": 0.75}
size_hint_x: (0.25)
size_hint_y: (0.25)
MDTextField:
id: input_username
hint_text: "Username"
size_hint: (0.5, 0.1)
pos_hint: {"center_x": 0.5, "center_y": 0.55}
font_size: 20
mode: "rectangle"
MDTextField:
id: input_password
hint_text: "Password"
size_hint: (0.5, 0.1)
pos_hint: {"center_x": 0.5, "center_y": 0.45}
font_size: 20
mode: "rectangle"
MDFillRoundFlatButton:
text: "LOG IN"
font_size: 17
pos_hint: {"center_x": 0.5, "center_y": 0.25}
on_press: app.loginFunction()
<SecondScreen>:
MDLabel:
text: 'Profile'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press: root.manager.current = 'menu'
"""
class MenuScreen(Screen):
pass
class SecondScreen(Screen):
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SecondScreen(name='second'))
class myApp(MDApp):
def loginFunction(self, args=None):
if args is None:
args = sys.argv
mainScreenInstance = sm.get_screen('menu')
username = mainScreenInstance.ids["input_username"].text
password = mainScreenInstance.ids["input_password"].text
print (username)
print (password)
def build(self):
screen = Builder.load_string(screen_helper)
return screen
myApp().run()
The problem is that your loginFunction is trying to get the mainScreenInstance from sm, but sm is created before the screen_helper is loaded, so it doesn't know about the ids you have defined in your screen_helper. Also, that sm is not actually used as part of you GUI, it is just ignored (except for your reference to it in the loginFunction).
So, I recommend that you eliminate the following lines completely:
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SecondScreen(name='second'))
And change loginFunction to not use sm by replacing:
mainScreenInstance = sm.get_screen('menu')
with:
mainScreenInstance = self.root.get_screen('menu')
You're duplicating the widget definitions and then not using them in code. Finally, you're overwriting the Screen manager from the screen_helper string with a new instance. Tbh I did some of that when I started learning Kivy. The kv language is confusing at first but once you get it is beautiful. This version should work (I haven't tested it since I haven't found an easy way to install kivymd on my phone's dev env):
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
import sys
screen_helper = """
ScreenManager:
MenuScreen:
name: 'menu'
SecondScreen:
name: 'second'
<MenuScreen>:
MDToolbar:
title: "Menu Screen"
pos_hint: {"top": 1}
anchor_title: "center"
md_bg_color: (0, 0, 0, 1)
Image:
source: "Logo_h_black.png"
pos_hint: {"center_x": 0.5, "center_y": 0.75}
size_hint_x: (0.25)
size_hint_y: (0.25)
MDTextField:
id: input_username
hint_text: "Username"
size_hint: (0.5, 0.1)
pos_hint: {"center_x": 0.5, "center_y": 0.55}
font_size: 20
mode: "rectangle"
MDTextField:
id: input_password
hint_text: "Password"
size_hint: (0.5, 0.1)
pos_hint: {"center_x": 0.5, "center_y": 0.45}
font_size: 20
mode: "rectangle"
MDFillRoundFlatButton:
text: "LOG IN"
font_size: 17
pos_hint: {"center_x": 0.5, "center_y": 0.25}
on_press: app.loginFunction()
<SecondScreen>:
MDLabel:
text: 'Profile'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press: root.manager.current = 'menu'
"""
class MenuScreen(Screen):
pass
class SecondScreen(Screen):
pass
class myApp(MDApp):
sm = None
def on_start():
self.sm = self.root
def loginFunction(self, args=None):
if args is None:
args = sys.argv
mainScreenInstance = self.sm.get_screen('menu')
username = mainScreenInstance.ids["input_username"].text
password = mainScreenInstance.ids["input_password"].text
print (username)
print (password)
# if self.is_login_correct(username, password):
# self.sm.current('second')
def build(self):
return Builder.load_string(screen_helper)
myApp().run()
Personally, I would move all login-related functions to the MenuScreen class, for consistency.
I made a chat app but it was command line so I wanted to add some gui to it and after I started making app use ScreenManager the error start occurring.
Here is python code:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(("192.168.41.20", 5002))
except Exception as e:
print(e)
class LoginScreen(Screen):
pass
class MainScreen(Screen):
pass
class SettingScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
class MainApp(MDApp):
def build(self):
return Builder.load_file('kivy.kv')
def logger(self):
self.root.ids.welcome_label.text = ('Hello ' + self.root.ids.user.text + '!')
s.send(bytes('#u'+self.root.ids.user.text, 'utf-8'))
s.send(bytes('#p'+self.root.ids.password.text, 'utf-8'))
def clear(self):
self.root.ids.user.text = ""
self.root.ids.welcome_label.text = "WELCOME"
self.root.ids.password.text = ""
MainApp().run()
And here is my kivymd .kv code:
WindowManager:
LoginScreen:
MainScreen:
SettingScreen:
<LoginScreen>:
name: "login"
Widget:
MDCard:
size_hint: None, None
size: 300, 400
pos_hint: {"center_x": 0.5, "center_y":0.5 }
elevation: 10
padding: 25
spacing: 25
orientation: "vertical"
MDLabel:
id: welcome_label
text: "BALCOME!"
halign: 'center'
font_size: 40
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextFieldRound:
id: user
hint_text: "username"
icon_right: "account"
width: 200
size_hint_x: None
font_size: 18
pos_hint: {"center_x": 0.5}
MDTextFieldRound:
id: password
hint_text: "password"
icon_right: "eye-off"
width: 200
size_hint_x: None
font_size: 18
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "LOGIN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: app.logger()
on_release: app.root.current = "main"
MDRoundFlatButton:
text: "CLEAR"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: app.clear()
Widget:
size_hint_y: None
height: 10
<MainScreen>:
name: "main"
MDRoundFlatButton:
text: "Get LOst"
pos_hint: {"center_y: 0.5"}
<SettingScreen>:
name: "satt"
MDRoundFlatButton:
text: "Get Lost"
pos_hint: {"center_y: 0.5"}
And when I run it this error comes:
AttributeError: 'set' object has no attribute 'items' error occurring when I trying to run my code.
Any help is highly appreciated.
When I try to grab the Text value from my TextField, I always get empty. At the same time, if I set the default text value, for example "123", then regardless of whether I enter anything into my TextField into the console, I still get "123". I had a guess that it might be due to some kind of screen duplication, but when I call self.root.get_screen ('registration'). Ids, I get three different ids, i.e. there is no duplication. I will be glad for your help <3
my.kv
<RegistrationScreen>:
name: "registration"
MDCard:
size_hint: None, None
size: 400, 600
orientation: "vertical"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
padding: 15
spacing: 50
MDLabel:
text: "Регистрация"
font_name: 'fonts/montserrat-bold.ttf'
font_size: 20
color: .43, 0, .48, 1
halign: "center"
BoxLayout:
size_hint: None, None
size: 200, 160
pos_hint: {"center_x": 0.5}
orientation: "vertical"
MDTextField:
id: pomogite
hint_text: "Rectangle mode"
mode: "rectangle"
helper_text_mode: "on_focus"
hint_text: "Введите логин"
helper_text: "Минимум 6 символов (a-z, A-Z, 0-9)"
icon_right: "account"
color_mode: 'custom'
line_color_focus: .43, 0, .48, 1
size_hint_x: None
width: 250
pos_hint: {"center_x": .5, "center_y": .3}
text: "Начинайте ввод"
MDTextField:
id: textfield_password
hint_text: "Rectangle mode"
mode: "rectangle"
helper_text_mode: "on_focus"
hint_text: "Введите пароль"
helper_text: "Минимум 6 символов (a-z, A-Z, 0-9)"
icon_right: "form-textbox-password"
color_mode: 'custom'
line_color_focus: .43, 0, .48, 1
size_hint_x: None
width: 250
pos_hint: {"center_x": .5, "center_y": .3}
MDRectangleFlatButton:
id: reg
text: "Регистрация"
theme_text_color: "Custom"
text_color: .43, 0, .48, 1
line_color: .43, 0, .48, 1
pos_hint: {"center_x": .5}
on_press: app.registration()
main.py
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from client import Client
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.textfield import MDTextField
sm = ScreenManager()
class LoadingScreen(Screen):
pass
class AuthorizationScreen(Screen):
pass
class RegistrationScreen(Screen):
pass
class MyApp(MDApp):
def build(self):
sm.add_widget(LoadingScreen(name='loading'))
sm.add_widget(AuthorizationScreen(name='authorization'))
sm.add_widget(RegistrationScreen(name='registration'))
sm.switch_to(AuthorizationScreen())
return sm
def fff(self):
self.screen.ids.text_field_error.error = True
sm.switch_to(LoadingScreen())
def registration(self):
addwindow_instance = self.root.get_screen('registration')
print(addwindow_instance.ids)
print(addwindow_instance.ids["pomogite"].text)
MyApp().run()
A couple errors in your code, Whenever you use a classname followed by (), you are creating a new instance of that class. So the line:
sm.switch_to(AuthorizationScreen())
creates a new instance of AuthorizationScreen, in addition to the one already created by the line:
sm.add_widget(AuthorizationScreen(name='authorization'))
A better approach is to use sm.current = rather than sm.switch_to, like this:
def build(self):
sm.add_widget(LoadingScreen(name='loading'))
sm.add_widget(AuthorizationScreen(name='authorization'))
sm.add_widget(RegistrationScreen(name='registration'))
sm.current = 'authorization'
return sm
This switched the current screen to the already existing AuthorizationScreen instance. Or, even simpler, just place the AuthorizationScreen as the first added Screen and it will become the current Screen:
def build(self):
sm.add_widget(AuthorizationScreen(name='authorization'))
sm.add_widget(LoadingScreen(name='loading'))
sm.add_widget(RegistrationScreen(name='registration'))
return sm
The same error appears in your fff() method in the line:
sm.switch_to(LoadingScreen())
which is creating a new instance of LoadingScreen rather than using the already existing one. That line should probably be:
sm.current = 'loading'
I got a popup filechooser from the below link:
Kivy popup Filechooser pass variable (selection)
I cannot get the file path string value to pass between my main Tab() class instance of the app and the class FileChoosePopup popup instance. I know this question has been asked before, but I cannot seem to figure it out. When I run the app and click on the file I want to pass to a TextInput on my main class I get the following error:
AttributeError: 'super' object has no attribute '__getattr__'
I have tried passing a reference to the popup class instance in the main class by using the super init method, but then the app does not even initialize.
Here is my popup in python:
class FileChoosePopup(Popup):
file_path = StringProperty("No file chosen")
def __init__(self, **kwargs):
super(FileChoosePopup, self).__init__(**kwargs)
self.Tab = Tab()
def load(self, selection):
self.file_path = str(selection[0])
path_file = self.file_path
the_file = self.ids.get_file #is the textinput id
if path_file != "No file chosen":
the_file.text = path_file
self.dismiss()
else:
self.dismiss()
class Tab(StackLayout):
def open_popup(self):
the_popup = FileChoosePopup()
the_popup.open()
Here the kivy code:
<FileChoosePopup>:
title: "Choose a .CSV File"
size_hint: .9, .9
auto_dismiss: False
BoxLayout:
orientation: "vertical"
FileChooser:
id: filechooser
FileChooserIconLayout
BoxLayout:
size_hint: (1, 0.1)
pos_hint: {'center_x': .5, 'center_y': .5}
spacing: 20
RoundedCancelButton:
text: "Cancel"
on_release: root.dismiss()
RoundedAcceptButton:
text: "Load"
on_release: root.load(filechooser.selection)
id: ldbtn
disabled: True if filechooser.selection==[] else False
<Tab>:
TabbedPanel:
do_defualt_tab: False
background_color: (.87, .87, .87, 1)
border: [0, 0, 0, 0]
background_image: 'path/to/background/image'
TabbedPanelItem:
text: 'Import'
background_color: (1, .5, 0, 1)
background_normal: ''
StackLayout:
orientation: 'lr-tb'
size_hint_y: None
height: 30
spacing: 5
Label:
text: ''
size_hint_x: 1
Label:
text: ''
size_hint_x: 0.2
RoundedButton:
text: 'Choose File'
size_hint_x: 0.2
on_press: root.open_popup()
TextInput:
id: get_file
readonly: True
size_hint_x: 0.4
Label:
text: ''
size_hint_x: 0.2
Can someone please give me some pointers on how to get the value to pass from the popup to the textinput?
Reference TextInput
Populate TextInput by using the following:
self.ids.get_file.text = self.file_path
Example
main.py
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty, StringProperty
from kivy.lang import Builder
class FileChoosePopup(Popup):
load = ObjectProperty()
class Tab(TabbedPanel):
file_path = StringProperty("No file chosen")
the_popup = ObjectProperty(None)
def open_popup(self):
self.the_popup = FileChoosePopup(load=self.load)
self.the_popup.open()
def load(self, selection):
self.file_path = str(selection[0])
self.the_popup.dismiss()
print(self.file_path)
# check for non-empty list i.e. file selected
if self.file_path:
self.ids.get_file.text = self.file_path
Builder.load_file('main.kv')
class TestApp(App):
def build(self):
return Tab()
if __name__ == "__main__":
TestApp().run()
main.kv
<FileChoosePopup>:
title: "Choose a .CSV File"
size_hint: .9, .9
auto_dismiss: False
BoxLayout:
orientation: "vertical"
FileChooser:
id: filechooser
FileChooserIconLayout
BoxLayout:
size_hint: (1, 0.1)
pos_hint: {'center_x': .5, 'center_y': .5}
spacing: 20
Button:
text: "Cancel"
on_release: root.dismiss()
Button:
text: "Load"
on_release: root.load(filechooser.selection)
id: ldbtn
disabled: True if filechooser.selection==[] else False
<Tab>:
do_default_tab: False
TabbedPanelItem:
text: 'Import'
background_color: (1, .5, 0, 1)
background_normal: ''
StackLayout:
orientation: 'lr-tb'
size_hint_y: None
height: 30
spacing: 5
Label:
text: ''
size_hint_x: 1
Label:
text: ''
size_hint_x: 0.2
Button:
text: 'Choose File'
size_hint_x: 0.2
on_press: root.open_popup()
TextInput:
id: get_file
readonly: True
size_hint_x: 0.4
Label:
text: ''
size_hint_x: 0.2
Output