From: Ben Harris Date: Sun, 26 Nov 2017 10:21:52 +0000 (+0000) Subject: Python 3 compatibility for the editor. X-Git-Tag: bedstead-002.000~51 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=2536b614a2cfb9c8c586ea4cd717336f1e140a66;p=bedstead.git Python 3 compatibility for the editor. It still works in Python 2.7 as well. --- diff --git a/editor.py b/editor.py index 1b8f0c1..0a02705 100644 --- a/editor.py +++ b/editor.py @@ -1,9 +1,15 @@ #!/usr/bin/env python +from __future__ import division, print_function, unicode_literals + import sys import string -from Tkinter import * +try: + from tkinter import * +except ImportError: + # Fall back to Python 2 name for module + from Tkinter import * import subprocess @@ -66,15 +72,15 @@ def regenerate(): cont.canvas.delete(pg) cont.polygons = [] - data = subprocess.check_output(["./bedstead"] + map(str, cont.bitmap)) + data = subprocess.check_output(["./bedstead"] + list(map(str, cont.bitmap))) paths = [] path = None for line in data.splitlines(): words = line.split() - if len(words) >= 3 and words[2] in ["m","l"]: + if len(words) >= 3 and words[2] in [b"m",b"l"]: x = int((float(words[0])-LEFT)*pixel*0.01 + 2*gutter + XSIZE*pixel) y = int((TOP - float(words[1]))*pixel*0.01 + gutter) - if words[2] == "m": + if words[2] == b"m": path = [] paths.append(path) path.append([x,y]) @@ -132,8 +138,8 @@ def regenerate(): def click(event): for dragstartx in gutter, 2*gutter + XSIZE*pixel: - x = (event.x - dragstartx) / pixel - y = (event.y - gutter) / pixel + x = (event.x - dragstartx) // pixel + y = (event.y - gutter) // pixel if x >= 0 and x < XSIZE and y >= 0 and y < YSIZE: cont.dragstartx = dragstartx cont.dragstate = not getpixel(x,y) @@ -142,8 +148,8 @@ def click(event): break def drag(event): - x = (event.x - cont.dragstartx) / pixel - y = (event.y - gutter) / pixel + x = (event.x - cont.dragstartx) // pixel + y = (event.y - gutter) // pixel if x >= 0 and x < XSIZE and y >= 0 and y < YSIZE: setpixel(x, y, cont.dragstate) regenerate() @@ -152,7 +158,7 @@ def drag(event): def key(event): if event.char in (' '): bm = ",".join(map(lambda n: "%03o" % n, cont.bitmap)) - print " {{%s}, 0x }," % bm + print(" {{%s}, 0x }," % bm) elif event.char in ('c','C'): for y in range(YSIZE): for x in range(XSIZE):