-
Notifications
You must be signed in to change notification settings - Fork 13
/
QR_code_generator.py
262 lines (205 loc) · 9.31 KB
/
QR_code_generator.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askopenfilename
import os
import shutil
import barcode
from barcode.writer import ImageWriter
from MyQR import myqr
import csv
from datetime import datetime
s=0
#Creating the base window for the GUI
window = Tk()
window.resizable(width = False, height = False)
window.title("QR and Bar Code Generator")
window.iconbitmap("icon.ico")
#func to set size
def setsize(a):
global s
s=a
messagebox.showinfo("","Select format")
def open_file():
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
file = askopenfilename() # show an "Open" dialog box and return the path to the selected file
return file
#fucn to store data in csv files
def storedata():
if not os.path.isfile(os.getcwd() + "\\QrCode and Barcode Datas.csv"):
with open("QrCode and Barcode Datas.csv","w",newline="") as codes:
fields=["Subject","Type","Timestamp"]
writer=csv.DictWriter(codes,fieldnames =fields)
writer.writeheader()
with open("QrCode and Barcode Datas.csv","a", newline="") as codes:
writer=csv.writer(codes)
if type1==1:
writer.writerow([Subject.get(),"Qrcode",timestampStr1])
elif type1==2:
writer.writerow([Subject.get(),"Barcode",timestampStr2])
#function generate the qr code
def generate():
if len(name.get())!=0 and name.get() != "Enter filename here" and len(Subject.get())!=0 and Subject.get() != "Enter subject here":
if '/' not in name.get():
global qr, photo, filename, save_dir, timestampStr1,type1
filename = open_file()
version, level, qr = myqr.run( Subject.get(), version=1, level='H', picture=filename, colorized=True, contrast=1.0, brightness=1.0, save_name=name.get()+".png", save_dir=os.path.join(os.getcwd(), "src"))
dateTimeObj = datetime.now()
timestampStr1 = dateTimeObj.strftime("%d-%b-%Y (%H:%M:%S.%f)")
type1 = 1
storedata()
try: showcode()
except: pass
else: messagebox.showinfo("","filename can't contains '/'")
else:
messagebox.showinfo("","Please Enter some filename or subject")
#func to generate Barcode
def bargenerate():
if len(name.get())!=0 and name.get() != "Enter filename here" and len(Subject.get())!=0 and Subject.get() != "Enter subject here":
if '/' not in name.get():
global brcode, fpath, type1,timestampStr2
brcode=barcode.get("code128", Subject.get(), writer=ImageWriter() )
brcodesvg=barcode.get("code128", Subject.get() )
dummy = os.getcwd()+"\\images"
if not os.path.exists(dummy):
os.makedirs(dummy)
fpath=os.path.join(dummy,name.get())
brcode.save(fpath)
dateTimeObj = datetime.now()
timestampStr2 = dateTimeObj.strftime("%d-%b-%Y (%H:%M:%S.%f)")
type1 = 2
storedata()
try: showbrcode()
except: pass
else: messagebox.showinfo("","filename can't contains '/'")
else:
messagebox.showinfo("","Please Enter some filename or subject")
#func to save barcode in png format
def brsave():
try:
if len(name.get())!=0 and name.get() != "Enter filename here":
dir = "Bar_Codes"
if not os.path.exists(dir):
os.makedirs(dir)
spath=os.path.join(dir,name.get())
brcode.save(spath)
os.remove(fpath+".png")
messagebox.showinfo("", "Barcode saved in png format")
else:
messagebox.showinfo("","Please enter a File Name")
except:
messagebox.showinfo("","Generate the Bar code first!")
#function to show the qr code
def showcode():
global photo
photo = PhotoImage(file = os.path.join(os.getcwd(), "src") + "/" + name.get()+".png")
imageLabel.config(image = photo)
subLabel.config(text="QR of " + Subject.get())
#func to show barcode
def showbrcode():
global photo1
photo1 = PhotoImage(file= fpath +".png")
imageLabel.config(image = photo1)
subLabel.config(text="")
#function to save the generated code locally in png format
def save():
dir = "QR_Codes"
if not os.path.exists(dir):
os.makedirs(dir)
try:
if len(name.get())!=0 and name.get() != "Enter filename here":
if s == 0:
messagebox.showinfo("alert", "Select size first")
else:
version, level, qr = myqr.run( Subject.get(), version=1, level='H', picture=filename, colorized=True, contrast=1.0, brightness=1.0, save_name=name.get()+".png", save_dir=os.path.join(os.getcwd(), "QR_Codes"))
os.remove(os.path.join("src",name.get())+".png")
messagebox.showinfo("","Saved")
else:
messagebox.showinfo("","Please enter a File Name")
except:
messagebox.showinfo("","Generate the QR code first!")
#function to save the generated code locally in svg format
def svg():
dir = "QR_Codes"
if not os.path.exists(dir):
os.makedirs(dir)
if not os.path.exists('src/'+Subject.get()+'.png'):
version, level, qr = myqr.run( Subject.get(), version=1, level='H', picture=filename, colorized=True, contrast=1.0, brightness=1.0, save_name=Subject.get()+".png", save_dir=os.path.join(os.getcwd(), "src"))
shutil.copyfile('src/'+Subject.get()+'.png', 'QR_Codes/'+Subject.get()+'.svg')
os.remove(os.path.join("src",Subject.get())+".png")
messagebox.showinfo("","Saved")
#dummy func
def dummy():
try:
if len(name.get())!=0 and Subject.get() != "Enter subject here" and name.get() != "Enter filename here":
messagebox.showinfo("","Select size")
else:
messagebox.showinfo("","Please enter a File Name")
except:
messagebox.showinfo("","Generate the QR code first!")
#func to clear out any entry boxes when the user shifts focus
def clear_widget(event):
if SubEntry == window.focus_get() and SubEntry.get() == "Enter subject here":
SubEntry.delete(0, END)
elif nameEntry == window.focus_get() and nameEntry.get() == "Enter filename here":
nameEntry.delete(0, END)
#func to repopulate the default text previously inside the entry boxes if nothing is putin
#while focused and changes focus to another widget
def repopulate_defaults(event):
if SubEntry != window.focus_get() and SubEntry.get() == "":
SubEntry.insert(0, "Enter subject here")
elif nameEntry != window.focus_get() and nameEntry.get() == "":
nameEntry.insert(0, "Enter filename here")
#designing the GUI
f=Frame(window)
f.grid(row=0, column=0 ,sticky=N+S+W+E)
Subject = StringVar()
SubEntry = Entry(f,textvariable = Subject,width=50, font=("gothic", 12))
SubEntry.grid(row =0,column =0,columnspan=5,sticky=N+S+W+E)
SubEntry.insert(END,"Enter subject here")
SubEntry.bind("<FocusOut>", repopulate_defaults)
SubEntry.bind("<FocusIn>", clear_widget)
name = StringVar()
nameEntry = Entry(f,textvariable = name, width=50, font=("gothic", 12))
nameEntry.grid(row =1,column =0, columnspan=5,sticky=N+S+W+E)
nameEntry.insert(0,"Enter filename here")
nameEntry.bind("<FocusOut>", repopulate_defaults)
nameEntry.bind("<FocusIn>", clear_widget)
f2=Frame(window)
f2.grid(row=2, column=0, sticky=N+S+W+E)
s1 = Button(f2,text = "Qr code",width=9 , command = generate , font=("Helvetica", 12))
s1.grid(row =0,column =0,sticky=N+S+W+E)
s1 = Button(f2,text = "Save",width=9, command = dummy , font=("Helvetica", 12))
s1.grid(row =0,column =1,sticky=N+S+W+E)
s1=Label(f2,text=" ", width=3)
s1.grid(row=0,column=2,sticky=N+S+W+E)
s1 = Button(f2,text = "Bar code",width=9 , command = bargenerate , font=("Helvetica", 12))
s1.grid(row =0,column =3,sticky=N+S+W+E)
s1 = Button(f2,text = "Save",width=9 , command = brsave , font=("Helvetica", 12))
s1.grid(row =0,column =4,sticky=N+S+W+E)
f3= Frame(window)
f3.grid( row=3, column=0, columnspan=2, sticky=N+S+W+E)
s1 = Button(f3,text = "Small",width=4, command = lambda: setsize(8) , font=("Helvetica", 12))
s1.grid(row =0,column =0,sticky=N+S+W+E)
s1 = Button(f3,text = "Medium",width=7, command = lambda: setsize(10), font=("Helvetica", 12))
s1.grid(row =0,column =1,sticky=N+S+W+E)
s1 = Button(f3,text = "Large",width=6, command = lambda: setsize(16) ,font=("Helvetica", 12))
s1.grid(row =0,column =2,sticky=N+S+W+E)
f4=Frame(window)
f4.grid( row=4, column=0, columnspan=2, sticky=N+S+W+E)
saveB = Button(f4,text="PNG",width=9,command = save, font=("Helvetica", 12))
saveB.grid(row =1,column =0,sticky=N+S+W+E)
saveC = Button(f4,text="SVG",width=9,command = svg , font=("Helvetica", 12))
saveC.grid(row =1,column =1,sticky=N+S+W+E)
imageLabel = Label(window)
imageLabel.grid(row =5,column =0,sticky=N+S+W+E)
subLabel = Label(window,text="")
subLabel.grid(row =6,column =0,sticky=N+S+W+E)
#making the GUI resposnsive
Rows = 6
Columns = 4
for row in range(Rows+1):
window.grid_rowconfigure(row,weight=1)
for col in range(Columns+1):
window.grid_columnconfigure(col,weight=1)
#looping the GUI
window.mainloop()