Tkinter import without *?

In my last programming, I used the following code:

from tkinter import * Gui = Tk() 

but someone told me that import * was not good for many reasons, but now that I want to import

 from tkinter import geometry 

this says that geometry is not a module thing (name).

and when I do this:

  import tkinter tkinter.geometry(500x500) 

he says that the “module” object does not have the attribute “geometry”

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

+5
source share
4 answers

This is because the tkinter module tkinter not have a geometry function. These are instances of Tk .

Here is a good way to accomplish what you are 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 an imperfect way to import tkinter

Aside, who told you that from tkinter import * was a bad idea was right - when you do this, you load the entire tkinter namespace into the namespace of your module.

If you do this, you may encounter unpleasant namespace conflicts, for example:

 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 have rewritten the link to the tkinter Label widget so that you can no longer create shortcuts! Of course, you should not be obsessed with such local variables as anyway, but why worry about avoiding these namespace problems when you can do this:

 import tkinter as TK 

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

 #import tkinter as TK import newTkinter as TK 

And you are all set. Or, if you want to use another module that has the same names for its classes and methods, the following will lead to chaos:

 from tkinter import * from evilOverlappingModule import * 

but it will be good:

 import tkinter as TK import evilOverlappingModule as evil 
+6
source

The reason that from module import * is considered harmful is because it pollutes the main namespace with any public name in the module. In the best case, this makes the code less explicit; in the worst case, it can cause name conflicts. For example, the codecs module has an open method, and there is a built-in version that takes different arguments. If i write

 from codecs import * f = open(…) 

which will i get? Tkinter has many characters, and tkinter-based programs tend to use them very heavily. better than import *

 import tkinter as tk 

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

 tk.Tk().geometry(…) 
+1
source

If you * imported tkinter, essentially tkinter. located in the namespace, which means that to access the tkinter modules, without worrying about prefixes with tkinter. . In this case, geometry("500x500") is the Tk() method, so you should use it like this:

 from Tkinter import * Gui = Tk() Gui.geometry("500x500") Gui.mainloop() 

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

 from Tkinter import * Gui = Tk() label= Label(Gui, text="Hello World!") label.pack() Gui.mainloop() 
0
source

I don’t know why someone said that importing * is not a good reason, which is not true, in fact it is better than just importing tkinter. Import * makes programming easier. Just using tkinter you will need to type tkinter. before you do something, or if you do it as a mk, you will need to do tk. from tkinter import * is the best you can do.

0
source

Source: https://habr.com/ru/post/1498016/


All Articles