Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import _tkinter or tkinter?

Tags:

python

tkinter

All tutorials simply import tkinter,

I am wondering, though, why not import _tkinter? If my understanding is correct, _tkinter is the actual library in cpython and tkinter is the interface or API.

I am simply trying to grasp the paradigm as I read through some of the tkinter source code. It seems there is some python black magic afoot.

like image 300
IAbstract Avatar asked Jan 22 '18 15:01

IAbstract


People also ask

Is _tkinter the same as tkinter?

Build A Paint Program With TKinter and Python The only difference between Tkinter and tkinter is that Tkinter was initially used with Python 2 and tkinter is used for working with Python 3 or later versions.

Do you need to import tkinter?

Tkinter actually comes along when we install Python. While installing Python, we need to check the td/tk and IDLE checkbox. This will install the tkinter and we need not install it separately. However, if we missed installing Tkinter while installing Python, we can do it later using the pip command.

What is the difference between import tkinter and from tkinter import *?

Using import tkinter as tk It comes with all the modules defined in tkinter. However, to save the major typing efforts, we import the tkinter library with some acronym further that can be used to create an instance of widgets. Thus, the application structures become more aesthetical by using the import tkinter as tk.

What is import tkinter in Python?

Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps − Import the Tkinter module.


2 Answers

_tkinter is a C-based module that wraps an internal tcl/tk interpreter. When you import it, and it only, you get access to this interpreter but you do not get access to any of the python classes.

You certainly can import _tkinter, but then you would have to recreate all of the python interfaces to the tcl/tk functions.

like image 149
Bryan Oakley Avatar answered Sep 20 '22 21:09

Bryan Oakley


In python "_" marks a variable is intended for internal use

This convention is defined in PEP 8, but isn't enforced by Python

You shouldn't import class/modules/variables starting with "_" due to that nature, the developer should allow a property/setter methods to access those attributes..

For python2 use "Tkinter"

For python3 use "tkinter"

http://pep8.org/#descriptive-naming-styles

like image 38
Ragnarok Avatar answered Sep 20 '22 21:09

Ragnarok