About Libraries: What Are TCL, TK, and Tkinter?
About Libraries: What Are TCL, TK, and Tkinter?
Pip
The pip package manager makes it easy to install open-source libraries that expand what
you’re able to do with Python.
Later, we will use it to install everything needed to create a working web application.
If you don’t have it already, you can get pip by following these instructions. In Windows, it’s
necessary to make sure
that the Python Scripts directory is available on your system’s PATH so it can be called from
anywhere on the command line. This screencast can help.
Verify pip is installed with the following.
$ pip -V
Tkinter
The primary GUI toolkit we will be using is Tk, Python’s default GUI. We’ll access Tk from
its Python interface called Tkinter (short for “Tk interface”).
Tk is not the latest and greatest, nor does it have the most robust set of GUI building blocks,
but it is fairly simple to use, and with it, you can build GUIs that run on most platforms.
Once you have completed tkinter, you will have the skills to build more complex
applications and/or move to a more advanced toolkit. Python has bindings or adapters to
most of the current major toolkits, including commercial systems.
You can choose to completely design all your widgets first, and then add the real
functionality, or do a little of this and a little of that along the way.
Widgets can be stand-alone or be containers. If a widget contains other widgets, it is
considered the parent of those widgets. Accordingly, if a widget is contained in another
widget, it’s considered a child of the parent, the parent being the next immediate enclosing
container widget.
Usually, widgets have some associated behaviours, such as when a button is pressed, or text
is filled into a text field. These types of user behaviours are called events, and the GUI’s
response to such events are known as callbacks.
Event-Driven Processing
Events can include the actual button press (and release), mouse movement, hitting the Return
or Enter key, etc. The entire system of events that occurs from the beginning until the end of
a GUI application is what drives it. This is known as event-driven processing.
One example of an event with a callback is a simple mouse move.
so everything just remains idle on the screen again.
• The event-driven processing nature of GUIs fits right in with client/server architecture.
• When you start a GUI application, it must perform some setup procedures to prepare for the
core execution, just as how a network server must allocate a socket and bind it to a local
address.
• This loop runs forever waiting for GUI events, processing them, and then going to wait for
more events to process.
Geometry Managers
Geometry managers allow us to organize widgets inside of a container
Tk has three geometry managers that help with positioning your widgetset:
• Placer: You provide the size of the widgets and locations to place them; the manager then
places them for you. The problem is that you have to do this with all the widgets, burdening
the developer with coding that should otherwise take place automatically.
• Packer: it packs widgets into the correct places (namely the containing parent widgets,
based on your instruction), and for every succeeding widget, it looks for any remaining “real
estate” into which to pack the next one. The process is similar to how you would pack
elements into a suitcase when traveling.
• Grid: It is used to specify GUI widget placement, based on grid coordinates. The Grid will
render each object in the GUI in their grid position.
Tk Widgetsget Description
Button Similar to a Label but provides additional functionality for mouse-overs, presses, and
releases, as well as keyboard activity/events.
Canvas Provides ability to draw shapes (lines, ovals, polygons, rectangles); can contain
images or bitmaps.
Checkbutton Set of boxes, of which any number can be “checked”.
Entry Single-line text field with which to collect keyboard input.
Frame Pure container for other widgets.
Label Used to contain text or images.
LabelFrame Combo of a label and a frame but with extra label attributes.
Listbox Presents the user with a list of choices from which to choose.
Menu Actual list of choices “hanging” from a Menu button from which the user can choose.
Menubutton Provides infrastructure to contain menus (pulldown, cascading, etc.)
Message Similar to a Label, but displays multiline text.
PanedWindow A container widget with which you can control other widgets placed within
it.
Radiobutton Set of buttons, of which only one can be “pressed”.
Scale Linear “slider” widget providing an exact value at current setting; with defined starting
and ending values.
Scrollbar Provides scrolling functionality to supporting widgets, for example, Text, Canvas,
Listbox, and Entry.
Spinbox Combination of an entry with a button letting you adjust its value
Text Multiline text field with which to collect (or display) text from user
Toplevel Similar to a Frame, but provides a separate window container
pymysql
In order to connect Python to a database you need a driver, which is a library used to Interact
with the database. For MySQLdatabase, you have such 3 Driver choices:
1. MySQL/connector for Python
2. MySQLdb
3. PyMySQL
Install PyMySQL
In order to install PyMySQL on Windows (or Ubuntu/Linux) you need to open
the CMD window, and run the following statement: pip install PyMySQL.
Connection Object
classpymysql.connections.Connection(host=None, user=None, password='',
database=None)
Representation of a socket with a mysql server.
The proper way to get an instance of this class is to call connect().
Establish a connection to the MySQL database. Accepts several arguments:
Cursor Objects
classpymysql.cursors.Cursor(connection)
This is the object you use to interact with the database.
Do not create an instance of a Cursor yourself. Call connections.Connection.cursor().
Methods of cursor object
close() Closing a cursor just exhausts all remaining data.
execute(query, args=None) Execute a query
Return
int
type:
If args is a list or tuple, %s can be used as a placeholder in the query. If args is a dict, %
(name)s can be used as a placeholder in the query.
fetchall() Fetch all the rows
fetchmany(size=None) Fetch several rows
fetchone() Fetch the next row
sys
To get the name of the platform we’re running Python on, we make a call
to sys.platform in Python.
Instead of delivering the output to the console, you can log into a text file.
Using sys.stderr and a text file in Python, we can log error information to
the text file.
Note that this traceback doesn’t show up in the log file until we close its file object in
Python (which, in this case, is fsock).