Skip to content

Commit a31c859

Browse files
committed
Demo kivy app
1 parent c1d4ec5 commit a31c859

File tree

4 files changed

+95
-36
lines changed

4 files changed

+95
-36
lines changed
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
1-
21
from kivymd.app import MDApp
2+
from kivymd.uix.screen import Screen
3+
from kivy.lang import Builder
4+
from kivymd.uix.boxlayout import MDBoxLayout
5+
from kivymd.uix.label import MDLabel
36

47
class Demo(MDApp):
5-
68
def build(self):
7-
return super().build()
9+
screen = Screen()
10+
self.theme_cls.primary_palette = "Green"
11+
12+
# Create a box layout to center elements
13+
box_layout = MDBoxLayout(
14+
orientation="vertical",
15+
spacing=20,
16+
pos_hint={"center_x": 0.5, "center_y": 0.5},
17+
size_hint=(None, None),
18+
)
19+
20+
# Label
21+
label = MDLabel(text="Login", halign="center")
22+
23+
# Load the text fields from the strings
24+
username = Builder.load_string(username_helper)
25+
password = Builder.load_string(password_helper)
26+
27+
28+
# Add widgets to the box layout
29+
box_layout.add_widget(label)
30+
box_layout.add_widget(username)
31+
box_layout.add_widget(password)
32+
33+
# Set the size of the box layout according to the widgets
34+
box_layout.height = username.height + password.height + 40 # Adding spacing
35+
box_layout.width = 400 # Width matches the text fields
36+
37+
# Add the box layout to the screen
38+
screen.add_widget(box_layout)
39+
40+
return screen
841

9-
Demo().run()
42+
Demo().run()
540 Bytes
Binary file not shown.

Kivy/helpers.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
username_input = """
1+
username_helper = """
22
MDTextField:
3-
hint_text: "Enter username"
4-
helper_text: "or click on forgot username"
3+
hint_text: "Enter your username"
4+
size_hint_x: None
5+
width: 400
6+
icon_right: "account"
7+
"""
8+
9+
password_helper = """
10+
MDTextField:
11+
hint_text: "Enter your password"
12+
helper_text: "or click on forgot password"
513
helper_text_mode: "on_focus"
6-
icon_right: "android"
7-
icon_right_color: app.theme_cls.primary_color
8-
pos_hint:{'center_x': 0.5, 'center_y': 0.5}
9-
size_hint_x:None
10-
width:300
14+
size_hint_x: None
15+
width: 400
16+
icon_right: "key-variant"
17+
password: True
1118
"""

Kivy/main.py

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11

22
from kivymd.app import MDApp
33
from kivymd.uix.screen import Screen
4-
from kivymd.uix.button import MDButton
5-
from kivymd.uix.textfield import MDTextField
4+
from kivymd.uix.boxlayout import MDBoxLayout
5+
from kivymd.uix.label import MDLabel
6+
from helpers import username_helper, password_helper
67
from kivy.lang import Builder
7-
from kivymd.uix.button import MDButton
8-
from kivymd.uix.button import MDButtonIcon
8+
from kivymd.uix.button import MDRaisedButton
9+
from kivymd.uix.dialog import MDDialog
910

10-
username_input = """
11-
MDTextField:
12-
hint_text: "Enter username"
13-
helper_text: "or click on forgot username"
14-
pos_hint:{'center_x': 0.5, 'center_y': 0.5}
15-
size_hint_x:None
16-
width:300
17-
"""
18-
19-
class TodoApp(MDApp):
11+
class Demo(MDApp):
2012

2113
def build(self):
2214
screen = Screen()
15+
self.theme_cls.primary_pelatte = "Green"
2316

24-
# user_name = MDTextField(text="Enter username", pos_hint={"center_x":0.5, "center_y":0.5},
25-
# size_hint_x=None, width=400)
17+
box_layout = MDBoxLayout(
18+
orientation = "vertical",
19+
spacing = 20,
20+
pos_hint = {"center_x":0.5, "center_y":0.5},
21+
size_hint = (None, None)
22+
)
2623

27-
username = Builder.load_string(username_input)
28-
button = MDButtonIcon(text="Click Me", pos_hint={"center_x":0.5, "center_y":0.5})
2924

25+
# Widgets
26+
label = MDLabel(text="Login", halign="center")
27+
self.username = Builder.load_string(username_helper)
28+
self.password = Builder.load_string(password_helper)
29+
button = MDRaisedButton(text="Login", pos_hint={"center_x":0.5, "center_y":0.5},
30+
on_release=self.show_values)
3031

31-
32-
# screen.add_widget(username)
33-
screen.add_widget(button)
32+
# Add widgets in the box layout
33+
box_layout.add_widget(label)
34+
box_layout.add_widget(self.username)
35+
box_layout.add_widget(self.password)
36+
box_layout.add_widget(button)
3437

35-
36-
return screen
38+
box_layout.height = self.username.height + self.password.height + 40
39+
box_layout.width = 400
3740

38-
TodoApp().run()
41+
# Add the box layout in the screen
42+
screen.add_widget(box_layout)
43+
44+
return screen
45+
46+
# Function for showing the dialog
47+
def show_values(self, obj):
48+
close_btn = MDRaisedButton(text="Close")
49+
more_btn = MDRaisedButton(text="More")
50+
dialog = MDDialog(title="Show details",
51+
text=f"Username: {self.username.text} \n\nPassword: {self.password.text}",
52+
size_hint=(0.7, 1),
53+
buttons=[close_btn, more_btn])
54+
dialog.open()
55+
56+
if __name__ == "__main__":
57+
Demo().run()

0 commit comments

Comments
 (0)