简体   繁体   中英

Tkinter importing without *?

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...???

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

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(…)

If you * imported tkinter, essentially tkinter. is in the namespace, meaning that to access to tkinter modules without worrying about prefixing it with tkinter. . In this case, geometry("500x500") is a method of Tk() , so you would use it like this

from Tkinter import *

Gui = Tk()
Gui.geometry("500x500")
Gui.mainloop()

Similar objects, such as various widgets, etc. are used the same. For example, a label would be made like this:

from Tkinter import *

Gui = Tk()
label= Label(Gui, text="Hello World!")
label.pack()

Gui.mainloop()

I don't know why someone said that importing * wasn't good cause that isn't true, it's actually better then just importing tkinter. Importing * will make the programming easier. With just tkinter you would need to type tkinter. before doing something, or if you do it as tk, you would need to do tk. , from tkinter import * is the best what you can do.

Instead of doing:

from tkinter import *

You can do:

from tkinter import Tk, Canvas #The widgets you want to use

Or:

import tkinter as tk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM