-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialog_tk.py
executable file
·276 lines (213 loc) · 10 KB
/
dialog_tk.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#! /usr/bin/python
# -*- coding: iso-8859-1 -*-
#
# TODO:
# Wire up switches and test the "edit" feature
# Add QR printing option
# re-implement ad-hoc label
# Re-size edit window default
# See if it's possible to run only a single instance of the program
# and/or update fields when app regains focus. (Hard, to avoid losing edits)
import Tkinter
import cups
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
#import wand
from wand.image import Image as WandImage
# sudo apt-get install libmagickwand-dev
# sudo pip install Wand
import sqlite3 as sqlite
import sys
from subprocess import call
generalLabel = "4777701c"
doingGeneralLabel = False
class badger_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
try:
# turn off screen saver if it's on (relies on xscreensaver being installed)
call(["xscreensaver-command", "-deactivate"])
except:
print "no screensaver found"
#define place to put temp files (in RAM for speed)
self.tempFileDir = "/dev/shm/"
# set up page size parameters - 89 x 36 mm
self.w = 89 * mm
self.h = 36 * mm
# temporary tagString
#self.tagString = 'bb9c0b1c'
if len(sys.argv) < 2:
exit(1)
if len(sys.argv) == 1:
print "This should be ad-hoc label but not implemented yet"
tagString = sys.argv[1]
tagString2 = hex(int(tagString,16))[2:] # drop leading "0x"
if tagString2[-1:] == "L":
tagString2 = tagString2[:-1] # drop final "L"
if tagString != ("00000000"+tagString2)[-8:]:
print "Bad tag number: ", tagString, ("00000000"+hex(int(tagString,16))[2:-1])[-8:]
exit(1)
elif tagString == generalLabel:
# special tag for printing plain labels
print "Printing general label"
name = comment = ""
self.doingGeneralLabel = True
self.tagString = tagString ## TODO: may not be needed...
self.updating = False
else:
print "using tagString "+tagString
self.doingGeneralLabel = False
self.tagString = tagString
###################################################################
# interrogate the database
###################################################################
with sqlite.connect('badge.db') as con:
cur = con.cursor()
cur.execute("SELECT Name, Comment FROM Tags WHERE Tag = x'"+self.tagString+"'")
result = cur.fetchone()
self.updating = result != None
if self.updating:
name = result[0]
comment = result[1]
else:
name = comment = ""
###################################################################
# Create the UI
###################################################################
self.grid()
self.bind("<Return>", self.OnPressEnter)
self.bind("<FocusOut>", self.PreviewOnEvent)
# Application title
Tkinter.Label(self, anchor="w", text="Makespace badger"+ "" if self.doingGeneralLabel else" data update").grid(column=0,row=0,columnspan=3,sticky='EW')
# Tag being edited
Tkinter.Label(self,anchor="w", text="General label" if self.doingGeneralLabel else "Editing tag ").grid(column=0,row=1,sticky='EW')
if not self.doingGeneralLabel:
Tkinter.Label(self,anchor="w", text=self.tagString, fg='red').grid(column=1,row=1,sticky='EW')
if not self.updating and not self.doingGeneralLabel:
self.newLabel = Tkinter.Label(self,anchor="w", text="* NEW *", fg='red')
self.newLabel.grid(column=2,row=1,sticky='EW')
# name field
Tkinter.Label(self,anchor="w", text="Line 1" if self.doingGeneralLabel else "Name").grid(column=0,row=2,sticky='EW')
self.nameEntry = Tkinter.Entry(self)
self.nameEntry.grid(column=1,row=2,columnspan=2,sticky='EW')
self.nameEntry.bind("FocusIn", lambda: self.nameEntry.selection_from(0))
self.nameEntry.insert(0,name)
# "nameClear" button
Tkinter.Button(self,text=u"x", command=lambda:self.OnClrClick(self.nameEntry), takefocus=0).grid(column=4,row=2)
# comment field
Tkinter.Label(self,anchor="w", text="Line 2" if self.doingGeneralLabel else "Comment").grid(column=0,row=3,sticky='EW')
self.commentEntry = Tkinter.Entry(self)
self.commentEntry.grid(column=1,row=3,columnspan=2,sticky='EW')
self.commentEntry.bind("FocusIn", lambda: self.commentEntry.selection_from(0))
self.commentEntry.insert(0,comment)
# "commentClear" button
Tkinter.Button(self,text=u"x", command=lambda:self.OnClrClick(self.commentEntry).focus_set(), takefocus=0).grid(column=4,row=3)
# preview window
Tkinter.Label(self, anchor="w", text="Preview:").grid(column=0,row=4,columnspan=5,sticky='W')
# create the canvas, size in pixels
self.previewPane = Tkinter.Canvas(width = self.w, height = self.h)
self.previewPane.grid(column=0, columnspan=5,row=5)
# "print" button
self.printButton = Tkinter.Button(self,text="Print" if self.doingGeneralLabel else u"Save and Print", command=self.OnPrintButtonClick)
self.printButton.grid(column=1,row=6, columnspan=2, sticky="")
self.printButton.bind("<Return>", self.OnPrintEnter)
# generate preview files
self.DoPreview()
self.grid_columnconfigure(1,weight=1)
self.grid_columnconfigure(2,weight=1)
# fix vertical size
self.resizable(True,False)
##########################################################
# Printer initialisation
##########################################################
self.conn = cups.Connection()
# test to see if the label printer is installed
self.printers = self.conn.getPrinters ()
try:
self.printers["DYMO-LabelWriter-450"]["device-uri"]
except KeyError:
print "Label printer not found"
exit(1)
self.nameEntry.focus_set()
def OnClrClick(self,fld):
fld.delete(0, "end")
fld.focus_set()
def PreviewOnEvent(self,event):
self.DoPreview()
def DoPreview(self):
#####################################################
# Use pdfgen to create our badge...
#####################################################
name=self.nameEntry.get()
comment = self.commentEntry.get()
c = canvas.Canvas(self.tempFileDir+"badge.pdf",pagesize=(self.w,self.h))
# Now shrink font until name fits...
fontSize = 60
nameWidth = c.stringWidth(name,"Helvetica-Bold",60)
if (nameWidth > (self.w * 0.9) ):
fontSize = fontSize * self.w * 0.9 / nameWidth
c.setFont("Helvetica-Bold",fontSize)
c.drawCentredString(self.w/2,70-fontSize/2,name)
c.setFont("Helvetica",14)
c.translate(self.w/2,15)
commentWidth = c.stringWidth(comment,"Helvetica-Bold",14)
if (commentWidth > (self.w * 0.9) ):
hScale = self.w * 0.9 / commentWidth
else:
hScale = 1
c.scale(hScale,1)
c.drawCentredString(0,0,comment)
c.showPage()
c.save()
#############################################################
# now create and view displayable version of the image
#############################################################
with WandImage(filename=self.tempFileDir+'badge.pdf') as im:
im.format = "gif"
im.alpha_channel = False
im.save(filename=self.tempFileDir+'badge.gif')
# load the .gif image file
self.gif1 = Tkinter.PhotoImage(file = self.tempFileDir+'badge.gif')
# put gif image on canvas, centered
self.previewPane.create_image(self.w/2, self.h/2, image = self.gif1, anchor = "center")
def OnPrintEnter(self,event):
#############################################################
# now print label and save values to db
#############################################################
# DoPreview not needed in this case, because button has focus, so must have done preview when
# focus left another field.
# ...so do the print
self.conn.printFile("DYMO-LabelWriter-450",self.tempFileDir+"badge.pdf","Badge",{})
# ...and the save
name=self.nameEntry.get()
comment = self.commentEntry.get()
if app.doingGeneralLabel:
pass
else:
with sqlite.connect('badge.db') as con:
cur = con.cursor()
if self.updating:
t = (name, comment)
# seems you can't use variables in a where clause...
cur.execute("UPDATE Tags SET Name=?, Comment=? WHERE Tag=x'"+self.tagString+"'", t)
else:
t = (name, comment)
cur.execute("INSERT INTO Tags VALUES(x'"+self.tagString+"', ?, ?)", t)
self.updating = True
# clear the "new" field by destroying '* NEW *' label
self.newLabel.destroy()
self.destroy()
#return("break") # prevent propagation of <Return> event -- as a result, focus stays on the button
def OnPrintButtonClick(self):
self.DoPreview() # This is necessary in the case where you click Print without the focus leaving one of the Entry fields.
self.OnPrintEnter(None) # now can process as if <Enter> has been pressed.
def OnPressEnter(self,event):
self.DoPreview()
event.widget.tk_focusNext().focus()
if __name__ == "__main__":
app = badger_tk(None)
app.title('Makespace Badger (database update) v0.01')
app.mainloop()