-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignments
248 lines (211 loc) · 7.13 KB
/
Assignments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
Assignment10_1.py
'''
Created on Jun 5, 2015
A GUI application, which displays a window for reading the width and height of
a shape and then draws it. The shape can be either a rectangle or an oval and
it should also be read from an Entry widget.
@author: lebs
'''
from tkinter import *
def drawRectangle():
global shapeEntry, canvas
canvas.pack_forget()
shape = shapeEntry.get()
width = shape.split(',')[0]
height = shape.split(',')[1]
canvas=Canvas(root, height=300, width=400)
canvas.create_rectangle(10,10,eval(width),eval(height))
canvas.pack()
def drawOval():
global shapeEntry, canvas
canvas.pack_forget()
shape = shapeEntry.get()
width = shape.split(',')[0]
height = shape.split(',')[1]
canvas=Canvas(root, height=300, width=400)
canvas.create_oval(10,10,eval(width),eval(height))
canvas.pack()
if __name__ == '__main__':
root = Tk()
root.wm_title('Draw')
root.geometry('400x400+50+60')
root.resizable(width=True, height=False)
label=Label(root, text='Enter width,height:')
label.pack()
shapeEntry=Entry(root)
shapeEntry.pack()
canvas=Canvas(root, height=300, width=400)
button1=Button(root, text='Rectangle', command=drawRectangle)
button2=Button(root, text='oval', command=drawOval)
button1.pack()
button2.pack()
root.mainloop()
Assignment10_2.py
'''
Created on Jun 5, 2015
A GUI application , which changes the background color of the main window as the
mouse moves above it.
@author: lebs
'''
from tkinter import *
def changeColor(Event):
root.configure(background='black')
if __name__ == '__main__':
root = Tk()
root.wm_title('Color Change')
root.geometry('500x500')
root.bind('<Motion>',changeColor)
root.mainloop()
Assignment10_3.py
'''
Created on Jun 5, 2015
A GUI application, which consists of two buttons and the program should work so
that when the first button is clicked, the second button should disappear if it
is visible and vice versa. The application should also work so that if the mouse
is moved above the second button, the first button will be moved up.
@author: lebs
'''
from tkinter import *
def changeButton2(Event):
global button2, visible
if visible == 1:
button2.grid_remove()
visible = 0
elif visible == 0:
button2.grid()
visible = 1
def changeButton1(Event):
global button2, x
x += 1
button2.grid_remove()
button2.grid(row = x, column = 1)
if __name__ == '__main__':
root = Tk()
button1 = Button(root, text='Click')
button2 = Button(root, text='Move')
button1.grid(row=0, column=0)
visible = 1
button2.grid(row=0, column=1)
x = 0
button1.bind('<Button-1>', changeButton2)
button2.bind('<Motion>', changeButton1)
root.mainloop()
Assignment11_1.py
'''
Created on Jun 8, 2015
A GUI application which provides appropriate interfaces to enter and search product
information (id, name, amount and unit price) to and from a SQLITE3 database.
The application should allow searching product data based on name or unit price.
@author: lebs
'''
from tkinter import messagebox, Entry, Button, Tk, Label
import sqlite3
from os.path import exists
from tkinter.constants import END
dbPath = 'pdt.db'
def search():
global idEntry, nameEntry, amountEntry, unitPriceEntry
productID = idEntry.get()
name = nameEntry.get()
amount = amountEntry.get()
unitPrice = unitPriceEntry.get()
con = sqlite3.connect(dbPath)
cur = con.cursor()
if productID == '' and name == '' and amount == '' and unitPrice == '':
query = 'SELECT * FROM product'
else:
query = 'SELECT * FROM product WHERE '
if productID != '':
query += 'id = ' + productID + ' '
if name != '':
query += 'name = ' + name + ' '
if amount != '':
query += 'amount = ' + amount + ' '
if unitPrice != '':
query += 'unit_price = ' + unitPrice
cur.execute(query)
result = cur.fetchall()
formatResult = ''
for product in result:
formatResult += 'id: ' + str(product[0]) + '\n'
formatResult += 'name: ' + product[1] + '\n'
formatResult += 'amount: ' + str(product[2]) + '\n'
formatResult += 'unit price: ' + str(product[3]) + '\n'
formatResult += '-------------------------------\n'
messagebox.showinfo(title='Result', message=formatResult)
con.close()
idEntry.delete(0, END)
nameEntry.delete(0, END)
amountEntry.delete(0, END)
unitPriceEntry.delete(0, END)
def create():
global idEntry, nameEntry, amountEntry, unitPriceEntry
productID = idEntry.get()
name = nameEntry.get()
amount = amountEntry.get()
unitPrice = unitPriceEntry.get()
con = sqlite3.connect(dbPath)
cur = con.cursor()
cur.execute("INSERT INTO product VALUES({}, '{}', {}, {})".format(productID, name, amount, unitPrice))
con.commit()
con.close()
messagebox.showinfo(title='Congratulations', message='This product has been created successfully!')
idEntry.delete(0, END)
nameEntry.delete(0, END)
amountEntry.delete(0, END)
unitPriceEntry.delete(0, END)
if not exists(dbPath):
con = sqlite3.connect(dbPath)
cur = con.cursor()
cur.execute('CREATE TABLE product(id int, name text, amount int, unit_price float)')
con.commit()
con.close()
root = Tk()
root.wm_title('Product Manager')
Label(root, text='id:').grid(row=0, column=0)
idEntry = Entry(root)
idEntry.grid(row=0, column=1)
Label(root, text='name:').grid(row=1, column=0)
nameEntry = Entry(root)
nameEntry.grid(row=1, column=1)
Label(root, text='amount:').grid(row=2, column=0)
amountEntry = Entry(root)
amountEntry.grid(row=2, column=1)
Label(root, text='unit price:').grid(row=3, column=0)
unitPriceEntry = Entry(root)
unitPriceEntry.grid(row=3, column=1)
Button(root, text='Search', command=search).grid(row=4, column=0)
Button(root, text='Create', command=create).grid(row=4, column=1)
root.mainloop()
Assignment11_2.py
'''
Created on Jun 8, 2015
A GUI application, which provides appropriate interface for entering the address of an
image on Internet and then download and save it to the local machine and display it on
the application.
@author: lebs
'''
from tkinter import messagebox, Entry, Label, Button, LEFT, END, Tk
from urllib import request
from os.path import basename,abspath
def download():
global urlEntry
url = urlEntry.get()
urlEntry.delete(0, END)
try:
response = request.urlopen(url).read()
fileName = basename(url)
file = open(fileName,'wb')
file.write(response)
file.close()
messagebox.showinfo(title="Congratulations", message='The picture "' + fileName + '" has been downloaded to: "' + abspath(fileName) + '".')
except Exception as e:
messagebox.showinfo(title="Error", message=str(e))
root = Tk()
root.wm_title('Picture Download')
root.geometry('300x50')
Label(root, text='Address: ').pack(side=LEFT)
urlEntry = Entry(root)
urlEntry.pack(side=LEFT)
Button(root, text='Download', command=download).pack(side=LEFT)
root.mainloop()