Can I make graphics in Python without importing a graphics library? If not, why?
Question:
I am learning graphics and GUI in Python. I am curious to know how these modules like pygame
, tkinter
, pyside
work on the backend. I opened their code but they are importing more and when I reached the more there’s something like def Window: --> None ....
Where is the code which is drawing a window on the screen? How can I get that code or how will I be able to make just a simple blank window?
Answers:
Practically? Not really.
Each operating system has its own means of interacting with the graphical subsystem, and provides C libraries (with wrappers available for various languages) for communication with that subsystem.
If communicating with a C library (whether via ctypes
or building a Python module in C) that’s specific to graphics counts as "importing something graphics-specific", the practical, easy, maintainable approaches are ruled out.
Technically? Yes.
Except that some of those interfaces are abstracted in a way that does let Python reasonably communicate with them; a key example of this is X11, which supports communicating with the windowing system over a socket.
Mind, you won’t write a high-performance game this way: X11 supports modern extensions that support shared memory for faster communication than what the traditional socket approach allows; but it’ll let you draw a window.
See python-xlib as a demonstrative example of a pure Python graphics library; anything it can do, you could also do yourself inside your script.
Take its examples/draw.py
as a starting point, and run down all the non-standard-library import
s — if you folded all the imported code into your script, you end up with a script that’s drawing graphics with no graphics-specific imports.
I am learning graphics and GUI in Python. I am curious to know how these modules like pygame
, tkinter
, pyside
work on the backend. I opened their code but they are importing more and when I reached the more there’s something like def Window: --> None ....
Where is the code which is drawing a window on the screen? How can I get that code or how will I be able to make just a simple blank window?
Practically? Not really.
Each operating system has its own means of interacting with the graphical subsystem, and provides C libraries (with wrappers available for various languages) for communication with that subsystem.
If communicating with a C library (whether via ctypes
or building a Python module in C) that’s specific to graphics counts as "importing something graphics-specific", the practical, easy, maintainable approaches are ruled out.
Technically? Yes.
Except that some of those interfaces are abstracted in a way that does let Python reasonably communicate with them; a key example of this is X11, which supports communicating with the windowing system over a socket.
Mind, you won’t write a high-performance game this way: X11 supports modern extensions that support shared memory for faster communication than what the traditional socket approach allows; but it’ll let you draw a window.
See python-xlib as a demonstrative example of a pure Python graphics library; anything it can do, you could also do yourself inside your script.
Take its examples/draw.py
as a starting point, and run down all the non-standard-library import
s — if you folded all the imported code into your script, you end up with a script that’s drawing graphics with no graphics-specific imports.