Python - KivyMD - ScreenManager y ids.

 
Vista:
Imágen de perfil de Jorge Alberto
Val: 137
Ha disminuido 1 puesto en Python (en relación al último mes)
Gráfica de Python

KivyMD - ScreenManager y ids.

Publicado por Jorge Alberto (48 intervenciones) el 11/08/2021 18:47:41
Buenas tardes. Estoy practicando con KivyMD y sqlite3 y me encontré con un problema. La idea es un programa sencillo que permita a una persona loguearse o registrarse (nada oficial, es sólo para practicar). Al principio agregue tanto la pantalla de logueo como la de registro en una sola ventana y funcionaba perfectamente, pero cuando quise separar las pantallas empezaron los problemas. Cuando hago click en el botón 'LOG IN' o 'SIGN UP' me arroja el error:

1
2
3
4
File "signup_login.py", line 45, in submit_info
     _username = _username = self.root.ids.my_username.text
   File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

Les paso el archivo py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sqlite3
 
class MainMenu(Screen):
	pass
 
class LogIn(Screen):
	pass
 
class SignUp(Screen):
	pass
 
class MainLogSignApp(MDApp):
 
	def build(self):
		self.title = 'Login - Sign Up'
 
		return Builder.load_file('mainlogsign.kv')
 
	def submit_info(self):
		'''If the info does not exist, it adds it to the table;
		If it does, shows a message indicating that the username already exists'''
 
		_username = self.root.ids.my_username.text
		_password = self.root.ids.my_password.text
 
		# Connect and create cursor
		conn = sqlite3.connect('user_pass.db')
		c = conn.cursor()
 
		# This should not be necesary, but it is here just in case I delete the table using the DELETE button.
		c.execute("""CREATE TABLE if not exists user_name(
					username text,
					password text
			)""")
 
		c.execute("SELECT * FROM user_name WHERE username = (?)", (_username,))
		already_in = c.fetchone()
 
		if already_in and already_in[0]:
			self.root.ids.my_message.text = already_in[0] + '\nUser already exists'
		else:
			c.execute("INSERT INTO user_name VALUES (?,?)", (_username, _password))
			self.root.ids.my_message.text = 'User added successfully'
 
		conn.commit()
		conn.close()
 
		self.root.ids.my_username.text = ''
		self.root.ids.my_password.text = ''
 
 
	def log_in(self):
 
		# Connect and create cursor:
		conn = sqlite3.connect('user_pass.db')
		c = conn.cursor()
 
		_username = self.root.ids.my_username_l.text
		_password = self.root.ids.my_password_l.text
 
		c.execute("SELECT * FROM user_name WHERE username = ?", (_username,))
		data = c.fetchone()
 
		if data and data[0] == _username and data[1] == _password:
			self.root.ids.my_message_l.text = 'Log in successfully'
		else:
			self.root.ids.my_message_l.text = 'The info is incorrect'
 
		# Commit and close:
		conn.commit()
		conn.close()
 
if __name__ == '__main__':
	MainLogSignApp().run()

y el archivo kv:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
ScreenManager:
	name: 'screen_manager'
 
	MainMenu:
	LogIn:
	SignUp:
 
<MyTextField@MDTextFieldRound>:
	font_size: 12
	size_hint_x: 0.6
	pos_hint: {'center_x': 0.5}
	halign: 'center'
 
 
<MainMenu>:
	name: 'main_menu'
 
	BoxLayout:
		orientation: 'vertical'
 
		Label:
			size_hint: .1, .3
 
		MDRoundFlatButton:
			text: 'LOG IN'
			pos_hint: {'center_x': .5}
			on_release:
				root.manager.current = 'login'
				root.manager.transition.direction = 'left'
 
		Label:
			size_hint: .1, .1
 
		MDRoundFlatButton:
			text: 'SIGN UP'
			pos_hint: {'center_x': .5}
			on_release:
				root.manager.current = 'signup'
				root.manager.transition.direction = 'left'
 
		Label:
			size_hint: .1, .3
 
 
<LogIn>:
	name: 'login'
 
	BoxLayout:
		orientation: 'vertical'
 
		MDLabel:
			text: 'LOGIN'
			halign: 'center'
 
 
		MyTextField:
			id: my_username_l
			hint_text: 'Enter your username'
			icon_right: 'account'
 
		MyTextField:
			id: my_password_l
			hint_text: 'Enter your password'
			icon_right: 'eye-outline'
 
		Button:
			id: log_in
			text: 'LOG IN'
			size_hint: 0.6, 0.2
			pos_hint: {'center_x': .5}
			on_press: app.log_in()
 
 
		MDLabel:
			id: my_message_l
			text: ''
			halign: 'center'
 
		MDRectangleFlatIconButton:
			icon: 'backspace-outline'
			text: 'BACK'
			pos_hint: {'right': 1}
			on_release:
				root.manager.current = 'main_menu'
				root.manager.transition.direction = 'right'
 
 
 
<SignUp>:
	name: 'signup'
 
	BoxLayout:
		orientation: 'vertical'
		MDLabel:
			text: 'SIGNUP'
			halign: 'center'
 
 
		MyTextField:
			id: my_username
			hint_text: 'Enter your username'
			icon_right: 'account'
 
		MyTextField:
			id: my_password
			hint_text: 'Enter your password'
			icon_right: 'eye-outline'
 
		Button:
			id: submit_button
			text: 'Submit info'
			size_hint: 0.6, 0.2
			pos_hint: {'center_x': .5}
			on_press: app.submit_info()
 
 
		MDLabel:
			id: my_message
			text: ''
			halign: 'center'
 
		MDRectangleFlatIconButton:
			icon: 'backspace-outline'
			text: 'BACK'
			pos_hint: {'right': 1}
			on_release:
				root.manager.current = 'main_menu'
				root.manager.transition.direction = 'right'

Basándome en el error, supongo que hay que referenciar las ids de alguna otra manera, pero no sé cómo.

Muchas gracias de antemano.

PD: como un problema secundario, al escribir texto en los TextInput no se borra el texto preestablecido (el hint_text). Por ahora no me preocupa tanto, pero si alguien el por qué se lo agradezco.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder