Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter importing without *?

Tags:

python

tkinter

In my past programming i used the following code:

from tkinter import *
Gui = Tk()

but someone told me that importing * was not good for many reasons but now when i want to import

from tkinter import geometry

it says geometry not a module thing (name).

and when i do:

    import tkinter 
tkinter.geometry(500x500)

it says 'module' object has no attribute 'geometry'

Can someone explain me how to import with tkinter all the different ways?? Not only for geometry but most of the tkinter modules...???

like image 795
Shahid Iqbal Avatar asked Aug 20 '13 14:08

Shahid Iqbal


2 Answers

That's because the module tkinter doesn't have a geometry function. It's the Tk instances that do.

Here's a good way to accomplish what you're trying to do:

import tkinter as tk     # tk is a convenient, easy to type alias to use for tkinter.
gui = tk.Tk()
gui.geometry("500x500")   # don't forget the quotes

Why from tkinter import * is a non-ideal way to import tkinter

As an aside, whoever told you that from tkinter import * was a bad idea was correct - when you do that, you load all of tkinter's namespace into your module's namespace.

If you do that, you can end up with unpleasant namespace collisions, like this:

from tkinter import *
gui = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)

# Traceback (most recent call last):
#   File "stackoverflow.py", line 98, in <module>
#     Label1 = Label(gui, text=Label)
# TypeError: 'str' object is not callable

You've overwritten the reference to tkinter's Label widget - so you can't create any more Labels! Of course you shouldn't be capitalizing local variables like that anyways, but why worry about avoiding those namespace problems when you can do this instead:

import tkinter as tk

This ^^^^ import method is also preferable because if at some point you want to swap Tkinter out for another module with a similar implementation, instead of combing through your code for all elements of the Tkinter module, you can just go like this:

#import tkinter as tk
import newTkinter as tk

And you're all set. Or, if you want to use another module that happens to have some of the same names for its classes and methods, the following would cause chaos:

from tkinter import *
from evilOverlappingModule import *

but this would be fine:

import tkinter as tk
import evilOverlappingModule as evil
like image 92
Brionius Avatar answered Sep 22 '22 10:09

Brionius


The reason that from module import * is considered harmful is that it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can cause name collisions. For example, module codecs has an open method defined, and there is the built-in version, which take different arguments. If I write

from codecs import *
f = open(…)

which open will I get? Tkinter has a lot of symbols, and tkinter based programs tend to use them very heavily. better than the import * is

import tkinter as tk

which then allows the — still explicit, but easier to type and read:

tk.Tk().geometry(…)
like image 23
msw Avatar answered Sep 23 '22 10:09

msw