code: def loop_tk(kernel):
      """Start a kernel with the Tk event loop."""
  
      from tkinter import READABLE, Tk
  
      app = Tk()
      # Capability detection:
      # per https://docs.python.org/3/library/tkinter.html#file-handlers
      # file handlers are not available on Windows
      if hasattr(app, "createfilehandler"):
          # A basic wrapper for structural similarity with the Windows version
          class BasicAppWrapper:
              def __init__(self, app):
                  self.app = app
                  self.app.withdraw()
  
          def process_stream_events(stream, *a, **kw):
              """fall back to main loop when there's a socket event"""
              if stream.flush(limit=1):
                  app.tk.deletefilehandler(stream.getsockopt(zmq.FD))
                  app.quit()
                  app.destroy()
                  del kernel.app_wrapper
  
          # For Tkinter, we create a Tk object and call its withdraw method.
          kernel.app_wrapper = BasicAppWrapper(app)
  
          notifier = partial(process_stream_events, kernel.shell_stream)
          # seems to be needed for tk
          notifier.__name__ = "notifier"  # type:ignore[attr-defined]
          app.tk.createfilehandler(
              kernel.shell_stream.getsockopt(zmq.FD), READABLE, notifier
          )
          # schedule initial call after start
          app.after(0, notifier)
  
          app.mainloop()
  
      else:
          import asyncio
  
          import nest_asyncio
  
          nest_asyncio.apply()
  
          doi = kernel.do_one_iteration
          # Tk uses milliseconds
          poll_interval = int(1000 * kernel._poll_interval)
  
          class TimedAppWrapper:
              def __init__(self, app, func):
                  self.app = app
                  self.app.withdraw()
                  self.func = func
  
              def on_timer(self):
                  loop = asyncio.get_event_loop()
                  try:
                      loop.run_until_complete(self.func())
                  except Exception:
                      kernel.log.exception("Error in message handler")
                  self.app.after(poll_interval, self.on_timer)
  
              def start(self):
                  self.on_timer()  # Call it once to get things going.
                  self.app.mainloop()
  
          kernel.app_wrapper = TimedAppWrapper(app, doi)
          kernel.app_wrapper.start()
  
imports: import os
  import platform
  import sys
  from functools import partial
  import zmq
  from packaging.version import Version
  from traitlets.config.application import Application
label_desc: Use image node_id 9 for calling a global function with example usage: loop_tk(kernel) without return types