1
+ import flet
2
+ from flet import TextField , FilledButton , Text ,Page ,Container ,padding ,theme ,Image ,FloatingActionButton ,Icon ,icons ,SnackBar ,Theme
3
+
4
+
5
+ class AppMain :
6
+ """
7
+ หน้าหลัก
8
+ """
9
+ def __init__ (self ,page ):
10
+ self .page = page
11
+ self .page .horizontal_alignment = 'center'
12
+ self .page .vertical_alignment = 'center'
13
+ self .TextHeaderWelcome = Text ('สวัสดีสมาชิกทุกท่านด้วยนี่คือแอพทดสอบของ wk-18k' ,style = "headlineLarge" ,text_align = 'center' )
14
+ self .BtnToRes = FilledButton ('กดเพื่อลงทะเบียน' ,height = 50 ,width = 200 )
15
+ self .BtnToRes .on_click = self .to_res
16
+ self .Container1 = Container (content = self .TextHeaderWelcome ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
17
+ self .Container2 = Container (content = self .BtnToRes ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
18
+ self .img = Image (src = f"/icons/icon-512.png" ,width = 100 ,height = 100 ,fit = "contain" ,)
19
+ self .WidgetList = [self .img ,self .Container1 ,self .Container2 ]
20
+ for i in self .WidgetList :
21
+ self .page .add (i )
22
+ self .page .update ()
23
+
24
+
25
+ def to_res (self ,event ):
26
+ """
27
+ ไปหน้าลงทะเบียน
28
+ """
29
+ for i in self .WidgetList :
30
+ self .page .controls .pop ()
31
+ AppRegister (self .page )
32
+ self .page .update ()
33
+
34
+ class AppRegister :
35
+ """
36
+ หน้าลงทะเบียน
37
+ """
38
+ def __init__ (self ,page ):
39
+ self .page = page
40
+ self .page .horizontal_alignment = 'center'
41
+ self .page .vertical_alignment = 'start'
42
+ self .Is_has_validate_name = {'status_name' :0 ,'status_surname' :0 }
43
+ self .Is_has_name_surname = 0
44
+ self .Is_has_session = 0
45
+
46
+ self .create_form ()
47
+ self .btn ()
48
+ if self .Is_has_session == 1 :
49
+ self .Submit .disabled = True
50
+
51
+ def create_form (self ):
52
+ self .NameInput = TextField (label = 'ชื่อ' ,hint_text = 'กรอกชื่อจริง' ,width = 300 )
53
+ self .SurnameInput = TextField (label = 'นามสกุล' ,hint_text = 'กรอกนามสกุล' ,width = 300 )
54
+ self .AgeInput = TextField (label = 'อายุ' ,hint_text = 'อายุ' ,width = 300 ,keyboard_type = 'number' ,value = 0 )
55
+ self .TextHeader = Text ('สมัครสมาชิก' ,style = "displaySmall" )
56
+
57
+ self .Container1 = Container (content = self .TextHeader ,margin = 10 ,)
58
+ self .Container2 = Container (content = self .NameInput ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
59
+ self .Container3 = Container (content = self .SurnameInput ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
60
+ self .Container4 = Container (content = self .AgeInput ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
61
+ self .InputList = [self .Container1 ,self .Container2 ,self .Container3 ,self .Container4 ]
62
+ for i in self .InputList :
63
+ self .page .add (i )
64
+ self .page .update ()
65
+
66
+ def btn (self ):
67
+ self .Submit = FilledButton ('ลงทะเบียน' ,height = 50 ,width = 200 )
68
+ self .Submit .on_click = self .submit
69
+ self .BackMain = FilledButton ('กลับหน้าหลัก' ,height = 50 ,width = 200 )
70
+ self .BackMain .on_click = self .back_main
71
+ self .Container5 = Container (content = self .Submit ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
72
+ self .Container6 = Container (content = self .BackMain ,margin = 5 ,padding = padding .only (left = 30 ,right = 30 ))
73
+ self .BtnList = [self .Container5 ,self .Container6 ]
74
+
75
+ for i in self .BtnList :
76
+ self .page .add (i )
77
+ self .page .update ()
78
+
79
+ def back_main (self ,event ):
80
+ """
81
+ กลับไปหน้าหลัก
82
+ """
83
+ if self .Is_has_session == 1 :
84
+ self .page .remove_at (4 )
85
+ if self .Is_has_validate_name ['status_name' ] == 1 :
86
+ self .page .remove_at (2 )
87
+ if self .Is_has_validate_name ['status_surname' ] == 1 :
88
+ self .page .remove_at (3 )
89
+
90
+ self .pop_form ()
91
+ self .pop_btn ()
92
+ AppMain (self .page )
93
+ def pop_form (self ):
94
+ for i in self .InputList :
95
+ self .page .controls .pop ()
96
+ def pop_btn (self ):
97
+ for i in self .BtnList :
98
+ self .page .controls .pop ()
99
+
100
+ def submit (self ,event ):
101
+ import json
102
+ """
103
+ ส่งข้อมูลลงฐานข้อมูล
104
+ """
105
+ data = [{
106
+ "id" :self .page .session_id ,
107
+ "name" :self .NameInput .value ,
108
+ "surname" :self .SurnameInput .value ,
109
+ "age" :self .AgeInput .value
110
+ }]
111
+ if self .NameInput .value != "" and self .SurnameInput .value != "" :
112
+ with open ('data.json' ,'r' ,encoding = "utf-8" ) as f :
113
+ data_json = json .loads (f .read ())
114
+ if len (data_json ) >= 0 :
115
+ if self .page .session_id not in [i ['id' ] for i in data_json ] :
116
+ if self .NameInput .value not in [i ['name' ] for i in data_json ] or self .SurnameInput .value not in [i ['surname' ] for i in data_json ]:
117
+ data_json .append (data [0 ])
118
+ with open ('data.json' ,'w' ,encoding = "utf-8" ) as f :
119
+ f .write (json .dumps (data_json ,indent = 4 ,ensure_ascii = False ))
120
+ else :
121
+ if self .Is_has_name_surname != 1 :
122
+ if self .Is_has_validate_name ['status_name' ] == 1 :
123
+ self .page .remove_at (2 )
124
+ if self .Is_has_validate_name ['status_surname' ] == 1 :
125
+ self .page .remove_at (3 )
126
+ if self .Is_has_session == 1 :
127
+ self .page .remove_at (4 )
128
+ self .page .insert (4 ,Text ('ชื่อหรือนามสกุลเคยลงทะเบียนไว้แล้ว' ,style = "bodySmall" ,text_align = 'start' ))
129
+ self .Is_has_name_surname = 1
130
+ else :
131
+ if self .Is_has_session != 1 :
132
+ if self .Is_has_validate_name ['status_name' ] == 1 :
133
+ self .page .remove_at (2 )
134
+ if self .Is_has_validate_name ['status_surname' ] == 1 :
135
+ self .page .remove_at (3 )
136
+ if self .Is_has_name_surname == 1 :
137
+ self .page .remove_at (4 )
138
+
139
+ self .Submit .disabled = True
140
+ self .page .insert (4 ,Text ('คุณได้ลงทะเบียนไปแล้ว' ,style = "bodySmall" ,text_align = 'start' ,))
141
+ self .Is_has_session = 1
142
+ self .Is_has_name_surname = 1
143
+
144
+ else :
145
+ if self .Is_has_validate_name ['status_name' ] != 1 :
146
+ self .page .insert (2 ,Text ('กรุณากรอกข้อมูลชื่อให้ครบถ้วน' ,style = "bodySmall" ,text_align = 'start' ))
147
+ self .Is_has_validate_name ['status_name' ] = 1
148
+ if self .Is_has_validate_name ['status_surname' ] != 1 :
149
+ self .page .insert (4 ,Text ('กรุณากรอกข้อมูลนามสกุลให้ครบถ้วน' ,style = "bodySmall" ,text_align = 'start' ))
150
+ self .Is_has_validate_name ['status_surname' ] = 1
151
+ # try:
152
+ # with open('data.json','r',encoding="utf-8") as f:
153
+ # data_json = json.load(f)
154
+ # if len(data_json) >= 1:
155
+ # with open('data.json','w',encoding="utf-8") as f:
156
+ # data_json.append(data[0])
157
+ # json.dump(data_json,f,ensure_ascii=False)
158
+ # except:
159
+ # with open('data.json','w',encoding="utf-8") as f:
160
+ # json.dump(data,f,ensure_ascii=False)
161
+
162
+ # print(self.page.session_id)
163
+ # print(self.NameInput.value)
164
+ # print(self.SurnameInput.value)
165
+ # print(self.AgeInput.value)
166
+
167
+ class SwithMode :
168
+ """
169
+ เปลี่ยนโหมด
170
+ """
171
+ def __init__ (self ,page ):
172
+ self .page = page
173
+ self .page .floating_action_button = FloatingActionButton ("+" ,icon = "add" ,content = Icon (icons .DARK_MODE ))
174
+ self .page .floating_action_button .on_click = self .switch_mode
175
+ def switch_mode (self ,e ):
176
+ """
177
+ สลับโหมดมืด สว่าง
178
+ """
179
+ self .page .theme_mode = "light" if self .page .theme_mode == "dark" else "dark"
180
+ self .page .floating_action_button .content = Icon (icons .LIGHT_MODE ) if self .page .theme_mode == "dark" == "dark" else Icon (icons .DARK_MODE )
181
+ self .page .update ()
182
+
183
+
184
+
185
+ def main (page : Page ):
186
+ page .title = 'แอพ wk18k'
187
+ page .theme = theme .Theme (color_scheme_seed = "indigo" )
188
+ page .theme_mode = "dark"
189
+ page .horizontal_alignment = 'center'
190
+ AppMain (page )
191
+ SwithMode (page )
192
+
193
+ page .update ()
194
+
195
+
196
+ flet .app (target = main ,port = 25648 ,view = flet .WEB_BROWSER )
0 commit comments