I want to change the screen by pressing the button and checking that the text box is not empty.
here is my kv file:
test.kv:
ScreenManager:
id: sc_m
Screen:
name: 'welcome'
MDLabel:
text: 'WeLCOME'
halign: 'center'
font_style: 'H3'
color: (1, 1, 1, 1)
pos_hint: {'center_x': 0.5, 'center_y':0.8}
MDTextField:
id: textbox
hint_text: "The text box should not be empty"
text:""
pos_hint: {'center_x': .5, 'center_y': .5}
MDRoundFlatIconButton:
text: "OK"
icon: "folder"
pos_hint: {'center_x': .5, 'center_y': .3}
on_press: app.callback()
Screen:
name:'screen2'
MDLabel:
text: 'WeLCOME'
halign: 'center'
font_style: 'H3'
color: (1, 1, 1, 1)
pos_hint: {'center_x': 0.5, 'center_y':0.8}
and here is my python code:
main.py:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style="Dark"
self.theme_cls.primary_palette="Cyan"
return Builder.load_file('test.kv')
def callback(self):
text = self.root.ids.textbox.text
if len(text)==0:
print('The text box should not be empty')
else:
self.root.ids.sc_m.current='screen2'
MainApp().run()
And the error i have:
error:
AttributeError: 'super' object has no attribute '__getattr__'
please help me!
Please do not read the rest of the article
I do not know what to say anymore because it gets an error when posting!
'''It looks like your post is mostly code; please add some more details.'''
Your code:
self.root.ids.sc_m.current='screen2'
is trying to use an id that does not exist. You cannot make an ids entry for the root object of a rule (the ids only includes lower level widgets). But, since the ScreenManager is the root widget, you don't need to access it using ids anyway. Just replace the above code with:
self.root.current = 'screen2'
Related
When going from the first screen to the second screen, I want to pass a variable as an argument so that kivyMD can update the second screen from text stored in an excel file. The following is a skeleton of my app's functionality:
The user reaches Screen 1 thru the navigation drawer in KivyMD, screen 1 presents the user with two options on two small clickable MDCards:
"Change text to 1"
"Change text to 2"
After clicking on one of these, the app switches to screen 2 with a single big MDCard, the text on this MDCard should change to reflect the option the user chose.
However, kivy is pulling the text that is to be displayed on the big MDCard from an excel file.
The variable that I want to pass from screen 1 to screen 2 is simply a number (1 or 2) that will tell kivy which row in the excel file it should pull the text from
If the user clicks "Change text to 1" then the first screen should pass "1" as the argument row_x to the function def change_text() (see screen 2 .py) so that the text in row 1 of excel can be displayed on the second screen. How can I achieve this?
I have 4 files in total; 3 are .py files (one for the main app, one for screen 1, and one for screen 2), and the excel file
NOTE: in the code below, Screen 1 & 2 are called Element 1 & 2 respectfully
Main.py:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from element_1 import element_1_screen
from element_2 import element_2_screen
MainNavigation = '''
<ContentNavigationDrawer>:
ScrollView:
MDList:
OneLineListItem:
text: 'Go to Element 1'
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "go_to_element_1_screen"
Screen:
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
Screen:
name: "words_nav_item"
element_1_screen:
name: "go_to_element_1_screen"
element_2_screen:
name: "go_to_element_2_screen"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class mainApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Red"
return Builder.load_string(MainNavigation)
mainApp().run()
Screen 1 / Element 1
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen
element_1_contents = '''
<element_1_screen>:
MDGridLayout:
rows: 2
size: root.width, root.height
pos_hint: {"center_x": .8, "center_y": .2}
spacing: 40
MDCard:
orientation: 'vertical'
size_hint: None, None
size: "360dp", "120dp"
ripple_behavior: True
on_release:
root.manager.current = "go_to_element_2_screen"
MDLabel:
id: LabelTextID
text: "Change Text to 1"
halign: 'center'
MDCard:
orientation: 'vertical'
size_hint: None, None
size: "360dp", "120dp"
ripple_behavior: True
on_release:
root.manager.current = "go_to_element_2_screen"
MDLabel:
id: LabelTextID
text: "Change Text to 2"
halign: 'center'
'''
class element_1_screen(MDScreen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Builder.load_string(element_1_contents)
Screen 2 / Element 2
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen
import openpyxl
element_2_contents = '''
<element_2_screen>:
MDCard:
orientation: 'vertical'
size_hint: None, None
size: "360dp", "360dp"
pos_hint: {"center_x": .5, "center_y": .5}
ripple_behavior: True
focus_behavior: True
on_release: root.manager.current = "go_to_element_1_screen"
MDLabel:
id: TextID
text: "NOTHING HAS CHANGED"
halign: 'center'
MDLabel:
text: "(Click here to return)"
halign: 'center'
'''
class element_2_screen(MDScreen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
path = "data.xlsx"
self.wb_obj = openpyxl.load_workbook(path)
self.sheet_obj = self.wb_obj.active
Builder.load_string(element_2_contents)
def change_text(self, row_x=0):
row_number = self.sheet_obj.cell(row_x, column=1)
self.ids.TextID.text = str(row_number.value)
And the excel file only has two entries in Column A:
Row 1: You have chosen 1
Row 2: You have chosen 2
I found the answer and now it works flawlessly. Someone over on Reddit (u/Username_RANDINT) helped me, this is what they said:
The ScreenManager has a get_screen() method. You could use it to get
the instance of the second screen and call the change_text() method on
that. In the same place where you switch screens, add another line:
on_release:
root.manager.current = "go_to_element_2_screen"
root.manager.get_screen("go_to_element_2_screen").change_text(1)
Then the same for the other card, just pass in 2 instead of 1.
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'm very new to Kivy (been using for about four hours...) and I've hit a wall with popups.
I have a main screen which has four buttons in a float layout. On press down I want the 'MOVE' button to open a popup. Now I've got this working but the popup contains the same four buttons as my mainscreen.
This is my Python code:
def show_movepop():
show = MovePop()
movepopWindow = Popup(title="Move", content=show, size_hint=(None, None),size=(400,400))
movepopWindow.open()
class MovePop(FloatLayout):
pass
class MainWindow(Screen):
def movebtn(self):
show_movepop()
class StatsWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("gamegui.kv")
class MainFloatApp(App):
def build(self):
return kv
if __name__ == "__main__":
MainFloatApp().run()
and this is my .kv file:
WindowManager:
MainWindow:
StatsWindow:
<Button>
font_size:40
color:0.3,0.6,0.7,1
size_hint: 0.5, 0.1
<MainWindow>:
name: "mainscreen"
FloatLayout
Button:
text: "MOVE"
id: move
pos_hint: {"x":0, "y":0.1}
on_release: root.movebtn()
Button:
text: "ACTION"
id: action
pos_hint: {"x":0.5, "y":0.1}
Button:
text: "EXAMINE"
id: examine
pos_hint: {"x":0, "y":0}
Button:
text: "STATS"
id: stats
pos_hint: {"x":0.5, "y":0}
on_release:
app.root.current = "statsscreen"
root.manager.transition.direction = "left"
<StatsWindow>:
name: "statsscreen"
Button:
text: "Back"
on_release:
app.root.current = "mainscreen"
root.manager.transition.direction = "right"
<MovePop>:
Button:
text: "!"
pos_hint: {"x":0.1, "y":0.5}
on_release:
Apologies in advance if the above is super dirty, I'm not very efficient :')
All suggestions appreciated!
Okay so I don't know why but it was the FloatLayout that was causing the problem.
Changed
class MovePop(FloatLayout):
pass
to:
class MovePop(AnchorLayout):
pass
BoxLayout also got rid of the duplicate buttons but I couldn't arrange the content on the popup the way I wanted in that layout.
I'm trying to use the id function to reference text_input from MDTextField, however I can't understand how does the id function works. Does anyone know what is wrong in my code?
The first code is the Main App and the second one is where all the widgets are. I've already searched for a solution in internet, but I can't understand why my code does not work.
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from Screen_helper import Home
class MenuScreen(Screen):
pass
class ProfileScreen(Screen):
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name='Menu'))
sm.add_widget(MenuScreen(name='Profile'))
class Mainapp(MDApp):
def build(self):
screen = Screen()
helper = Builder.load_string(Home)
screen.add_widget(helper)
key = self.root.ids.username_input
return screen
Mainapp().run()
Home = '''
ScreenManager:
MenuScreen:
ProfileScreen:
<MenuScreen>:
name: 'Menu'
MDRectangleFlatButton:
id: my_button
text: 'Profile'
pos_hint: {'center_x': 0.5, 'center_y': 0.1}
on_press: root.manager.current = 'Profile'
MDTextField:
id: username_input
input_filter: "int"
hint_text: 'CHIAVE NUMERICA'
helper_text: 'compresa tra 0 e 95'
helper_text_mode: 'on_focus'
icon_right: 'key-variant'
icon_right_color: app.theme_cls.primary_color
pos_hint: {'center_x':0.5,'center_y':0.55}
size_hint_x:None
width:230
input_filter: 'int'
<ProfileScreen>:
name: 'Profile'
MDLabel:
text: 'Welcome'
halign: 'center'
MDRectangleFlatButton:
text: 'back'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_press: root.manager.current = 'Menu' '''
Give an id to the MenuScreen class and then access to the widget.
Add this to the kv file.
<MenuScreen>:
id: menu
To access the widget you can now do this in python:
key = self.root.menu.ids.username_input
I've recently started learning python and kivy, and for some reason I keeping getting
AttributeError: 'super' object has no attribute '__getattr__'
Currently the code I have is simply for testing this, and I know other similar posts exist but I've followed the things they're doing to fix it and I keep getting the same error.
This part is the text.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_file('text.kv')
# Declare both screens
class MenuScreen(Screen):
pass
class WritingScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(WritingScreen(name='Write'))
screens = [Screen(name='Title {}'.format(i)) for i in range(2)]
class TestApp(App):
def build(self):
return sm
def write(self):
sm.current = 'Write'
def process(self):
text = self.root.ids.input.text
print("Hello")
if __name__ == '__main__':
TestApp().run()
and here is my Text.kv file
<MenuScreen>:
FloatLayout:
Button:
text: 'Write'
on_press: app.write()
size_hint: .3, .2
background_color: 1, 2, 1, 1
pos_hint: {'x': .35, 'y': .4}
<WritingScreen>:
FloatLayout:
TextInput:
id: input
hint_text: "Ello There... Yaren't supposed to be here yet..."
size_hint: .8, .1
pos_hint: {'x':0, 'y': 0.9}
Button:
text: 'print'
on_press: app.process()
size_hint: .2, .1
pos_hint: {'x':.8, 'y': .8}
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
size_hint: .2, .1
pos_hint: {'x':.8, 'y': .9}
Again I am so sorry if this is a repeated issue, but every post I've tried hasn't worked for me. Thank you for all the help, I'm still a beginner, and would appreciate any tips! :)
Your line in the process() method:
text = self.root.ids.input.text
is trying to use the input id as though it was in the ids of the root (which is the ScreenManager), but ids dictionaries are set up in the root of kv rule where they are defined. So the input id is in the ids of the WritingScreen. To access the ids of the WritingScreen, you can change that line of code to something like:
text = self.root.get_screen('Write').ids.input.text