2
2
3
3
import sys , json
4
4
import tkinter as tk
5
- from PIL import *
5
+ from PIL import Image , ImageTk
6
6
7
7
elements = {}
8
8
zlist = []
@@ -170,38 +170,33 @@ def renderText(values, parent, args):
170
170
screenTop = top + getScreenTop (parent )
171
171
width = getValue (args , values ['width' ]) if 'width' in values else 100
172
172
height = getValue (args , values ['height' ]) if 'height' in values else 100
173
- right = screenLeft + width
174
- bottom = screenTop + height
175
173
shape = getValue (args , values ['shape' ]) if 'shape' in values else 'rectangle'
176
- fill = getValue (args , values ['fill' ]) if 'fill' in values else None
177
174
outline = getValue (args , values ['outline' ]) if 'outline' in values else None
178
- outlineWidth = getValue (args , values ['outlineWidth' ]) if 'outlineWidth' in values else 0 if outline == None else 1
179
175
color = getValue (args , values ['color' ]) if 'color' in values else None
180
176
text = getValue (args , values ['text' ]) if 'text' in values else ''
181
177
fontFace = getValue (args , values ['fontFace' ]) if 'fontFace' in values else 'Helvetica'
182
178
fontWeight = getValue (args , values ['fontWeight' ]) if 'fontWeight' in values else 'normal'
183
- fontSize = round (height * 2 / 5 ) if shape == 'ellipse' else round (height * 3 / 5 )
184
- fontTop = screenTop + height / 2
179
+ fontTop = int (round (screenTop + height / 2 ))
185
180
if 'fontSize' in values :
186
181
fontSize = getValue (args , values ['fontSize' ])
187
- fontTop = top + round (fontSize * 5 / 4 )
188
- adjust = round (fontSize / 5 ) if shape == 'ellipse' else 0
182
+ fontTop = int (round (screenTop + height / 2 - fontSize / 4 ))
183
+ else :
184
+ fontSize = int (round (height * 2 / 5 ) if shape == 'ellipse' else round (height * 3 / 5 ))
185
+ fontTop -= int (round (screenTop + height / 2 - fontSize / 5 ))
186
+ adjust = int (round (fontSize / 5 )) if shape == 'ellipse' else 0
189
187
align = getValue (args , values ['align' ]) if 'align' in values else 'center'
190
188
if align == 'left' :
191
- xoff = round (fontSize / 5 )
189
+ xoff = int ( round (fontSize / 5 ) )
192
190
anchor = 'w'
193
191
elif align == 'right' :
194
- xoff = width - round (fontSize / 5 )
192
+ xoff = width - int ( round (fontSize / 5 ) )
195
193
anchor = 'e'
196
194
else :
197
- xoff = width / 2
195
+ xoff = int ( round ( width / 2 ))
198
196
anchor = 'center'
199
197
if xoff < 3 :
200
198
xoff = 3
201
- if shape == 'ellipse' :
202
- containerId = getCanvas ().create_oval (screenLeft , screenTop , right , bottom , fill = fill , outline = outline , width = outlineWidth )
203
- else :
204
- containerId = getCanvas ().create_rectangle (screenLeft , screenTop , right , bottom , fill = fill , outline = outline , width = outlineWidth )
199
+ xoff -= int (round (fontSize / 4 ))
205
200
textId = canvas .create_text (screenLeft + xoff , fontTop + adjust , fill = color , font = f'"{ fontFace } " { fontSize } { fontWeight } ' , text = text , anchor = anchor )
206
201
if 'name' in values :
207
202
widgetName = getValue (args , values ['name' ])
@@ -211,7 +206,6 @@ def renderText(values, parent, args):
211
206
"type" : "text" ,
212
207
"name" : widgetName ,
213
208
"id" : textId ,
214
- "containerId" : containerId ,
215
209
"left" : left ,
216
210
"top" : top ,
217
211
"width" : width ,
@@ -229,34 +223,31 @@ def renderImage(values, parent, args):
229
223
top = getValue (args , values ['top' ]) if 'top' in values else 10
230
224
screenTop = top + getScreenTop (parent )
231
225
width = getValue (args , values ['width' ]) if 'width' in values else 100
232
- height = getValue (args , values ['height' ]) if 'height' in values else 100
233
- right = screenLeft + width
234
- bottom = screenTop + height
235
- src = getValue (args , values ['src' ]) if 'src' in values else None
236
- containerId = getCanvas ().create_rectangle (screenLeft , screenTop , right , bottom , width = 0 )
226
+ source = getValue (args , values ['source' ]) if 'source' in values else None
237
227
if 'name' in values :
238
228
widgetName = values ['name' ]
239
229
else :
240
230
widgetName = None
231
+ if source == None :
232
+ raise (Exception (f'No image source given for \' { id } \' ' ))
233
+ img = (Image .open (source ))
234
+ height = int (round (img .height * width / img .width ))
235
+ resized_image = img .resize ((width , height ), Image .LANCZOS )
236
+ new_image = ImageTk .PhotoImage (resized_image )
237
+ imageid = getCanvas ().create_image (screenLeft , screenTop , anchor = 'nw' , image = new_image )
238
+ images [widgetName ] = {'id' : imageid , "image" : new_image }
241
239
widgetSpec = {
242
240
"type" : "image" ,
243
241
"nme" : widgetName ,
244
- "id" : containerId ,
245
242
"left" : left ,
246
243
"top" : top ,
247
244
"width" : width ,
248
245
"height" : height ,
246
+ "source" : source ,
249
247
"parent" : parent
250
248
}
251
249
elements [widgetName ] = widgetSpec
252
250
zlist .append ({widgetName : widgetSpec })
253
- if src == None :
254
- raise (Exception (f'No image source given for \' { id } \' ' ))
255
- img = (Image .open (src ))
256
- resized_image = img .resize ((width , height ), Image .ANTIALIAS )
257
- new_image = ImageTk .PhotoImage (resized_image )
258
- imageid = getCanvas ().create_image (screenLeft , screenTop , anchor = 'nw' , image = new_image )
259
- images [containerId ] = {'id' : imageid , "image" : new_image }
260
251
return widgetSpec
261
252
262
253
# Create a canvas or render a widget
0 commit comments