A comprehensive Introduction to Python Programming and gui design Using Tkinter
Download 257.92 Kb. Pdf ko'rish
|
python-intro
- Bu sahifa navigatsiya:
- Contents 2 / 75
- A Tkinter color names 55 1
- 1.2 Language Features 4 / 75
- 1.2.1.2 Floats
- 1.2.1.3 Strings
- 1.2 Language Features 5 / 75 Sequence Description
- 1.2.1.4 Imaginary Numbers
- 1.2.2 Variables
- 1.2 Language Features 6 / 75
- 1.2.2.1 Private Instance Variables
- 1.2.3 Operators
- 1.2 Language Features 7 / 75 1.2.3.2 Binary Operators Operator Description
- 1.2.4 Data Structures
- 1.2 Language Features 8 / 75 1.2.4.1 Lists
- 1.2 Language Features 9 / 75
- 1.2.4.2 Tuples
- 1.2 Language Features 10 / 75
- 1.2.4.3 Dictionaries
A Comprehensive Introduction to Python Programming and GUI Design Using Tkinter Bruno Dufour McGill Univeristy SOCS
Contents 1 Overview of the Python Language 2 1.1
Main Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2
Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2.1
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2.2
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2.3
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2.4
Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2.5
Control Structures and the range( ) Function . . . . . . . . 10 1.2.6
Function Definitions and Function Calls . . . . . . . . . . . . 12 1.2.7
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1
Designing User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . 16 2.2
What is Tkinter? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3
Why Tkinter? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.4
Nothing is perfect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5
Show me what you can do! . . . . . . . . . . . . . . . . . . . . . . . 18
3.1
What is a Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.2
The Different Widgets . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.2.1
Toplevel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.2.2
Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2.3
Checkbutton . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2.4
Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.5
Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.6
Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2.7
Listbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2.8
Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.2.9
Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.2.10 OptionMenu . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.2.11 Radiobutton . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.2.12 Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.2.13 Scrollbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.2.14 Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.2.15 Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Contents 2 / 75 3.3
Additional Widgets: the Python Mega Widgets (PMW) . . . . . . . . 37
4.1
What is Geometry Management? . . . . . . . . . . . . . . . . . . . . 38 4.2
The “Pack” Geometry Manager . . . . . . . . . . . . . . . . . . . . . 38 4.3
The “Grid” Geometry Manager . . . . . . . . . . . . . . . . . . . . . 40 4.4
The “Place” Geometry Manager . . . . . . . . . . . . . . . . . . . . 41
5.1
Specifying colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.2
Specifying Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.3
Tkinter Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.3.1
The Need for New Variables . . . . . . . . . . . . . . . . . . 45 5.3.2
Using Tkinter Variables . . . . . . . . . . . . . . . . . . . . 45
6.1
The Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 6.2
Event Types and Properties . . . . . . . . . . . . . . . . . . . . . . . 49 6.3
Event Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.4
Binding Callbacks to Events . . . . . . . . . . . . . . . . . . . . . . 52 6.5
Unit testing and Graphical User Interfaces: Simulating Events . . . .
53 A Tkinter color names 55 1 Overview of the Python Language 1.1 Main Features Python is an interpreted, interactive, object-oriented high-level language. Its syntax resembles pseudo-code, especially because of the fact that indentation is used to in- dentify blocks. Python is a dynamcally typed language, and does not require variables to be declared before they are used. Variables “appear” when they are first used and “disappear” when they are no longer needed. Python is a scripting language like Tcl and Perl. Because of its interpreted nature, it is also often compared to Java. Unlike Java, Python does not require all instructions to reside inside classes. Python is also a multi-platform language, since the Python interpreter is available for a large number of standard operating systems, including MacOS, UNIX, and Mi- crosoft Windows. Python interpreters are usually written in C, and thus can be ported to almost any platform which has a C compiler.
There are 3 kinds of integer literals: Decimal integers : integers not starting with a ’0’ digit or the integer 0 (eg: 205). Octal integers : integers which have a leading 0 as a prefix (eg: 0205). 1.2 Language Features 4 / 75 Hexadecimal integers : integers which have 0x (or 0X) as a prefix (eg: 0x00FF0205). Standard integers occupy 32 bits of memory, but Python allows appending ’L’ (or ’l’) to the integer to make it a long integer, which is unlimited in size (that is, as with all unlimited things in computer science, it is only limited by the amount of available memory).
There are two kinds of float literals: Point Floats : floats containing a period followed by one or more digits (eg: 123.456). Exponent Floats : floats in exponential form, ie which end with ’e’ (or ’E’) followed by a decimal integer exponent. Note that even if the integer part has a leading zero, it is still considered as decimal (eg: 123.456e78).
String constants in Python are enclosed either within two single quotes or within two double quotes, interchangeably.
u or U
Defines a Unicode string r or R
Defines a Raw string (backslash characters are not in- terpreted as escape characters). Mostly used for regu- lar expressions. If a prefix must contain both a ’U’ and an ’R’, then the ’U’ must appear before the ’R’. Standard C escape sequences are also accepted as part of the string constants, in addition to some escape sequences that are added by Python,as indicated below: 1.2 Language Features 5 / 75 Sequence Description \newline
Ignored \\ \ \’ Single quote \" Double quote \a ASCII Bell Character (BEL) \b ASCII Backspace Character (BS) \f ASCII Form Feed Character (FF) \n ASCII Line Feed Character (LF) \Nname Unicode character named ’name’ (Unicode only) \r ASCII Carriage Return Character (CR) \t ASCII Horizontal Tab Character (TAB) \uhhhh Character with the 16-bit Hexadecimal value hhhh (Unicode Only) \Uhhhhhhhh Character with
the 32-bit
Hexadecimal value
hhhhhhhh (Unicode Only) \v ASCII Vertical Tab Character (VT) \ooo ASCII Character with the octal value ooo \hh ASCII Character with the hexadecimal value hh Notes: 1. Two string adjacent string literals will be concatenated at compile time. For example, "Hello"" world!" will be concatenated into "Hello world!". This allows to easily break string literals across multiple lines. Comments are allowed between the parts of the string. Also, different quotation marks can be used for each part. The different parts of the string must be enclosed in parantheses if the string is to span multiple lines, if it is not already the case (eg for function calls). 2. String literals can also be enclosed within three double quotes, in which case newlines within the string are become part of it.
Python provides built-in support for imaginary numbers. It defines 1j to be equal to √
integers or floats. 1.2.2 Variables Variables are not declared in Python. Since Python is a dnamically typed language, any variable can be assigned any value, regardless of its previous type. This also holds for instance variables in classes. Therefore, it is a good idea to initialize all instance variables in the constructor to make sure that memory has been allocated for them, and in order to make sure that they can safely be used inside the class. Python used to rely on a reference counting algorithm to free the unused variables. The newer versions of Python (ie 2.0 beta and above) rely on a more traditional garbage collection technique. 1.2 Language Features 6 / 75 This thus implies that allocated memory is automatically freed when it is no longer used. It is usually very useful to think of Python variables as objects, since it is in essence how they are implemented. 1.2.2.1 Private Instance Variables By default, all instance variables are declared as public variables. However, there is a possibly to use name mangling to make a variable “pseudo-private”. This is done by preceding the variable name with 2 or more underscores. In this case, the actual name of the variable will be a combination of the class name and the variable name, without the leading underscores removed from the class name and a single underscore being added as a prefix to the new name. For example, inside a class named ’my class’, a variable named ’ my var’ would actually be internally known as my class my var. 1.2.3 Operators Python supports the standard C operators, along with some new operators, as described below.
+ Unary plus + Unary minus ∼ Bit inversion 1.2 Language Features 7 / 75 1.2.3.2 Binary Operators Operator Description Arithmetic Operators + Binary addition - Binary subtraction * Binary multiplication / Binary division % Remainder of the integer division ** Power
Bitwise Operators <<, >> Bit shift & Bitwise AND | Bitwise OR ˆ Bitwise XOR Logical Operators == Equal !=, <> Not equal < Greater than > Less than >= Greater than or equal <= Less than or equal is, is not Object identity in, not in Set membership Boolean Operators and
Boolean AND or Boolean OR not Boolean NOT Python allows combining multiple logical operators into a more intuitive form to avoid the use of the keyword and. For example, instead of writing (x > 10 and x <= 20 , one could write (10 < x <= 20). 1.2.4 Data Structures The Python language provides built-in support for some very interesting data struc- tures: lists, tuples and dictionaries. These data structures can be classified within two main categories: sequences and mappings. Sequences are finite ordered sets of ele- ments. The elements of a sequence can be accessed by indexing using non-negative numbers. This indexing starts from 0 in Python. Mappings are also finite sets, but the way elements of a mapping are accessed differs. Mappings use arbitrary index sets for accessing the elements. The Python language defines 3 main types of sequences: lists, tuples and strings (for simplicity, I gather under the term strings ordinary strings and unicode objects). At the current time, only one mapping type is available: the dictionary. Each of these is discussed in details below. 1.2 Language Features 8 / 75 1.2.4.1 Lists Lists are composed of an arbitrary number of elements. Each of these elements can have any valid type. Lists in Python are defined by enumerating the elements of a list, separated by commas, and enclosed within square brackets. Note that lists can be nested. For example: [0, 1, 2, 4], [’arial’, 20, ’bold’], [1, ’a string’, [3, 4, 5]] are valid lists. To access an element of the list, it is necessary to know its index. The first element has index 0. Accessing elements using their index is done using square brackets, just like for an array reference in C or Java. For example: Listing 1: Accessing List Elements >> list
= [ ’a’
, ’b’
, ’c’
] >> list
[0] ’a’
>> list [2]
’c’ 5 >> [ ’a’
, ’b’
, ’c’
][1] ’b’
>> The elements of a list can also be modified by assigning a value to a selection, just like it would be done for arrays in C or Java. For example:
>> list
= [ ’a’
, ’b’
, ’c’
] >> list
[1] = ’d’
>> list [ ’a’ , ’d’
, ’c’
] >> 5 When a negative index is specified, the length of the list is added to it before it is used. This make a negative index “wrap” around. Therefore, -1 denotes the last element of the list, -2 the second to last element, and so on. It is also possible to select multiple elements of a list at a time by taking a “slice” of the original list. Slicings are of the form
[
:
] . This creates a new sub-list contanining the elements of the original list having an index k, lower <= k < upper. For example: Listing 3: Slicing 1.2 Language Features 9 / 75 >> list
= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >> list
[1:7] [1, 2, 3, 4, 5, 6] >> It is possible to assign a list to a slice to modify part of an existing list, as shown below. Assigning an empty list to a slice deleted the slice from the original list. Listing 4: Slice Assignment >> list
= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >> list
[1:6] = [ ’a’
, ’b’
] [0,
’a’ , ’b’ , 6, 7, 8, 9, 10] >> The colon operator has some interesting defaults. When its left-hand side is not specified, it defaults to 0. When its right-hand side is not specified, it defaults to the length of the list. Therefore, the Python instruction list name [:]
will return the entire list. The built-in function len( ) can be used to obtain the length of a list. It is also possible to concatenate lists by using the ’+’ operator. 1.2.4.2 Tuples Tuples simply consist of a series of values enclosed within parentheses. Here is an example of a tuple: (’arial’, 10, ’bold’) Tuples work exactly like lists, except for the fact that they are immutable, which means that their elements cannot be modified in place. However, the values stored in the components of a tuple can be modified. In other words, a tuple should be thought of as holding references (or pointers) to other objects. Consider the following two examples. The first one shows that trying to change the elements of a tuple is not allowed. The second one shows that it is possible to modify a mutable element which is inside of a tuple. Note the fundamental difference between the two examples.
>> list
= [ ’a’
, ’f’
, ’c’
] >> tuple
= (10, list
, ’string constant’ ) >> tuple
[1] [ ’a’ , ’f’
, ’c’
] >> tuple
[1] = [ ’some’
, ’other’
, ’list’
] 5
1.2 Language Features 10 / 75 Traceback (innermost last): File " TypeError: object doesn’t support item assignment Listing 6: Tuple Example 2 >> list
= [ ’a’
, ’f’
, ’c’
] >> tuple
= (10, list
, ’string constant’ ) >> tuple
[1] [ ’a’ , ’f’
, ’c’
] >> tuple
[1][1] = ’b’
5 >> list
[ ’a’
, ’b’
, ’c’
] >> tuple
(10, [ ’a’
, ’b’
, ’c’
], ’string constant’ ) >>
Tuples, just like lists, can be concatenated using the ’+’ operator. 1.2.4.3 Dictionaries Dictionaries are mutable objects similar to lists, but are not restricted to integers for indexing purposes. For example, using dictionaries allows to map string values to objects, and reference these objects using their associated string. This is similar to what is done in a common english dictionary, where definitions are looked up by word. The index for a dictionary is called a key, and can be anything except a list, a dictionary, or other mutable types that are compared by value rather than by identity, since Python requires that keys have a constant hash value. Dictionaries in Python are declared by listing an arbitrary number of key/datum pairs sperated by a comma, all enclosed within curly brackets. Key/datum pairs are of the form key : datum
. Thus, a dictionary declaration has the following form: {key0 : datum0, key1 : datum1, ..., keyN : datumN} The square brackets are again used to access elements of a dictionary. However, this time, a key is used instead of an integer index. Here is a Python example using dictionaries:
>> dict
= \{"alice" : "123-4567" , "bob"
: "234-5678" , "charlie" : "N/A"
\} >> dict
[ ’alice’
] ’123-4567’ >> dict [
] ’N/A’
5 >> dict
[ ’charlie’ ] = ’987-6543’ >> dict \{’alice’ : ’123-4567’ , ’bob’
: ’234-5678’ , ’charlie’ : ’987-6543’ \} >>
Download 257.92 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling