How do programming languages draw GUI like Tkinter for example? - python

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.

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.

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.

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.

Related

Python 3.4 how to create a line of text that always stays at the top of the screen while the script runs

I'm trying to find a way to keep a fixed line of text displaying at the top of the screen the entire time a Python script runs. Functionally, it would behave like the top line in nano editor; the program title remains at the top of the screen, even when scrolling through text in the editor area. The idea is that the line would contain the name of the script that's running, and perhaps some dynamic information (progress indicators), and remain visible even when text output beneath that line scrolls.
From what I've researched so far, the curses module could possibly provide the foundation for reaching that goal. I have yet to find a curses example that specifically demonstrates what I'm trying to accomplish, and the learning curve is a bit steep for me at the moment.
The scripts would run on a Linux box without a GUI.
Thanks!
If you want something simpler than curses, then look into using npyscreen.
This framework should be powerful enough to create everything from quick, simple programs to complex, multi-screen applications. It is designed to make doing the simple tasks very quick and to take much of the pain out of writing larger applications.
And it's built on curses.

Importing tkinter modules

Is it possible to just import parts of tkinter, like you can import parts of Java swing without having to import the entire library when you only need to use 4 or 5 modules. I am writing small python program with pop-up input/output window a few textboxes and buttons and only want to use grid layout manager.
I have looked through all the python and tkinter documentation and searched tutorial websites and youtube unable to find an example.
General python/tkinter language query.
You have to have all of tkinter in memory even if you don't use it all. You can import individual pieces like you can with any other python module, but that won't make your program any smaller or more efficient. Under the hood python will import the entire module, even if it only makes part of the module visible to your code.
Arguably, the only real effect would be in making your code a bit harder to understand by deviating from best practices.

How to place text boxes in Python Turtle?

I'm making a mock OS in python Turtle, and I want to make a username and password prompt. Thing is, I don't want a turtle text window, I want just the type box. and I want to be able to place it. Any suggestions?
Why would you want to use turtle?
Turtle was meant to help you get used to the language before you can build any full-fledged applications. This means it is extremely limited in what it can do.
If you are looking to build a GUI application then I recommend you check out TKinter
I should also point out that you will not be able to build an OS using just python as python is a high-level language. This means it cannot directly interact the hardware.
However, you could write the low-level memory management using C and assembly language and write the rest of your high-level code in python.

Can I embed a matplotlib plot in a Tcl/Tk application?

I'm wondering how difficult it will be to use invoke matplotlib from a Tcl interpreter and plot to a Tk canvas created on the Tcl side.
I'm wondering what the best way to do this is.
I'm guessing I'll have to create a python interpreter, pass the canvas handle from the Tcl side to python and make use of the C API in both languages. Or is there a more elegant way?
Getting this integration right won't be easy. However, here's what I'm thinking about:
Connecting Python and Tcl
You can run Python inferior to Tcl apparently. According to this wiki page, there's a package called tclpython which can do the basic integration work for you, and the page linked to appears live. The code appears to be here, though it might need some work to make it build. (I've never tried.)
Connecting matplotlib and Tk
You can make matplotlib draw on a Tkinter window, and as long as you can make that Tkinter window with the right options, it will show up. The key is that the underlying system window IDs can be found out from Tk with winfo id $nameOfTheWindow. If you create a frame with the -container option set to true (which turns off some things and turns on a few others; you'll need to set the size explicitly) then that particular frame will be suitable for use with TkAgg on the python/matplotlib side. The key will be to make a toplevel on that side that has the -use option set to the ID retrieved from winfo id. You probably need to make sure that the python side is running its rendering in a separate thread to the outer Tcl/Tk to make the event handling work right (unless you're keen on doing deep event loop hacking; the situation is that you're actually going to have Tcl/Tk inside Python inside Tcl/Tk, as Tkinter works by delegating to a subordinate Tcl/Tk, and getting all this glued together right will be “a bit tricky”).
If that doesn't work, get matplotlib to render as an image (GIF or PNG; the latter is preferred if you've got Tk 8.6) as you can just show that easily via a photo image. This is definitely going to be possible to do without thread games; it's just passing around pure data. Probably slower and not interactive, but that might not matter for what you're doing, and it is easier in the simple case.
You will probably need the second approach if you're on Windows or OSX, as Tk there doesn't actually use X11. (Unless you're using one of the more perverse configuration options, of course.)

Python compile a script within a GUI

I am currently working on the final year project for my degree. I have chosen to research and develop a tool to aid the delivery of the new Computing curriculum that is coming to schools next year.
I am using a Raspberry Pi in my development, and I aim to teach extremely basic Python programming to children between the ages of 8 and 10. They are going to be able to control some hardware attached to the Pi using a simple API that I am going to create.
My question is: I would like to be able to create a GUI for the children to work in, which would allow them to write and compile scripts. This is mainly to get away from the unfamiliar interface of Linux and terminals etc, and put them in a friendly, basic interface which will pretty much just allow them to write their code and click a big red button to compile and run it to interact with the hardware. Is it possible to allow for text to be written in a GUI and then compiled when the button is pressed?
I am pretty new to Python myself so I am not as clued up as I'd like to be about the specifics of it. I know that it is possible to have the output of IDLE inside of a tkinter interface, and that it is possible to have text boxes for user input and stuff, but would it actually be possible to compile a script on button press and then run it? I have been thinking that maybe threading is the answer. Perhaps I could create a new thread to do it when the button is pressed?
My apologies if this is incredibly basic, but I am having no luck finding any answers about how I would do this. I think it's mainly because I am unsure on what exactly to ask for.
I appreciate any feedback/help, thank you very much.
Dell
Have your GUi write the Python code to a file, then dynamically import using the imp module. I actually do something similar :-)
import imp
hest = imp.load_source("Name", Path)

Categories