-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
484 lines (424 loc) · 17 KB
/
notes.txt
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
Python has a lot of GUI frameworks, but Tkinter is the only framework that’s
built into the Python standard library. Tkinter has several strengths. It’s
cross-platform, so the same code works on Windows, macOS, and Linux. Visual
elements are rendered using native operating system elements, so applications
built with Tkinter look like they belong on the platform where they’re run.
Although Tkinter is considered the de facto Python GUI framework, it’s not
without criticism. One notable criticism is that GUIs built with Tkinter look
outdated. If you want a shiny, modern interface, then Tkinter may not be what
you’re looking for.
However, Tkinter is lightweight and relatively painless to use compared to other
frameworks. This makes it a compelling choice for building GUI applications in
Python, especially for applications where a modern sheen is unnecessary, and the
top priority is to quickly build something that’s functional and cross-platform.
Building Your First Python GUI Application With Tkinter
The foundational element of a Tkinter GUI is the window. Windows are the containers
in which all other GUI elements live. These other GUI elements, such as text boxes,
labels, and buttons, are known as widgets. Widgets are contained inside of windows.
First, create a window that contains a single widget.
With your Python shell open, the first thing you need to do is import the Python
GUI Tkinter module:
>>> import tkinter as tk
A window is an instance of Tkinter’s Tk class. Go ahead and create a new window and
assign it to the variable window:
>>> window = tk.Tk()
Adding a Widget
Now that you have a window, you can add a widget. Use the tk.Label class to add some
text to a window. Create a Label widget with the text "Hello, Tkinter" and assign it
to a variable called greeting:
>>> greeting = tk.Label(text="Hello, Tkinter")
The window you created earlier doesn’t change. You just created a Label widget,
but you haven’t added it to the window yet. There are several ways to add widgets to
a window. Right now, you can use the Label widget’s .pack() method:
>>> greeting.pack()
When you pack a widget into a window, Tkinter sizes the window as small as it
can be while still fully encompassing the widget. Now execute the following:
>>> window.mainloop()
Nothing seems to happen, but notice that no new prompt appears in the shell.
window.mainloop() tells Python to run the Tkinter event loop. This method listens for
events, such as button clicks or keypresses, and blocks any code that comes after it
from running until you close the window where you called the method. Go ahead and
close the window you’ve created, and you’ll see a new prompt displayed in the shell.
Check Your Understanding
Expand the code blocks below to check your understanding:
Write a full Python script that creates a Tkinter window with the text "Python rocks!".
Here’s one possible solution:
import tkinter as tk
window = tk.Tk()
label = tk.Label(text="Python rocks!")
label.pack()
window.mainloop()
Keep in mind your code may look different
BUTTONS
The Button widget is used to add buttons in a Python application.
These buttons can display
text or images that convey the purpose of the buttons.
You can attach a function or a method
to a button which is called automatically when you click the button.
Syntax
Here is the simple syntax to create this widget −
w = Button (master, option=value, ... )
Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget.
These options
can be used as key-value pairs separated by commas.
1 activebackground
Background color when the button is under the cursor.
2 activeforeground
Foreground color when the button is under the cursor.
3 bd
Border width in pixels. Default is 2.
4 bg
Normal background color.
5 command
Function or method to be called when the button is clicked.
6 fg
Normal foreground (text) color.
7 font
Text font to be used for the button's label.
8 height
Height of the button in text lines (for textual buttons) or pixels
(for images).
9 highlightcolor
The color of the focus highlight when the widget has focus.
10 image
Image to be displayed on the button (instead of text).
11 justify
How to show multiple text lines: LEFT to left-justify each line;
CENTER to center them; or RIGHT to right-justify.
12 padx
Additional padding left and right of the text.
13 pady
Additional padding above and below the text.
14 relief
Relief specifies the type of the border. Some of the values are
SUNKEN, RAISED, GROOVE, and RIDGE.
15 state
Set this option to DISABLED to gray out the button and make it unresponsive.
Has the value ACTIVE when the mouse is over it. Default is NORMAL.
16 underline
Default is -1, meaning that no character of the text on the button
will be underlined. If nonnegative, the corresponding text character will
be underlined.
17 width
Width of the button in letters (if displaying text) or pixels (if
displaying an image).
18 wraplength
If this value is set to a positive number, the text lines will be wrapped to
fit within this length.
Methods
Following are commonly used methods for this widget −
Method & Description
1 flash()
Causes the button to flash several times between active and normal colors.
Leaves the button in the state it was in originally. Ignored if the button is disabled.
2 invoke()
Calls the button's callback, and returns what that function returns.
Has no effect if the button is disabled or there is no callback.
Example
Try the following example yourself −
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def helloCallBack():
tkMessageBox.showinfo( "Hello Python", "Hello World")
B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
Python - Tkinter Entry
The Entry widget is used to accept single-line text strings from a user.
If you want to display multiple lines of text that can be edited, then
you should use the Text widget.
If you want to display one or more lines of text that cannot be modified
by the user, then you should use the Label widget.
Syntax
Here is the simple syntax to create this widget −
w = Entry( master, option, ... )
Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget.
These options can be used as key-value pairs separated by commas.
Option & Description
1 bg
The normal background color displayed behind the label and indicator.
2 bd
The size of the border around the indicator. Default is 2 pixels.
3 command
A procedure to be called every time the user changes the state of this checkbutton.
4 cursor
If you set this option to a cursor name (arrow, dot etc.), the mouse cursor
will change to that pattern when it is over the checkbutton.
5 font
The font used for the text.
6 exportselection
By default, if you select text within an Entry widget, it is automatically
exported to the clipboard. To avoid this exportation, use exportselection=0.
7 fg
The color used to render the text.
8 highlightcolor
The color of the focus highlight when the checkbutton has the focus.
9 justify
If the text contains multiple lines, this option controls how the text is
justified: CENTER, LEFT, or RIGHT.
10 relief
With the default value, relief=FLAT, the checkbutton does not stand out from
its background. You may set this option to any of the other styles
11 selectbackground
The background color to use displaying selected text.
12 selectborderwidth
The width of the border to use around selected text. The default is one pixel
13 selectforeground
The foreground (text) color of selected text.
14 show
Normally, the characters that the user types appear in the entry.
To make a .password. entry that echoes each character as an asterisk, set show="*".
15 state
The default is state=NORMAL, but you can use state=DISABLED to gray out the control
and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
16 textvariable
In order to be able to retrieve the current text from your entry widget,
you must set this option to an instance of the StringVar class.
17 width
The default width of a checkbutton is determined by the size of the displayed
image or text. You can set this option to a number of characters and the checkbutton
will always have room for that many characters.
18 xscrollcommand
If you expect that users will often enter more text than the onscreen size of
the widget, you can link your entry widget to a scrollbar.
Methods
Following are commonly used methods for this widget −
Method & Description
1 delete ( first, last=None )
Deletes characters from the widget, starting with the one at index first,
up to but not including the character at position last. If the second argument is
omitted, only the single character at position first is deleted.
2 get()
Returns the entry's current text as a string.
3 icursor ( index )
Set the insertion cursor just before the character at the given index.
4 index ( index )
Shift the contents of the entry so that the character at the given index is
the leftmost visible character. Has no effect if the text fits entirely within the entry.
5 insert ( index, s )
Inserts string s before the character at the given index.
6 select_adjust ( index )
This method is used to make sure that the selection includes the character at the specified index.
7 select_clear()
Clears the selection. If there isn't currently a selection, has no effect.
8 select_from ( index )
Sets the ANCHOR index position to the character selected by index, and selects that character.
9 select_present()
If there is a selection, returns true, else returns false.
10 select_range ( start, end )
Sets the selection under program control. Selects the text starting at the start index,
up to but not including the character at the end index. The start position must be before the end position.
11 select_to ( index )
Selects all the text from the ANCHOR position up to but not including the character at the given index.
12 xview ( index )
This method is useful in linking the Entry widget to a horizontal scrollbar.
13 xview_scroll ( number, what )
Used to scroll the entry horizontally. The what argument must be either UNITS, to scroll by
character widths, or PAGES, to scroll by chunks the size of the entry widget. The number is positive to
scroll left to right, negative to scroll right to left.
Example
Try the following example yourself −
from Tkinter import *
top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
top.mainloop()
The Checkbutton widget is used to display a number of options to a user as
toggle buttons. The user can then select one or more options by clicking the
button corresponding to each option.
Syntax
Here is the simple syntax to create this widget −
w = Checkbutton ( master, option, ... )
Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget.
These options can be used as key-value pairs separated by commas.
1
activebackground
Background color when the checkbutton is under the cursor.
2
activeforeground
Foreground color when the checkbutton is under the cursor.
3
bg
The normal background color displayed behind the label and indicator.
4
bitmap
To display a monochrome image on a button.
5
bd
The size of the border around the indicator. Default is 2 pixels.
6
command
A procedure to be called every time the user changes the state of this checkbutton.
7
cursor
If you set this option to a cursor name (arrow, dot etc.), the mouse cursor
will change to that pattern when it is over the checkbutton.
8
disabledforeground
The foreground color used to render the text of a disabled checkbutton.
The default is a stippled version of the default foreground color.
9
font
The font used for the text.
10
fg
The color used to render the text.
11
height
The number of lines of text on the checkbutton. Default is 1.
12
highlightcolor
The color of the focus highlight when the checkbutton has the focus.
13
image
To display a graphic image on the button.
14
justify
If the text contains multiple lines, this option controls how the
text is justified: CENTER, LEFT, or RIGHT.
15
offvalue
Normally, a checkbutton's associated control variable will be set to 0 when it is cleared (off).
You can supply an alternate value for the off state by setting offvalue to that value.
16
onvalue
Normally, a checkbutton's associated control variable will be set to 1
when it is set (on). You can supply an alternate value for the on state by
setting onvalue to that value.
17
padx
How much space to leave to the left and right of the checkbutton and text.
Default is 1 pixel.
18
pady
How much space to leave above and below the checkbutton and text. Default is 1 pixel.
19
relief
With the default value, relief=FLAT, the checkbutton does not stand out
from its background. You may set this option to any of the other styles
20
selectcolor
The color of the checkbutton when it is set. Default is selectcolor="red".
21
selectimage
If you set this option to an image, that image will appear in the checkbutton
when it is set.
22
state
The default is state=NORMAL, but you can use state=DISABLED to gray out
the control and make it unresponsive. If the cursor is currently over the
checkbutton, the state is ACTIVE.
23
text
The label displayed next to the checkbutton. Use newlines ("\n") to
display multiple lines of text.
24
underline
With the default value of -1, none of the characters of the text label
are underlined. Set this option to the index of a character in the text
(counting from zero) to underline that character.
25
variable
The control variable that tracks the current state of the checkbutton.
Normally this variable is an IntVar, and 0 means cleared and 1 means set,
but see the offvalue and onvalue options above.
26
width
The default width of a checkbutton is determined by the size of the displayed
image or text. You can set this option to a number of characters and the
checkbutton will always have room for that many characters.
27
wraplength
Normally, lines are not wrapped. You can set this option to a number of
characters and all lines will be broken into pieces no longer than that number.
The Frame widget is very important for the process of grouping and
organizing other widgets in a somehow friendly way. It works like a container,
which is responsible for arranging the position of other widgets.
It uses rectangular areas in the screen to organize the layout and to
provide padding of these widgets. A frame can also be used as a foundation
class to implement complex widgets.
Syntax
Here is the simple syntax to create this widget −
w = Frame ( master, option, ... )
Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget.
These options can be used as key-value pairs separated by commas.
Option & Description
1
bg
The normal background color displayed behind the label and indicator.
2
bd
The size of the border around the indicator. Default is 2 pixels.
3
cursor
If you set this option to a cursor name (arrow, dot etc.), the mouse
cursor will change to that pattern when it is over the checkbutton.
4
height
The vertical dimension of the new frame.
5
highlightbackground
Color of the focus highlight when the frame does not have focus.
6
highlightcolor
Color shown in the focus highlight when the frame has the focus.
7
highlightthickness
Thickness of the focus highlight.
8
relief
With the default value, relief=FLAT, the checkbutton does not stand out from its
background. You may set this option to any of the other styles
9
width
The default width of a checkbutton is determined by the size of the displayed image or
text. You can set this option to a number of characters and the checkbutton will
always have room for that many characters.
Message box
The tkMessageBox module is used to display message boxes in your applications.
This module provides a number of functions that you can use to display an
appropriate message.
Some of these functions are showinfo, showwarning, showerror, askquestion,
askokcancel, askyesno, and askretryignore.
Syntax
Here is the simple syntax to create this widget −
tkMessageBox.FunctionName(title, message [, options])
Parameters
FunctionName − This is the name of the appropriate message box function.
title − This is the text to be displayed in the title bar of a message box.
message − This is the text to be displayed as a message.
options − options are alternative choices that you may use to tailor a standard
message box. Some of the options that you can use are default and parent.
The default option is used to specify the default button, such as ABORT, RETRY,
or IGNORE in the message box.
The parent option is used to specify the window on top of which the message box
is to be displayed.
You could use one of the following functions with dialogue box −
showinfo()
showwarning()
showerror ()
askquestion()
askokcancel()
askyesno ()
askretrycancel ()
Example
Try the following example yourself −
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def hello():
tkMessageBox.showinfo("Say Hello", "Hello World")
B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()
top.mainloop()