1 of 22

Python Libraries

Transfer Bridge 2020

2 of 22

csv

3 of 22

Reading CSV files (Array)

import csv

with open('data.csv') as csvfile: #open csv file

# create reader object to read in the file

readCSV = csv.reader(csvfile)

# print a line out for each row

for row in readCSV:

print(row)

Name

Age

StudentID

Blobby

21

012345678

Jimin

22

010101010

Pranav

20

101010101

Sofia

21

876543210

data.csv

Output:

['Name', 'Age', 'StudentID']

['Blobby', '21', '012345678']

['Jimin', '22', '010101010']

['Pranav', '20', '101010101']

['Sofia', '21', '876543210']

4 of 22

Reading CSV files (Dict)

import csv

with open('data.csv') as csvfile: #open csv file

# create Dictreader object to read in file

readCSV = csv.DictReader(csvfile)

# print a line out for each row

for row in readCSV:

print(row)

Output:

{'Name': 'Blobby', 'Age': '21', 'StudentID': '012345678'}

{'Name': 'Jimin', 'Age': '22', 'StudentID': '010101010'}

{'Name': 'Pranav', 'Age': '20', 'StudentID': '101010101'}

{'Name': 'Sofia', 'Age': '21', 'StudentID': '876543210'}

Name

Age

StudentID

Blobby

21

012345678

Jimin

22

010101010

Pranav

20

101010101

Sofia

21

876543210

data.csv

5 of 22

tkinter

6 of 22

What is tkinter?

  • tkinter (Tk interface) is the standard Python interface to Tk GUI toolkit
    • Used to develop desktop application
    • GUI (Graphic User Interface) ~ an interface that allows users to interact with electronic devices using graphical icons
      • more visual than command line

7 of 22

8 of 22

GUI

9 of 22

Initialize a Window

import tkinter as tk

def main():

# Create Tkinter window

window = tk.Tk()

window.title(‘Demo’)

“”” add widgets here “””

window.mainloop() # keep looping infinitely until there's a new event or the window is closed

main()

10 of 22

11 of 22

Widget Organization

  • Methods for organizing widgets in the parent window

12 of 22

13 of 22

Button

Widget Functions

  • activebackground: to set the background color when button is under the cursor.
  • activeforeground: to set the foreground color when button is under the cursor.
  • bg: to set he normal background color.
  • command: to call a function.
  • font: to set the font on the button label.
  • image: to set the image on the button.
  • width: to set the width of the button.
  • height: to set the height of the button.

14 of 22

Button Example

import tkinter as tk

WINDOW_WIDTH = 500

WINDOW_HEIGHT = 600

def main():

window = tk.Tk()

button = tk.Button(window, text='Press', height=WINDOW_HEIGHT, width=35, command=window.destroy)

button.pack(side=‘top’) #pack to the top of the screen

window.mainloop()

main()

15 of 22

16 of 22

Canvas

  • canvas widget used for drawing, or other layouts such as graphics and text
  • Canvas Functions:
    • bd: to set the border width in pixels.
    • bg: to set the normal background color.
    • cursor: to set the cursor used in the canvas.
    • highlightcolor: to set the color shown in the focus highlight.
    • width: to set the width of the widget.
    • height: to set the height of the widget.

17 of 22

Canvas Example

import tkinter as tk

WINDOW_WIDTH = 500

WINDOW_HEIGHT = 600

def main():

# Create Tkinter window

window = tk.Tk()

my_canvas = tk.Canvas(window, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg='yellow')

my_canvas.pack() # make width and height adjustment works on the canvas

window.mainloop() # keep looping until there's a new event

main()

18 of 22

19 of 22

Draw Lines and Shapes on Canvas

Canvas Coordinate System

(x0, y0)

(x1, y1)

(x0, y0)

(x1, y1)

20 of 22

Drawing Example

import tkinter as tk

WINDOW_WIDTH = 500

WINDOW_HEIGHT = 600

def main():

window = tk.Tk()

my_canvas = tk.Canvas(window, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg='yellow')

my_canvas.pack()

my_canvas.create_line(40, 50, 300, 100, 10, 150, fill='blue') #can use more than 2 points...

my_canvas.create_oval(30, 30, 50, 50, fill='purple')

my_canvas.create_rectangle(300, 75, 350, 125, fill='red')

window.mainloop() # keep looping until there's a new event

main()

21 of 22

22 of 22

References