Skip to content

Commit cd60e90

Browse files
phkeeseMargarete01timfel
committed
Add rudimentary support for _tkinter in GraalPy
This commit adds the _tkinter module to the list of built-in modules to allow use of tkinter. Before use, the bindings need to be built using tklib_build.py. The code was modified from existing code in PyPy's main branch. Right now, opening a window under Linux works, but still crashes on interacting with UI elements such as buttons. It seems like there is a bug in the `cffi` module causing a SegFault. macOS complains about other threads accessing the main window and the build script has some trouble finding the correct library for Tcl and Tk, as macOS comes with an older version preinstalled. Windows was not tested. Co-authored-by: Margarete Dippel <[email protected]> Co-authored-by: Tim Felgentreff <[email protected]>
1 parent faeb128 commit cd60e90

File tree

4 files changed

+1131
-0
lines changed

4 files changed

+1131
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# _tkinter package -- low-level interface to libtk and libtcl.
2+
#
3+
# This is an internal module, applications should "import tkinter" instead.
4+
#
5+
# This version is based PyPy which itself is based on cffi, and is a translation of _tkinter.c
6+
# from CPython, version 2.7.4.
7+
8+
import sys
9+
10+
class TclError(Exception):
11+
pass
12+
13+
from .tklib_cffi import ffi as tkffi, lib as tklib
14+
15+
from .app import TkApp
16+
from .tclobj import TclObject as Tcl_Obj
17+
from .app import FromTclString, ToTCLString
18+
19+
TK_VERSION = FromTclString(tkffi.string(tklib.get_tk_version()))
20+
TCL_VERSION = FromTclString(tkffi.string(tklib.get_tcl_version()))
21+
22+
READABLE = tklib.TCL_READABLE
23+
WRITABLE = tklib.TCL_WRITABLE
24+
EXCEPTION = tklib.TCL_EXCEPTION
25+
DONT_WAIT = tklib.TCL_DONT_WAIT
26+
27+
def create(screenName=None, baseName=None, className=None,
28+
interactive=False, wantobjects=False, wantTk=True,
29+
sync=False, use=None):
30+
return TkApp(screenName, baseName, className,
31+
interactive, wantobjects, wantTk, sync, use)
32+
33+
def dooneevent(flags=0):
34+
return tklib.Tcl_DoOneEvent(flags)
35+
36+
37+
def _flatten(item):
38+
def _flatten1(output, item, depth):
39+
if depth > 1000:
40+
raise ValueError("nesting too deep in _flatten")
41+
if not isinstance(item, (list, tuple)):
42+
raise TypeError("argument must be sequence")
43+
# copy items to output tuple
44+
for o in item:
45+
if isinstance(o, (list, tuple)):
46+
_flatten1(output, o, depth + 1)
47+
elif o is not None:
48+
output.append(o)
49+
50+
result = []
51+
_flatten1(result, item, 0)
52+
return tuple(result)
53+
54+
# Encoding is not specified explicitly, but "must be passed argv[0]" sounds like a simple conversion to raw bytes.
55+
tklib.Tcl_FindExecutable(ToTCLString(sys.executable))

0 commit comments

Comments
 (0)