-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyqt_test.py
97 lines (66 loc) · 2.13 KB
/
pyqt_test.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
The Kittens project
author: Rishabh Bhargava
"""
import sys
from PyQt4 import QtGui
from urllib2 import urlopen
import requests
class Kitten(QtGui.QWidget):
def __init__(self):
super(Kitten, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("KittenApp")
self.setWindowIcon(QtGui.QIcon('kitten_icon.png'))
self.height_label = QtGui.QLabel("Enter height: ", self)
self.height_label.move(10,10)
self.width_label = QtGui.QLabel("Enter width: ", self)
self.width_label.move(10, 40)
self.file_name_label = QtGui.QLabel("What name? ", self)
self.file_name_label.move(10, 70)
self.ok_button = QtGui.QPushButton('Click me', self)
self.ok_button.resize(self.ok_button.sizeHint())
self.ok_button.move(150,120)
self.ok_button.setToolTip("Please <b>Click</b> me!")
self.ok_button.clicked.connect(self.ok_button_clicked)
self.height_line_edit = QtGui.QLineEdit(self)
self.height_line_edit.move(100, 10)
self.width_line_edit = QtGui.QLineEdit(self)
self.width_line_edit.move(100, 40)
self.file_name_line_edit = QtGui.QLineEdit(self)
self.file_name_line_edit.move(100, 70)
self.show()
def ok_button_clicked(self):
if is_number(str(self.height_line_edit.text())) and is_number(str(self.width_line_edit.text())):
save_image(str(self.height_line_edit.text()), str(self.width_line_edit.text()), str(self.file_name_line_edit.text()))
self.height_line_edit.clear()
self.width_line_edit.clear()
self.file_name_line_edit.clear()
else:
error = QtGui.QMessageBox()
error.setText("Check the height and width fields again!")
error.exec_()
def save_image(height, width, name):
url = "http://placekitten.com/" + width + "/" + height
kittens = urlopen(url)
response = kittens.read()
file_name = name + ".jpeg"
file_kitten = open(file_name, "w")
file_kitten.write(response)
file_kitten.close()
def main():
app = QtGui.QApplication(sys.argv)
kit = Kitten()
sys.exit(app.exec_())
def is_number(num):
try:
float(num)
except:
return False
return True
if __name__ == '__main__':
main()