1
+ # Pyctures.py
2
+
3
+ import sys , json
1
4
import tkinter as tk
2
5
from PIL import Image , ImageTk
3
- import json
4
6
5
7
elements = {}
6
8
zlist = []
7
9
images = {}
8
10
onTick = None
9
11
12
+ # Get the canvas
13
+ def setCanvas (c ):
14
+ global canvas
15
+ canvas = c
16
+
17
+ # Get the canvas
18
+ def getCanvas ():
19
+ global canvas
20
+ return canvas
21
+
10
22
def createScreen (values ):
11
23
global screen , canvas , screenLeft , screenTop , running
12
24
running = True
13
25
screen = tk .Tk ()
26
+ screen .title ('RBR Simulator' )
14
27
# screen.attributes('-fullscreen', True)
15
28
16
- screen .overrideredirect (True )
17
- screenLeft = values ['left' ]['content' ] if 'left' in values else 0
18
- screenTop = values ['top' ]['content' ] if 'top' in values else 0
29
+ # screen.overrideredirect(True)
19
30
width = values ['width' ]['content' ] if 'width' in values else 600
20
31
height = values ['height' ]['content' ] if 'height' in values else 800
32
+ screenLeft = int ((screen .winfo_screenwidth () - width ) / 2 )
33
+ screenTop = int ((screen .winfo_screenheight () - height ) / 2 )
34
+ if 'left' in values :
35
+ screenLeft = values ['left' ]['content' ]
36
+ if 'top' in values :
37
+ screenTop = values ['top' ]['content' ]
21
38
22
- # screenLeft = int(screen.winfo_screenwidth() - width)
23
- # screenTop = int((screen.winfo_screenheight() / 2) - (height / 2))
24
39
geometry = str (width ) + 'x' + str (height ) + '+' + str (screenLeft ) + '+' + str (screenTop )
25
40
screen .geometry (geometry )
26
41
27
42
# Handle a click in the screen
28
43
def onClick (event ):
29
44
global screenLeft , screenTop , zlist
30
- x = event .x_root
31
- y = event .y_root
45
+ x = event .x
46
+ y = event .y
32
47
# print('Clicked at : '+ str(x) +","+ str(y))
33
48
for i in range (1 , len (zlist ) + 1 ):
34
49
element = zlist [- i ]
35
50
id = list (element )[0 ]
36
51
values = element [id ]
37
- x1 = screenLeft + values ['left' ]
52
+ x1 = values ['left' ]
38
53
x2 = x1 + values ['width' ]
39
- y1 = screenTop + values ['top' ]
54
+ y1 = values ['top' ]
40
55
y2 = y1 + values ['height' ]
41
56
if x >= x1 and x < x2 and y >= y1 and y < y2 :
42
57
if id in elements :
@@ -45,21 +60,22 @@ def onClick(event):
45
60
element ['cb' ]()
46
61
break
47
62
else :
48
- RuntimeError (command [ 'program' ] , f'Element \' { id } \' does not exist' )
63
+ RuntimeError (None , f'Element \' { id } \' does not exist' )
49
64
50
65
screen .bind ('<Button-1>' , onClick )
51
66
52
67
fill = values ['fill' ]['content' ] if 'fill' in values else 'white'
53
68
canvas = tk .Canvas (master = screen , width = width , height = height , bg = fill )
54
69
canvas .place (x = 0 , y = 0 )
70
+ setCanvas (canvas )
55
71
56
72
# Set up a click handler in an element
57
73
def setOnClick (id , cb ):
58
74
global elements
59
75
if id in elements :
60
76
elements [id ]['cb' ] = cb
61
77
else :
62
- RuntimeError (command [ 'program' ] , f'Element \' { id } \' does not exist' )
78
+ RuntimeError (None , f'Element \' { id } \' does not exist' )
63
79
return
64
80
65
81
# Set up the tick handler
@@ -76,11 +92,11 @@ def afterCB(screen):
76
92
screen .after (100 , lambda : afterCB (screen ))
77
93
screen .after (1000 , lambda : afterCB (screen ))
78
94
screen .mainloop ()
79
- return
95
+ sys . exit ()
80
96
81
97
# Render a graphic specification
82
98
def render (spec , parent ):
83
- global canvas , elements
99
+ global elements
84
100
85
101
def getValue (args , item ):
86
102
if item in args :
@@ -90,7 +106,7 @@ def getValue(args, item):
90
106
return item
91
107
92
108
def renderIntoRectangle (widgetType , values , offset , args ):
93
- global canvas , zlist
109
+ global zlist
94
110
left = getValue (args , values ['left' ]) if 'left' in values else 10
95
111
top = getValue (args , values ['top' ]) if 'top' in values else 10
96
112
left = offset ['dx' ] + left
@@ -106,9 +122,9 @@ def renderIntoRectangle(widgetType, values, offset, args):
106
122
else :
107
123
outlineWidth = 0
108
124
if widgetType == 'rect' :
109
- widgetId = canvas .create_rectangle (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
125
+ widgetId = getCanvas () .create_rectangle (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
110
126
elif widgetType == 'ellipse' :
111
- widgetId = canvas .create_oval (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
127
+ widgetId = getCanvas () .create_oval (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
112
128
else :
113
129
return f'Unknown widget type \' { widgetType } \' '
114
130
if 'id' in values :
@@ -141,7 +157,6 @@ def renderIntoRectangle(widgetType, values, offset, args):
141
157
return None
142
158
143
159
def renderText (values , offset , args ):
144
- global canvas
145
160
left = getValue (args , values ['left' ]) if 'left' in values else 10
146
161
top = getValue (args , values ['top' ]) if 'top' in values else 10
147
162
left = offset ['dx' ] + left
@@ -177,25 +192,26 @@ def renderText(values, offset, args):
177
192
if xoff < 3 :
178
193
xoff = 3
179
194
if shape == 'ellipse' :
180
- containerId = canvas .create_oval (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
195
+ containerId = getCanvas () .create_oval (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
181
196
else :
182
- containerId = canvas .create_rectangle (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
197
+ containerId = getCanvas ().create_rectangle (left , top , right , bottom , fill = fill , outline = outline , width = outlineWidth )
198
+ textId = canvas .create_text (left + xoff , fontTop + adjust , fill = color , font = f'"{ fontFace } " { fontSize } { fontWeight } ' , text = text , anchor = anchor )
183
199
if 'id' in values :
184
200
id = getValue (args , values ['id' ])
185
201
widgetSpec = {
186
- "id" : containerId ,
202
+ "id" : textId ,
203
+ "containerId" : containerId ,
187
204
"left" : left ,
188
205
"top" : top ,
189
206
"width" : width ,
190
207
"height" : height
191
208
}
192
209
elements [id ] = widgetSpec
193
210
zlist .append ({id : widgetSpec })
194
- canvas .create_text (left + xoff , fontTop + adjust , fill = color , font = f'"{ fontFace } " { fontSize } { fontWeight } ' , text = text , anchor = anchor )
195
211
return None
196
212
197
213
def renderImage (values , offset , args ):
198
- global canvas , images
214
+ global images
199
215
left = getValue (args , values ['left' ]) if 'left' in values else 10
200
216
top = getValue (args , values ['top' ]) if 'top' in values else 10
201
217
left = offset ['dx' ] + left
@@ -205,7 +221,7 @@ def renderImage(values, offset, args):
205
221
right = left + width
206
222
bottom = top + height
207
223
src = getValue (args , values ['src' ]) if 'src' in values else None
208
- containerId = canvas .create_rectangle (left , top , right , bottom , width = 0 )
224
+ containerId = getCanvas () .create_rectangle (left , top , right , bottom , width = 0 )
209
225
if 'id' in values :
210
226
id = values ['id' ]
211
227
widgetSpec = {
@@ -222,7 +238,7 @@ def renderImage(values, offset, args):
222
238
img = (Image .open (src ))
223
239
resized_image = img .resize ((width , height ), Image .ANTIALIAS )
224
240
new_image = ImageTk .PhotoImage (resized_image )
225
- imageid = canvas .create_image (left , top , anchor = 'nw' , image = new_image )
241
+ imageid = getCanvas () .create_image (left , top , anchor = 'nw' , image = new_image )
226
242
images [containerId ] = {'id' : imageid , "image" : new_image }
227
243
return None
228
244
@@ -270,4 +286,14 @@ def getElement(name):
270
286
if name in elements :
271
287
return elements [name ]
272
288
else :
273
- RuntimeError (None , f'Element \' { name } \' does not exist' )
289
+ RuntimeError (None , f'Element \' { name } \' does not exist' )
290
+
291
+ # Set the content of a text widget
292
+ def setText (name , value ):
293
+ getCanvas ().itemconfig (getElement (name )['id' ], text = value )
294
+
295
+ # Set the background of a rectangle or ellipse widget
296
+ def setBackground (name , value ):
297
+ id = getElement (name )['id' ]
298
+ getCanvas ().itemconfig (getElement (name )['id' ], fill = value )
299
+
0 commit comments