-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflet-04-kullanicidan-girdi-almak.html
210 lines (178 loc) · 18.5 KB
/
flet-04-kullanicidan-girdi-almak.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns#">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Metadata -->
<meta name="description" content="3D Modelleme, Animasyon, Render, Vektör - Raster Grafik İşleme ve Python ile Programlama konularına meraklı Açık Kaynak Hayranı bir Makine Mühendisi">
<meta property="og:description" content="3D Modelleme, Animasyon, Render, Vektör - Raster Grafik İşleme ve Python ile Programlama konularına meraklı Açık Kaynak Hayranı bir Makine Mühendisi">
<meta property="og:title" content="Flet 04 - Kullanıcıdan Girdi Almak" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/flet-04-kullanicidan-girdi-almak.html" />
<meta property="og:image" content="/images/avatar.png" />
<!-- Enable responsiveness on mobile devices-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>mhalil - Programlama ve 3D</title>
<!-- CSS -->
<link href="//fonts.googleapis.com/" rel="dns-prefetch">
<link href="//fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic|Abril+Fatface|PT+Sans:400,400italic,700&subset=latin,latin-ext" rel="stylesheet">
<link rel="stylesheet" href="/theme/css/poole.css" />
<link rel="stylesheet" href="/theme/css/hyde.css" />
<link rel="stylesheet" href="/theme/css/syntax.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fork-awesome@1.1.7/css/fork-awesome.min.css" crossorigin="anonymous">
<!-- Feeds -->
<!-- Analytics -->
</head>
<body class="theme-base-0c">
<div class="sidebar">
<div class="container sidebar-sticky">
<div class="sidebar-about">
<h1>
<a href="/">
<img class="profile-picture" src="/images/avatar.png">
mhalil
</a>
</h1>
<p class="lead"></p>
<p class="lead">3D Modelleme, Animasyon, Render, Vektör - Raster Grafik İşleme ve Python ile Programlama konularına meraklı Açık Kaynak Hayranı bir Makine Mühendisi </p>
<p></p>
</div>
<ul class="sidebar-nav">
<li><a href="/archives.html">Arşiv</a></li>
<li><a href="/categories.html">Kategoriler</a></li>
<li><a href="/tags.html">Etiketler</a></li>
<li><a href="/pages/blender.html">Blender</a></li>
<li><a href="/pages/freecad.html">FreeCAD</a></li>
<li><a href="/pages/librecad.html">LibreCAD</a></li>
<li><a href="/pages/python.html">Python</a></li>
<li><a href="/pages/solvespace.html">Solvespace</a></li>
</ul>
<nav class="sidebar-social">
<a class="sidebar-social-item" href="https://twitter.com/AcikKaynakci" target="_blank">
<i class="fa fa-twitter"></i>
</a>
<a class="sidebar-social-item" href="https://www.artstation.com/mustafahalil" target="_blank">
<i class="fa fa-artstation"></i>
</a>
<a class="sidebar-social-item" href="https://github.com/mhalil" target="_blank">
<i class="fa fa-github"></i>
</a>
<a class="sidebar-social-item" href="/">
<i class="fa fa-rss"></i>
</a>
</nav>
<p class="sidebar-footer">İlmin zekatı %100'dür. Bildiklerizi paylaşın.</p>
</div>
</div> <div class="content container">
<div class="post">
<h1 class="post-title">Flet 04 - Kullanıcıdan Girdi Almak</h1>
<span class="post-date">Paz 11 Şubat 2024</span>
<h1>Kullanıcıdan girdi almak</h1>
<p><strong>Flet</strong> ile etkileşimli web uygulamaları yapmak çocuk oyuncağı! Bu yalnızca verileri görüntülemekle sınırlı değildir, bir kullanıcıdan girdi talep edebilir ve sayfa kontrolleri tarafından oluşturulan çeşitli olaylara yanıt verebilirsiniz.</p>
<h2>Butonlar / Düğmeler</h2>
<p><code>Button</code> (Düğme), basıldığında oluşturan <code>click</code> (tıklama) olayı, en önemli girdi kontrolüdür:</p>
<div class="highlight"><pre><span></span><code><span class="n">btn</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">ElevatedButton</span><span class="p">(</span><span class="s2">"Click me!"</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">btn</span><span class="p">)</span>
</code></pre></div>
<p><img alt="buton" src="https://flet.dev/img/docs/getting-started/getting-user-input-elevated-button.png"></p>
<p>Bir web sayfasındaki kontroller tarafından oluşturulan tüm olaylar, sürekli olarak komut dosyanıza geri gönderilir, peki bir düğme tıklamasına nasıl yanıt verirsiniz?</p>
<h2>Olay (Etkinlik) İşleyicileri</h2>
<p>"Sayaç (Counter)" uygulamasındaki etkinlikleri içeren düğmeler:</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">flet</span> <span class="k">as</span> <span class="nn">ft</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">page</span><span class="p">:</span> <span class="n">ft</span><span class="o">.</span><span class="n">Page</span><span class="p">):</span>
<span class="n">page</span><span class="o">.</span><span class="n">title</span> <span class="o">=</span> <span class="s2">"Flet counter example"</span>
<span class="n">page</span><span class="o">.</span><span class="n">vertical_alignment</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">MainAxisAlignment</span><span class="o">.</span><span class="n">CENTER</span>
<span class="n">txt_number</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">TextField</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="s2">"0"</span><span class="p">,</span> <span class="n">text_align</span><span class="o">=</span><span class="s2">"right"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">minus_click</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="n">txt_number</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">txt_number</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">plus_click</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="n">txt_number</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">txt_number</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
<span class="n">page</span><span class="o">.</span><span class="n">add</span><span class="p">(</span>
<span class="n">ft</span><span class="o">.</span><span class="n">Row</span><span class="p">(</span>
<span class="p">[</span>
<span class="n">ft</span><span class="o">.</span><span class="n">IconButton</span><span class="p">(</span><span class="n">ft</span><span class="o">.</span><span class="n">icons</span><span class="o">.</span><span class="n">REMOVE</span><span class="p">,</span> <span class="n">on_click</span><span class="o">=</span><span class="n">minus_click</span><span class="p">),</span>
<span class="n">txt_number</span><span class="p">,</span>
<span class="n">ft</span><span class="o">.</span><span class="n">IconButton</span><span class="p">(</span><span class="n">ft</span><span class="o">.</span><span class="n">icons</span><span class="o">.</span><span class="n">ADD</span><span class="p">,</span> <span class="n">on_click</span><span class="o">=</span><span class="n">plus_click</span><span class="p">),</span>
<span class="p">],</span>
<span class="n">alignment</span><span class="o">=</span><span class="n">ft</span><span class="o">.</span><span class="n">MainAxisAlignment</span><span class="o">.</span><span class="n">CENTER</span><span class="p">,</span>
<span class="p">)</span>
<span class="p">)</span>
<span class="n">ft</span><span class="o">.</span><span class="n">app</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">main</span><span class="p">)</span>
</code></pre></div>
<p><img alt="sayac" src="https://flet.dev/img/docs/getting-started/getting-user-input-event-handlers.png"></p>
<h2>Metin kutusu (Textbox)</h2>
<p><strong>Flet</strong>, form oluşturmak için bir dizi <a href="https://flet.dev/docs/controls">kontrol</a> sağlar: <a href="https://flet.dev/docs/controls/textfield">TextField (Metin Alanı)</a>, <a href="https://flet.dev/docs/controls/checkbox">Checkbox (Onay Kutusu)</a>, <a href="https://flet.dev/docs/controls/dropdown">Dropdown (Açılır Menü)</a>, <a href="https://flet.dev/docs/controls/elevatedbutton">ElevatedButton (Yükseltilmiş Düğme)</a>.</p>
<p>Kullanıcıdan bir isim isteyelim:</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">flet</span> <span class="k">as</span> <span class="nn">ft</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">page</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">btn_click</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">txt_name</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
<span class="n">txt_name</span><span class="o">.</span><span class="n">error_text</span> <span class="o">=</span> <span class="s2">"Please enter your name"</span>
<span class="n">page</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">txt_name</span><span class="o">.</span><span class="n">value</span>
<span class="n">page</span><span class="o">.</span><span class="n">clean</span><span class="p">()</span>
<span class="n">page</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ft</span><span class="o">.</span><span class="n">Text</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Hello, </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">!"</span><span class="p">))</span>
<span class="n">txt_name</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">TextField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s2">"Your name"</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">txt_name</span><span class="p">,</span> <span class="n">ft</span><span class="o">.</span><span class="n">ElevatedButton</span><span class="p">(</span><span class="s2">"Say hello!"</span><span class="p">,</span> <span class="n">on_click</span><span class="o">=</span><span class="n">btn_click</span><span class="p">))</span>
<span class="n">ft</span><span class="o">.</span><span class="n">app</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">main</span><span class="p">)</span>
</code></pre></div>
<p><img alt="metin_kutusu" src="https://flet.dev/img/docs/getting-started/getting-user-input-textbox.png"></p>
<h2>Onay Kutusu (Checkbox)</h2>
<p><a href="https://flet.dev/docs/controls/checkbox">Onay Kutusu (Checkbox)</a> kontrolü, kullanım kolaylığı için size çeşitli özellikler ve olay yayıcılar sağlar.</p>
<p>Bir tek onay kutusu içeren, <strong>Yapılacaklar (ToDo)</strong> uygulaması oluşturalım:</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">flet</span> <span class="k">as</span> <span class="nn">ft</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">page</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">checkbox_changed</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="n">output_text</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="p">(</span>
<span class="sa">f</span><span class="s2">"You have learned how to ski : </span><span class="si">{</span><span class="n">todo_check</span><span class="o">.</span><span class="n">value</span><span class="si">}</span><span class="s2">."</span>
<span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
<span class="n">output_text</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">Text</span><span class="p">()</span>
<span class="n">todo_check</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">Checkbox</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s2">"ToDo: Learn how to use ski"</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">on_change</span><span class="o">=</span><span class="n">checkbox_changed</span><span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">todo_check</span><span class="p">,</span> <span class="n">output_text</span><span class="p">)</span>
<span class="n">ft</span><span class="o">.</span><span class="n">app</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">main</span><span class="p">)</span>
</code></pre></div>
<p><img alt="onay_kutusu" src="https://flet.dev/img/docs/getting-started/getting-user-input-checkbox.png"></p>
<h2>Açılır Menü (Dropdown)</h2>
<p>İçerisinde <strong>KIRMIZI</strong>, <strong>YEŞİL</strong> ve <strong>MAVİ</strong> seçenekleri bulunan bir açılır menü oluşturalım:</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">flet</span> <span class="k">as</span> <span class="nn">ft</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">page</span><span class="p">:</span> <span class="n">ft</span><span class="o">.</span><span class="n">Page</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">button_clicked</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
<span class="n">output_text</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"Dropdown value is: </span><span class="si">{</span><span class="n">color_dropdown</span><span class="o">.</span><span class="n">value</span><span class="si">}</span><span class="s2">"</span>
<span class="n">page</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
<span class="n">output_text</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">Text</span><span class="p">()</span>
<span class="n">submit_btn</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">ElevatedButton</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">"Submit"</span><span class="p">,</span> <span class="n">on_click</span><span class="o">=</span><span class="n">button_clicked</span><span class="p">)</span>
<span class="n">color_dropdown</span> <span class="o">=</span> <span class="n">ft</span><span class="o">.</span><span class="n">Dropdown</span><span class="p">(</span>
<span class="n">width</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">options</span><span class="o">=</span><span class="p">[</span>
<span class="n">ft</span><span class="o">.</span><span class="n">dropdown</span><span class="o">.</span><span class="n">Option</span><span class="p">(</span><span class="s2">"KIRMIZI"</span><span class="p">),</span>
<span class="n">ft</span><span class="o">.</span><span class="n">dropdown</span><span class="o">.</span><span class="n">Option</span><span class="p">(</span><span class="s2">"YEŞİL"</span><span class="p">),</span>
<span class="n">ft</span><span class="o">.</span><span class="n">dropdown</span><span class="o">.</span><span class="n">Option</span><span class="p">(</span><span class="s2">"MAVİ"</span><span class="p">),</span>
<span class="p">],</span>
<span class="p">)</span>
<span class="n">page</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">color_dropdown</span><span class="p">,</span> <span class="n">submit_btn</span><span class="p">,</span> <span class="n">output_text</span><span class="p">)</span>
<span class="n">ft</span><span class="o">.</span><span class="n">app</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">main</span><span class="p">)</span>
</code></pre></div>
<p><img alt="acilir_menu" src="https://flet.dev/img/docs/getting-started/getting-user-input-dropdown.png"></p>
<table>
<thead>
<tr>
<th>Önceki Bölüm</th>
<th>Sonraki Bölüm</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="flet-03-python-rehberi.html"><<< 03 Python Rehberi</a></td>
<td><a href="flet-05-klavye-kisayollari.html">05 Klavye Kısayolları >>></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>