1

I'm a comp sci student studying some compiler design and I have a quick question that bugs me to no end.

I'm currently writing an Interpreter in JavaScript (run on nodeJS) that takes statements like:

x = 4
print x

Which would result in the console output: 4

I can parse these statements pretty easily and have them output stuff to the console window. But how would this process work with GUI applications?

The way I understand it is, let's take Python for example.

When you run python in the command line, it launches a console application which takes in python commands and interprets them. (I know it translates to bytecode first, but it does eventually get interpreted).

So like if it sees 1+1, I understand how it can parse this and return 2 to the console window that it is already running. Python in this case is itself is a console app, so it's intuitive that console output from user-inputted instructions can also be on the console.

I've been able to do THAT. But this ALSO works in python:

from tkinter import*
t = Tk()

How does THAT work? Does the Python Interpreter/VM somehow call a Win32 API function? Or does it draw its own window?

Thank you in advance, for any help given to clarify this.

Lauren 835
  • 291
  • 2
  • 12
  • Does this answer your question? [Where can I find the source code for the \_tkinter module?](https://stackoverflow.com/questions/44798254/where-can-i-find-the-source-code-for-the-tkinter-module) – Mike Scotty Jun 30 '20 at 22:33

3 Answers3

2

Somebody at some point long ago wrote a library that could directly access the screen and turn pixels on or off. Then, they wrote a function that takes two x,y pairs and computes all the pixels that need to be turned on to draw a line. The library would then call the function to turn pixels on or off.

Someone else then created a library that sits on top of that library, and uses it to not just draw lines, but draw boxes or circles and so on.

Someone else creates a library on top of that which can read font descriptions and turn that into text on the screen. And then someone creates a library that combines the font library and the line library to create a library that creates windows and checkbuttons. And then someone figures out how to add color, and object rotation, and 3d effects, and on and on.

Ultimate we end up with something like tkinter, which is a library that has functions for creating an input widget, which calls a tcl/tk library which converts the python to tcl, and which calls an X11 or DirectX or Win32 or Cocoa or OpenGL library which takes the input and calls some other function that ultimately turns a pixel on or off on the physical display.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

When you deal with programming, A LOT of what you are able to do comes down to existing libraries and APIs. If you had to reinvent the wheel every time, you'd never get anything meaningful done.

A simpler example is your print() call. This is mearly a wrapper that writes to stdout. The bash shell / OS you're using handles what happens to stdout. GUIs are more or less the same thing, with just a slightly more complicated path.

tkinter is a python library for generating GUI interfaces. It itself, is nothing more than a wrapper for the more general, Tk library. Tk is a general purpose GUI library that works across platforms. It does this by creating utilizing code that's customized for each operating system's GUI library. It's the OS* itself that ends up creating the GUI.

*This is kind of a generalization as in some operating systems (such as those that utilize something such as Gnome) the GUI interface is more decoupled from the OS than one would often think.

Jacobm001
  • 4,431
  • 4
  • 30
  • 51
  • The shell doesn't handle `stdout`, except that it provides operations to redirect it to files and pipes. – Barmar Jun 30 '20 at 22:41
  • @Barmar: no, but it ultimately displays it and like you said does some of the handling. I was trying to get the general point across without writing a thesis. – Jacobm001 Jun 30 '20 at 22:41
  • So when I parse "print 5" and return "5" as a result to the user, is that the wrong way of implementing the print function? – Lauren 835 Jun 30 '20 at 22:46
  • @Lauren835 These are not comparable things. `print "5"` is a command to write 5 to the output, which is usually `stdout` in a command line application. `return "5"` returns the value of "5" to the thing that called your application, or the function that called it. If that's the main function, it will cause your shell to think the program errored, as any value other than 0 indicates an error. – Jacobm001 Jun 30 '20 at 22:49
  • Sorry, I phrased that question wrong. I meant to say is. in my interpreter, when I parse "print 5", it performs: `console.log("5") in Javascript. Is that the way it should be done? – Lauren 835 Jun 30 '20 at 22:53
0

tkinter is essentially just a Python interface to the Tk library. This is an open source library that runs on all popular operating systems and provides a consistent API for GUI elements.

I don't think Python has any built-in GUI functions, it relies on external libraries like this.

Barmar
  • 741,623
  • 53
  • 500
  • 612