I've been trying to read up about how tkinter (or more accurately Tk) works on windows AND linux.
Here's what I've understood so far. Tk is a predefined set of widgets. That is stuff like text boxes, check boxes, drop down lists etc. Tkinter is just a way of translating Tk commands into python. (please correct me if I'm wrong).

So how, then, does Tk manage to provide windows which look 'native' for the different operating systems? For example, if I draw a window with a simple button in linux, it will look just like any other button in the operating system. If I do the same thing on windows, it will look just like a normal button in windows. How does Tk provide different looking windows with the same code?
(I've installed python 3.1 on my windows PC and python came 'out of the box' on my ubuntu linux. I just write code in windows and run it on linux.)

Tkinter is the build in gui(Graphical user interface) of python.
There are many other good gui toolkit like wxpython - PyQt -PyGTK.

Tkinter is just a way of translating Tk commands into python. (please correct me if I'm wrong).

No Tkinter or any other gui toolkit is to display our python code in a graphical way.

if I draw a window with a simple button in linux, it will look just like any other button in the operating system

Yes pretty much,gui toolkit come with many ways to customize look off button and any other widgets.

I like wxpython and it has a cool demo where you can se all widgets live and test them out.
look like this.

So how, then, does Tk manage to provide windows which look 'native' for the different operating systems?

I don't know the way it technically does it, but my best guess is that it uses an API - application programming interface - to standardize how it draws widgets without being aware of operating system details.

http://en.wikipedia.org/wiki/Application_programming_interface

I don't know the way it technically does it, but my best guess is that it uses an API - application programming interface - to standardize how it draws widgets without being aware of operating system details.

http://en.wikipedia.org/wiki/Application_programming_interface

What do you mean by "Standardize how it draws widgets"? Do you mean that tkinter or Tk uses an API of the system telling the system to draw a certain widget instead of drawing the widget on its own?

What do you mean by "Standardize how it draws widgets"? Do you mean that tkinter or Tk uses an API of the system telling the system to draw a certain widget instead of drawing the widget on its own?

Possibly. I don't know. Tkinter could also define its own API.

Don't overlook the fact that the Python installer you use is specific for the Operating System. If you install Python on a Windows OS, the GUI toolkits will use the Windows GUI. A similar thing goes for Linux or the Mac OSX.

Python source code is largely cross platform, but the actual interpreter is specific to each OS. The GUI toolkits follow the same rule.

commented: Good point. +0

So Tkinter (or Tk) does call the operating systems specific GUI software to actually draw the windows? Is that how it creates the 'native' look and feel?

Tkinter is Python's wrapper of the GUI toolkit Tk, originally written in a computer language called TCL. Tkinter has the advantage of a small footprint, also IDLE, the IDE that ships with Python, is written in Tkinter. Whatever program you have has to deal with the Operating System of your computer to be able to do anything.

Yes.. But without getting into all the details, does tkinter itself draw the graphics and get the OS to send it out to the screen or does it tell some component of the operating system to draw the graphics?

Thanks for your reply... I'll check it out and post back

Hi... After reading the documentation, I saw that Tk ultimately relies on Xlib. A wikipedia search on Xlib showed that it was more or less a component of X windows. Since MS Windows doesn't use the X window system, how does tkinter run on windows?

Hi... After reading the documentation, I saw that Tk ultimately relies on Xlib. A wikipedia search on Xlib showed that it was more or less a component of X windows. Since MS Windows doesn't use the X window system, how does tkinter run on windows?

Again, when you install Python on a Windows computer, Tkinter and Tk that install with it rely on the Windows GUI and not Xlib.

ah.. thanks. I think someone ought to correct or modify the python documentation. It just says tkinter uses Xlib. It doesn't mention anything about windows.

Again, when you install Python on a Windows computer, Tkinter and Tk that install with it rely on the Windows GUI and not Xlib.

This does not totally explain why Tkinter windows look so spartan on Windows and look so great on the MAC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.