-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
executable file
·213 lines (165 loc) · 8.12 KB
/
demo.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
#!/usr/bin/env python
import tkinter as tk
import tkinter.ttk as ttk
from tkapp import AppRootWindow
from tkshapes import GCanvas, GToggleSwitch
root = tk.Tk()
root.title("This is the title for the root window")
status_text = tk.StringVar()
status_text.set("This is the statusbar text")
root_window = AppRootWindow(root, window_width=800, window_height=600)
root_window.pack(fill="both", expand=True)
root_window.register_status_var(status_text)
statusbar_frame = tk.Frame(root_window)
statusbar_frame.pack(side="bottom", fill="x", expand=True, anchor="s")
statusbar_label = ttk.Label(statusbar_frame, textvariable=status_text, anchor="w")
statusbar_label.pack(side="left", fill="both", expand=True)
gcanvas = GCanvas(root_window, canvas_width=10000, canvas_height=10000)
gcanvas.pack(fill="both", expand=True)
gcanvas.register_status_var(status_text)
# Register builtin shapes
gcanvas.register_builtins()
# The 'BACKGROUND' name will associate this GObject with the immovable background items
graph_paper = gcanvas.create('GGraphPaper', 0, 0, 10000, 10000, name="BACKGROUND")
graph_paper.set_outline_width(2)
graph_paper.set_active_outline_width(0)
graph_paper.show()
# GObjects are "spawned" into existence by the create() method on the GCanvas
#
# An optional "label" can be added to the GObject for display purposes only.
# The label does not need to be unique, and can contain whitespace.
#
# An optional "name" can be added which will be used in the underlying GItem's
# name_tag, and must be unique. Using a unique name can be useful in development
# when printing out debug code, you can more easily see what objects you're
# dealing with if they're named something meaningful.
circle1 = gcanvas.create('GOval', 5390, 5100, 100, 100, label="Circle1")
circle1.add_mouse_bindings()
circle1.show()
circle2 = gcanvas.create('GOval', 5360, 5200, 50, 50, label="Circle2")
circle2.add_mouse_bindings()
circle2.show()
circle3 = gcanvas.create('GOval', 5340, 5250, 20, 20, label="Circle3")
circle3.add_mouse_bindings()
circle3.show()
oval1 = gcanvas.create('GOval', 5330, 5300, 20, 30, label="Oval1")
oval1.add_mouse_bindings()
oval1.show()
oval2 = gcanvas.create('GOval', 5330, 5350, 50, 40, label="Oval2")
oval2.add_mouse_bindings()
oval2.show()
oval3 = gcanvas.create('GOval', 5330, 5400, 100, 130, label="Oval3")
oval3.add_mouse_bindings()
oval3.show()
square1 = gcanvas.create('GRect', 5520, 5030, 100, 100, label="Square1")
square1.add_mouse_bindings()
square1.show()
square2 = gcanvas.create('GRect', 5650, 5060, 75, 75, label="Square2")
square2.add_mouse_bindings()
square2.show()
square3 = gcanvas.create('GRect', 5675, 5150, 25, 25, label="Square3")
square3.add_mouse_bindings()
square3.show()
gate1 = gcanvas.create('GBufferGate', 5060, 5040, label="BufferGate1")
gate1.add_mouse_bindings()
gate1.show()
gate2 = gcanvas.create('GNotGate', 5200, 5040, label="NotGate1")
gate2.add_mouse_bindings()
gate2.show()
gate3 = gcanvas.create('GAndGate', 5060, 5140, label="AndGate1")
gate3.add_mouse_bindings()
gate3.show()
gate4 = gcanvas.create('GOrGate', 5053, 5240, label="OrGate1")
gate4.add_mouse_bindings()
gate4.show()
gate5 = gcanvas.create('GXOrGate', 5060, 5340, label="XOrGate1")
gate5.add_mouse_bindings()
gate5.show()
gate6 = gcanvas.create('GNandGate', 5200, 5140, label="NandGate1")
gate6.add_mouse_bindings()
gate6.show()
gate7 = gcanvas.create('GNorGate', 5200, 5240, label="NorGate1")
gate7.add_mouse_bindings()
gate7.show()
gate8 = gcanvas.create('GXNorGate', 5200, 5340, label="XNorGate1")
gate8.add_mouse_bindings()
gate8.show()
logo = gcanvas.create('GPythonLogo', 5500, 5160, label="TwoSnakes")
logo.add_mouse_bindings()
logo.show()
hand_coords = (
4962, 2086, 5571, 3199, 6061, 4158, 6449, 5009, 6271, 5344, 5932, 5127, 5689, 4692,
5398, 4240, 5005, 4339, 4860, 5018, 4817, 5697, 4935, 6638, 4957, 7335, 4968, 7833,
4898, 8403, 4779, 8557, 4618, 8502, 4575, 8222, 4569, 7706, 4564, 7362, 4483, 6783,
4375, 6122, 4181, 5950, 4052, 6421, 3961, 7145, 3815, 7733, 3691, 8620, 3514, 8855,
3298, 8665, 3347, 8176, 3487, 7443, 3546, 6882, 3724, 5860, 3605, 5860, 3390, 6412,
3142, 7018, 2798, 8113, 2722, 8158, 2561, 8086, 2496, 7869, 2561, 7543, 2873, 6566,
2970, 6222, 3228, 5371, 3126, 5308, 2884, 5579, 2518, 6267, 2313, 6575, 2151, 6566,
2130, 6213, 2351, 5724, 2507, 5353, 2894, 4611, 2991, 4204, 3061, 3633, 3131, 3190,
3239, 2647, 3363, 2357, 3562, 2176,
)
polygon1_coords = [(x // 5) + 3800 for x in hand_coords]
polygon1 = gcanvas.create('GPolygon', coords=polygon1_coords, label="Polygon1")
polygon1.add_mouse_bindings()
polygon1.show()
switch1 = gcanvas.create('GToggleSwitch', 5060, 5420, label="ToggleSwitch1")
switch1.add_mouse_bindings()
switch1.show()
switch2 = gcanvas.create('GToggleSwitch', 5060, 5460, label="ToggleSwitch2")
switch2.add_mouse_bindings()
switch2.show()
bulb1 = gcanvas.create('GLightBulb', 5210, 5500, label="LightBulb1")
bulb1.add_mouse_bindings()
bulb1.show()
def event_handler(event):
print("------------------------------------------------------------------------------")
# Check GCanvas Event Queue for events to handle
if not gcanvas.event_queue.is_empty():
print(f"DEBUG: Event Queue Size = {gcanvas.event_queue.get_qsize()}")
event = gcanvas.event_queue.get_event()
if event:
if event.event_type == 'ClickableClicked':
canvas_item = event.event_data['canvas_item']
g_item = event.event_data['g_item']
g_object = event.event_data['g_object']
print(f"DEBUG: Event:{event.event_id}: canvas_item = {canvas_item}")
print(f"DEBUG: Event:{event.event_id}: GItem = {g_item}")
print(f"DEBUG: Event:{event.event_id}: GObject = {g_object}, label = {g_object.label}, state = {g_object.state}")
if type(g_object) is GToggleSwitch:
g_object.toggle()
if g_object.state:
state = "ON"
else:
state = "OFF"
print(f"DEBUG: Event:{event.event_id}: event_handler(): {g_object.label} is {state}")
elif event.event_type == 'AddConnection':
from_canvas_item = event.event_data['from_canvas_item']
from_g_item = event.event_data['from_g_item']
from_g_object = event.event_data['from_g_object']
from_g_node = event.event_data['from_g_node']
to_canvas_item = event.event_data['to_canvas_item']
to_g_item = event.event_data['to_g_item']
to_g_object = event.event_data['to_g_object']
to_g_node = event.event_data['to_g_node']
print(f"DEBUG: Event:{event.event_id}: from_canvas_item = {from_canvas_item}")
print(f"DEBUG: Event:{event.event_id}: from_g_item = {from_g_item}")
print(f"DEBUG: Event:{event.event_id}: from_g_object = {from_g_object}, label = {from_g_object.label}")
print(f"DEBUG: Event:{event.event_id}: from_g_node = {from_g_node}, name = {from_g_node.name}")
print(f"DEBUG: Event:{event.event_id}: to_canvas_item = {to_canvas_item}")
print(f"DEBUG: Event:{event.event_id}: to_g_item = {to_g_item}")
print(f"DEBUG: Event:{event.event_id}: to_g_object = {to_g_object}, label = {to_g_object.label}")
print(f"DEBUG: Event:{event.event_id}: to_g_node = {to_g_node}, name = {to_g_node.name}")
# Now we can do something with the data that was passed to us via the GEvent...
print(f"DEBUG: Event Queue Size = {gcanvas.event_queue.get_qsize()}")
# Demonstrate how we can receive virtual events from the tkshapes
# library, and then handle each type of event appropriately. We
# bind to the documented virtual event names, and then handle the
# events using one or more handlers. As we can check the event
# type, we can use the same event_handler() in this case.
gcanvas.canvas.bind("<<ClickableClicked>>", event_handler, "+")
gcanvas.canvas.bind("<<AddConnection>>", event_handler, "+")
# Print some debug info
gcanvas.known_types()
gcanvas.known_gobjects()
# Start the Tkinter event loop
root.mainloop()