Skip to content

Commit 8328be1

Browse files
committed
SQL
1 parent 98b52cf commit 8328be1

6 files changed

Lines changed: 141 additions & 11 deletions

File tree

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@
7777
from book import Book
7878

7979
book = Book("Are You My Mother?", 1000)
80-
print("Added book id:", booksAPI.add_book(book))
81-
print("Get book by title:", booksAPI.get_book_by_title(book.title))
82-
print("Not a valid book:", booksAPI.get_book_by_title("yeet"))
80+
print("Added book id:", booksSDK.add_book(book))
81+
print("Get book by title:", booksSDK.get_book_by_title(book.title))
82+
print("Not a valid book:", booksSDK.get_book_by_title("yeet"))
8383

84-
booksAPI.add_book(Book('The Digging-est Dog', 76))
85-
print("get books:", booksAPI.get_books())
84+
booksSDK.add_book(Book('The Digging-est Dog', 76))
85+
print("get books:", booksSDK.get_books())
8686

87-
book = booksAPI.update_book(book, book.title, 76)
87+
book = booksSDK.update_book(book, book.title, 76)
8888
print("Updated book:", book)
89-
booksAPI.delete_book(book)
90-
print("After delete:", booksAPI.get_book_by_title("Are You My Mother?"))
89+
print("Deleted:", booksSDK.delete_book(book))
90+
print("After delete:", booksSDK.get_book_by_title("Are You My Mother?"))
9191

92-
print("All books:", booksAPI.get_books())
92+
print("All books:", booksSDK.get_books())
9393

9494

9595

‎python/11-tk-list.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
import tkinter
3+
from book import Book
4+
import booksSDK
5+
6+
tk = tkinter.Tk()
7+
books = []
8+
9+
def add_to_list():
10+
11+
if title.get() == "" or pages.get() == "":
12+
return
13+
14+
book = Book(title.get(), int(pages.get()))
15+
print(book)
16+
if(booksSDK.add_book(book)):
17+
books.append(book)
18+
lb.insert(tkinter.END, book)
19+
title.delete(0, tkinter.END)
20+
pages.delete(0, tkinter.END)
21+
22+
def delete_from_list():
23+
index_tuple = lb.curselection()
24+
if booksSDK.delete_book(books.pop(index_tuple[0])):
25+
lb.delete((index_tuple))
26+
27+
lb = tkinter.Listbox(tk)
28+
29+
for book in booksSDK.get_books():
30+
books.append(book)
31+
lb.insert(tkinter.END, book)
32+
33+
lb.pack()
34+
35+
add = tkinter.Button(tk, text="Add Book", command=add_to_list)
36+
add.pack()
37+
38+
delete = tkinter.Button(tk, text="Delete Book", command=delete_from_list)
39+
delete.pack()
40+
41+
tkinter.Label(tk, text="Title:").pack()
42+
title = tkinter.Entry(tk, text="Title to Add:")
43+
title.pack()
44+
45+
tkinter.Label(tk, text="Pages:").pack()
46+
pages = tkinter.Entry(tk, text="Number of pages:")
47+
pages.pack()
48+
49+
tk.mainloop()

‎python/11-tk.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
########## Intro to GUI App Development ##########
2+
3+
4+
#There are numerous "frameworks" you can use to build a user interface with Python
5+
#This is to build desktop applications.
6+
#Although there may be some debate on which is best,
7+
#you can probably get the job done with any
8+
#tkinter is build it. So that's what we are going to learn first.
9+
10+
#Seperation between functionality and UI is ideal
11+
#Because of this, we try to keep logic in the UI code minimal
12+
#We can make this easier by creating an SDK to work with the Db (as we did)
13+
#Long term goal is to actually create an API we can consume...
14+
#We'll get into this, but essentially we can have the same backend for:
15+
# Desktop app
16+
# CLI
17+
# Web App
18+
# Mobile App
19+
# Etc...
20+
21+
#For now we are going to use the SDK, build out a UI, and then later an API
22+
23+
"""
24+
#Getting started
25+
#Because tkinter is built in, we don't have to download or install anything.
26+
#We just import it:
27+
import tkinter
28+
tk = tkinter.Tk() #This is the start up code just to get a window
29+
#We can modify the window title like so:
30+
tk.title("This is a Title")
31+
tk.mainloop() #This will "start" the application.
32+
print("This will wait")
33+
"""
34+
35+
#Let's build a small application:
36+
37+
from random import randint
38+
import tkinter
39+
from tkinter import messagebox
40+
41+
low = 0
42+
high = 20
43+
rand = randint(low, high)
44+
def check(guess):
45+
print(guess)
46+
if guess < rand:
47+
#tkinter.messagebox.showinfo("No", "You guessed " + str(guess)).pack
48+
tkinter.Label(tk, text=f"{guess} is low").pack()
49+
elif guess > rand:
50+
tkinter.Label(tk, text=f"{guess} is high").pack()
51+
else:
52+
tkinter.messagebox.showinfo("You win!", f"{guess} is correct!")
53+
54+
tk = tkinter.Tk()
55+
tk.title("Guessing Game")
56+
guess_goal = tkinter.Label(tk, text=f"Guess a number {low} to {high} (inclusive)")
57+
guess_goal.pack()
58+
entry = tkinter.Entry(tk)
59+
entry.pack()
60+
button = tkinter.Button(tk, text="Take a guess", command=lambda: check(int(entry.get())))
61+
button.pack()
62+
tk.mainloop()
63+
64+
########## Tkinter basics ##########
65+
########## Displaying a List of Data with Tkinter ##########
66+
########## Removing List Items in Tkinter ##########
67+
########## Working with SQLlite in Tkinter ##########
68+
69+
70+
71+
72+
"""
73+
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
74+
frame.pack(fill=BOTH,expand=1)
75+
label = tkinter.Label(frame, text="Hello, World")
76+
label.pack(fill=X, expand=1)
77+
button = tkinter.Button(frame,text="Exit",command=tk.destroy)
78+
button.pack(side=BOTTOM)
79+
tk.mainloop()
80+
"""
File renamed without changes.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@ def delete_book(book):
5959
c = cursor()
6060
with c.connection:
6161
c.execute('DELETE FROM books WHERE title=? AND pages=?', (book.title, book.pages))
62-
c.connection.close()
62+
rows = c.rowcount
63+
c.connection.close()
64+
return rows
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def print_menu():
2727
########### CONUMSING SDK ##########
2828

2929

30-
3130
#This brought up some questions on SDK design
3231
#Should deleting/updating a book require book object or just title? ID? So many options
3332
#Not very easy to maintain object as you might easily do with a UI.

0 commit comments

Comments
 (0)