· 6 years ago · Sep 18, 2019, 04:26 PM
1
2 import tkinter
3 top = tkinter.Tk()
4 # Code to add widgets will go here...
5 top.mainloop()
6
7 ## Tkinter Widgets
8 Tkinter provides various controls such as buttons labels and text boxes used in a GUI application. These controls are commonly called widgets.
9
10
11 There are currently 15 types of widgets in Tkinter. Few of them are as follow:
12
13 * Message Box
14
15 * Checkbox
16
17 * Button
18
19 * Radiobutton etc.
20
21 ## Standard attributes
22
23 Widgtes have some of their common attributes such as sizes colors and fonts are specified.
24
25 * Dimensions
26
27 * Colors
28
29 * Fonts
30
31 * Anchors
32
33 * Relief styles
34
35 * Bitmaps
36
37 * Cursors
38
39
40 ## Geometry Management
41
42 All Tkinter widgets have access to specific geometry management methods which have the purpose of organizing widgets throughout the parent widget area. Tkinter exposes the following geometry manager classes: pack grid and place.
43
44 * The pack() Method − This geometry manager organizes widgets in blocks before placing them in the parent widget.
45
46 * The grid() Method − This geometry manager organizes widgets in a table-like structure in the parent widget.
47
48 * The place() Method − This geometry manager organizes widgets by placing them in a specific position in the parent widget.
49
50 ## Message Box
51
52 This module is used to display message boxes in your applications.
53 cell_type: code
54 execution_count: 4
55 metadata: {}
56 outputs: []
57
58 import tkinter
59 import tkinter.messagebox
60
61
62 top = tkinter.Tk()
63 def hello():
64 tkMessageBox.showinfo(\Hello Python\ \Hello World\)
65
66 B1 = tkinter.Button(top text = \Say Hello\ command = hello)
67 B1.pack()
68
69 top.mainloop()
70
71 ## Canvas
72
73 The Canvas widget is used to draw shapes such as lines ovals polygons and rectangles in your application.
74 cell_type: code
75 execution_count: 6
76 metadata: {}
77 outputs: []
78
79 import tkinter
80 from tkinter import *
81
82 top = tkinter.Tk()
83
84 C = tkinter.Canvas(top bg=\blue\ height=250 width=300)
85 coord = 10 50 240 210
86 arc = C.create_arc(coord start=0 extent=150 fill=\red\)
87 C.pack()
88 top.mainloop()
89
90 ## Label
91
92 The Label widget is used to provide a single-line caption for other widgets. It can also contain images.
93 cell_type: code
94 execution_count: 7
95 metadata: {}
96 outputs: []
97
98 from tkinter import *
99
100 top = tkinter.Tk()
101 L1 = Label(top text=\User Name\)
102 L1.pack( side = LEFT)
103 E1 = Entry(top bd =5)
104 E1.pack(side = RIGHT)
105
106 top.mainloop()
107
108 ## Frame and Button
109
110 The Frame widget is used as a container widget to organize other widgets.
111
112 The Button widget is used to display buttons in your application.
113 cell_type: code
114 execution_count: 5
115 metadata: {}
116 outputs: []
117
118 from tkinter import *
119
120 root = Tk()
121 frame = Frame(root)
122 frame.pack()
123
124 bottomframe = Frame(root)
125 bottomframe.pack( side = BOTTOM )
126
127 redbutton = Button(frame text=\Red\ fg=\red\)
128 redbutton.pack( side = LEFT)
129
130 greenbutton = Button(frame text=\Brown\ fg=\brown\)
131 greenbutton.pack( side = LEFT )
132
133 bluebutton = Button(frame text=\Blue\ fg=\blue\)
134 bluebutton.pack( side = LEFT )
135
136 blackbutton = Button(bottomframe text=\Black\ fg=\black\)
137 blackbutton.pack( side = BOTTOM)
138
139 root.mainloop()
140
141 ## List Box
142
143 The Listbox widget is used to provide a list of options to a user.
144 cell_type: code
145 execution_count: 8
146 metadata: {}
147 outputs: []
148
149 from tkinter import *
150 #import tkMessageBox
151 #import Tkinter
152
153 top = Tk()
154
155 Lb1 = Listbox(top)
156 Lb1.insert(1 \Python\)
157 Lb1.insert(2 \Perl\)
158 Lb1.insert(3 \C\)
159 Lb1.insert(4 \PHP\)
160 Lb1.insert(5 \JSP\)
161 Lb1.insert(6 \Ruby\)
162
163 Lb1.pack()
164 top.mainloop()
165
166 ## Radio Button
167
168 The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time.
169 cell_type: code
170 execution_count: 9
171 metadata: {}
172 outputs: []
173
174 from tkinter import *
175
176 def sel():
177 selection = \You selected the option \ + str(var.get())
178 label.config(text = selection)
179
180 root = Tk()
181 var = IntVar()
182 R1 = Radiobutton(root text=\Option 1\ variable=var value=1
183 command=sel)
184 R1.pack( anchor = W )
185
186 R2 = Radiobutton(root text=\Option 2\ variable=var value=2
187 command=sel)
188 R2.pack( anchor = W )
189
190 R3 = Radiobutton(root text=\Option 3\ variable=var value=3
191 command=sel)
192 R3.pack( anchor = W)
193
194 label = Label(root)
195 label.pack()
196 root.mainloop()
197
198 ## Menu Button
199
200 The Menubutton widget is used to display menus in your application.
201 cell_type: code
202 execution_count: 11
203 metadata: {}
204 outputs: []
205
206 from tkinter import *
207
208
209 top = Tk()
210
211 mb= Menubutton ( top text=\condiments\ relief=RAISED )
212 mb.grid()
213 mb.menu = Menu ( mb tearoff = 0 )
214 mb[\menu\] = mb.menu
215
216 mayoVar = IntVar()
217 ketchVar = IntVar()
218
219 mb.menu.add_checkbutton ( label=\mayo\
220 variable=mayoVar )
221 mb.menu.add_checkbutton ( label=\ketchup\
222 variable=ketchVar )
223
224 mb.pack()
225 top.mainloop()
226
227 ## Check Button
228
229 The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time.
230 cell_type: code
231 execution_count: 13
232 metadata: {}
233 outputs: []
234
235 from tkinter import *
236
237
238 top = tkinter.Tk()
239 CheckVar1 = IntVar()
240 CheckVar2 = IntVar()
241 C1 = Checkbutton(top text = \Music\ variable = CheckVar1onvalue = 1 offvalue = 0 height=5width = 20)
242 C2 = Checkbutton(top text = \Video\ variable = CheckVar2
243 onvalue = 1 offvalue = 0 height=0
244 width = 0)
245 C1.pack()
246 C2.pack()
247 top.mainloop()
248
249 ## Scale
250
251 The Scale widget provides a graphical slider object that allows you to select values from a specific scale.
252 cell_type: code
253 execution_count: 19
254 metadata: {}
255 outputs: []
256
257 from tkinter import *
258
259 def sel():
260 selection = \Value = \ + str(var.get())
261 label.config(text = selection)
262
263 root = Tk()
264 var = DoubleVar()
265 scale = Scale( root variable = var )
266 scale.pack(anchor=CENTER)
267
268 button = Button(root text=\Get Scale Value\ command=sel)
269 button.pack(anchor=CENTER)
270
271 label = Label(root)
272 label.pack()
273
274 root.mainloop()
275
276 ## Spinbox
277
278 The Spinbox widget is a variant of the standard Tkinter Entry widget which can be used to select from a fixed number of values.
279 cell_type: code
280 execution_count: 21
281 metadata: {}
282 outputs: []
283
284 from tkinter import *
285
286 master = Tk()
287
288 w = Spinbox(master from_=0 to=10)
289 w.pack()
290
291 mainloop()
292
293 ## SQLite DB
294
295 SQLite in general is a server-less database that can be used within almost all programming languages including Python. Server-less means there is no need to install a separate server to work with SQLite so we can connect directly with the database.
296 cell_type: code
297 execution_count: 14
298 metadata: {}
299 outputs: []
300
301 import sqlite3
302 conn = sqlite3.connect('example.db')
303 c = conn.cursor()
304 # Create table
305 c.execute('''CREATE TABLE student (name text address text age real mobileno text)''')
306 # Insert a row of data
307 c.execute(\INSERT INTO student VALUES ( 'sanjay''lpu'29'959241')\)
308 # Save (commit) the changes
309 conn.commit()
310 # We can also close the connection if we are done with it.
311 # Just be sure any changes have been committed or they will be lost.
312 conn.close()
313 cell_type: code
314 execution_count: 16
315 metadata: {}
316 outputs: [
317 {
318 name: stdout
319 output_type: stream
320 text: [
321 ('sanjay' 'lpu' 29.0 '959241')
322 ]
323 }
324 ]
325
326 import sqlite3
327 conn = sqlite3.connect('example.db')
328 c = conn.cursor()
329 c.execute('SELECT * FROM student')
330 print(c.fetchone())
331 cell_type: code
332 execution_count: 18
333 metadata: {
334 scrolled: true
335 }
336 outputs: [
337 {
338 name: stdout
339 output_type: stream
340 text: [
341 ('sanjay' 'lpu' 29.0 '959241')
342 ('abc' 'add1' 25.0 '23456')
343 ('abc' 'add1' 25.0 '23456')
344 ('abc' 'add1' 25.0 '23456')
345 ]
346 }
347 ]
348
349 import sqlite3
350 conn = sqlite3.connect('example.db')
351 c = conn.cursor()
352 list1 = [('abc''add1'25'23456')
353 ('abc''add1'25'23456')
354 ('abc''add1'25'23456')
355 ]
356 c.executemany('INSERT INTO student VALUES (????)' list1)
357
358 for row in c.execute('SELECT * FROM student'):
359 print (row)
360 cell_type: code
361 execution_count: 2
362 metadata: {}
363 outputs: [
364 {
365 name: stdout
366 output_type: stream
367 text: [
368 [('rth' 'pass@123') ('asdfg' 'pass@123') ('dfgh' 'pass@123')]
369 ]
370 }
371 ]
372
373 def func():
374 a=E1.get()
375 import sqlite3
376 conn = sqlite3.connect('ex.db')
377 c = conn.cursor()
378 c.execute('CREATE TABLE IF NOT EXISTS user(uname text password text)')
379 c.execute(\INSERT INTO user VALUES (??)\(a'pass@123'))
380 conn.commit()
381 c.execute('SELECT * FROM user')
382 z=c.fetchall()
383 print(z)
384 L2.config(text=\Username is \+z[-1][0]+\\Password is \+z[-1][1])
385 conn.close()
386 from tkinter import *
387 top = Tk()
388 L1 = Label(top text=\User Name\)
389 L1.pack()
390 E1 = Entry(top bd =5)
391 E1.pack()
392 B=Button(top text=\get value\ command=func)
393 B.pack()
394 L2=Label(top)
395 L2.pack()
396 top.mainloop()