0% found this document useful (0 votes)
73 views25 pages

Learning Programming Using Python 3.8 With Tkinter Lecture - 1 Week 6-7

The document discusses setting up Python 3.8 and Tkinter for GUI development, including downloading and installing Python, configuring the development environment, learning basic Python programming concepts like docstrings and f-strings, and integrating Tkinter into a Python program to create a simple window with a label. Code conventions like naming, commenting, and packaging widgets are also explained.

Uploaded by

Lance Lira
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
73 views25 pages

Learning Programming Using Python 3.8 With Tkinter Lecture - 1 Week 6-7

The document discusses setting up Python 3.8 and Tkinter for GUI development, including downloading and installing Python, configuring the development environment, learning basic Python programming concepts like docstrings and f-strings, and integrating Tkinter into a Python program to create a simple window with a label. Code conventions like naming, commenting, and packaging widgets are also explained.

Uploaded by

Lance Lira
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

Learning Programming using

Python 3.8 with Tkinter


LECTURE – 1
Week 6-7

Prepared by : DaBudz
Objective

• Learn how to program using Python 3.8


• Learn how to setup Python 3.8
• Learn basics of Tkinter for GUI
Table of Contents
Introduction
Installation of Python
Working Environment
Learning to program using Python 3
Learning how to use Tkinter
Introduction
According to Wikipedia:

Python is an interpreted, high-level, general-purpose programming language.

Created by Guido van Rossum and first released in 1991, Python's design


philosophy emphasizes code readability with its notable use of  significant
whitespace. Its language constructs and object-oriented approach aim to help
programmers write clear, logical code for small and large-scale projects.
Installation
According to Wikipedia:

The Python download requires about 25 Mb of disk space; keep it on your


machine, in case you need to re-install Python. When installed, Python requires
about an additional 90 Mb of disk space.
Downloading
1. Click Python Download. The following page will appear in your browser.
Similar to this
Installation

2. Click the Windows link (two lines below


the Download Python 3.x.x button). The
following page will appear in your browser
Working Environment

Here in the terminal you can type


your codes. ( Short ones )
This is the Terminal
This is the most fundamental
Setting to try – out per line of your
Source code…

However , if you press


File  New , it will open the
Editor space
Working Environment
Code Convention
1. Docstring
A Python documentation string, aka docstring, is the first statement in the definition of a
module, function, class, or method enclosed by triple double-quotes """.
Here is a minimal example of docstring in a function.

def foo():
"""This function does nothing."""
pass
print(foo.__doc__) # This function does nothing.

The docstring of a function should contain a (one-line) short description of its purpose,
followed by paragraphs describing the conventions of calling the function.
2. f-string
You might be used to formatting strings using % or .format().

name = 'World'
'Hello %s' % name # Hello World
'Hello {}'.format(name) # Hello World

Ditch them. Once you need to print multiple variables in longer strings, the
code will soon become messy and less readable. These formatting methods
are not straightforward and intuitive anyway.
Python f-string is a game-changer introduced in Python 3.6. It is a readable and elegant
string formatting syntax that embeds expressions inside strings. This is done by syntax
f'{expr}' where an expression is enclosed by curly brackets inside an f-string with an f at the
beginning before the single quote.

name = 'World'
print(f'Hello {name}') # Hello World
You can also put any syntactically valid expressions inside the curly brackets and it works
just fine, you can even call functions in the expressions!
a = [1,2.2,3]
print(f'Sum of squares of {a} is {sum_of_squares(a)}')
# Sum of squares of [1, 2.2, 3] is 14.84
3. Naming Convention
Naming things is one of the most difficult things in Computer
Science. You run out of ideas. You do not know how to name a
temporary intermediary variable. You are not alone.

Despite all these struggles, there are naming conventions in


Python to ‘narrow down’ your choices of naming variables. They
help make your codes more consistent, readable, and reusable.
So you should no longer declare all variables using single
lowercase letters like a, x etc. if the letters themselves do not
carry meanings.

Also, you should use meaningful, easy-to-understand, and


easy-to-recognise words to name them, such as user_profile
instead of uspr.
Here are 6 extra tips on naming things:
• Avoid naming things using single letters such as O, I, l for obvious reasons.
• Variable and function names should all be in lowercase.
• Words in a variable or function name should be separated by an
underscore _.
• Private variables (say inside a class) might begin with a single underscore.
• Words in a class name should be concatenated and capitalised, such as
MarioKart.
• Constant names should be in uppercase, such as GOLDEN_RATIO.
Things to remember
Why Commenting Your Code Is So Important

Comments are an integral part of any program. They can come in the form of module-level
docstrings, or even inline explanations that help shed light on a complex function.

Before diving into the different types of comments, let’s take a closer look at why
commenting your code is so important.

Consider the following two scenarios in which a programmer decided not to comment their
code.
Lets integrate TKinter
First we Open a new text using the text editor in Python.

Then we include the following on the text

“ from tkinter import * ”


This allows us to use everything that tkinter has to offer.
''' Multi line comment
Everything in tkinter is a widget there is a lot of widgets here
button , text , frame, and the first thing to create is a root
widget.

ROOT widget is the same as a window widget similar to C#


workspace / Form1
This has to happen first
We are goint to create a simple window " Bulaga Window"
in tkinter there is a 2 step
1. You have to define the thing you want to create
2. You want it to put it on the screen
so as the example mylabel = Label widget
then on the arguement we load it on the root which is
the window then we put on the text) so the syntax is

Label ( widget , text = " " )


after this we need to pack the whole thing think of the package as a
method that will organize them and creating a package where everything
is just one call away .

This will show the item on the screen

The last thing to do is create an event loop. An event loop will continually
show the widget on the screen it will keep track of everything on the
screen .even the motion of the mouse and everything....
Summary of the Code
'Creating a window'
root = Tk()

' Creating a Label'


mylabel = Label(root,text = "Bulaga men ! Bulaga ")

'Creating a Package'
mylabel.pack()

'This is the loop'


root.mainloop()
Screenshot of the Output
The Output

As you can see, we have just


Call the output.
Reference
Riptutorial.com
Tutorialspoint
http://google.github.io/styleguide/pyguide.html

You might also like