I know it is a stupid question but I am just starting to learn python and i don't have good knowledge of python. My question is what is the difference between
from Tkinter import *
and
import Tkinter as tk
?Why can't i just write
import Tkinter
Could anyone spare a few mins to enlighten me?
from Tkinter import * imports every exposed object in Tkinter into your current namespace.
import Tkinter imports the "namespace" Tkinter in your namespace and
import Tkinter as tk does the same, but "renames" it locally to 'tk' to save you typing
let's say we have a module foo, containing the classes A, B, and C.
Then import foo gives you access to foo.A, foo.B, and foo.C.
When you do import foo as x you have access to those too, but under the names x.A, x.B, and x.C.
from foo import * will import A, B, and C directly in your current namespace, so you can access them with A, B, and C.
There is also from foo import A, C wich will import A and C, but not B into your current namespace.
You can also do from foo import B as Bar, which will make B available under the name Bar (in your current namespace).
So generally: when you want only one object of a module, you do from module import object or from module import object as whatiwantittocall.
When you want some modules functionality, you do import module, or import module as shortname to save you typing.
from module import * is discouraged, as you may accidentally shadow ("override") names, and may lose track which objects belong to wich module.
You can certainly use
import Tkinter
However, if you do that, you'd have to prefix every single Tk class name you use with Tkinter..
This is rather inconvenient.
On the other hand, the following:
import Tkinter as tk
sidesteps the problem by only requiring you to type tk. instead of Tkinter..
As to:
from Tkinter import *
it is generally a bad idea, for reasons discussed in Should wildcard import be avoided?
Writing:
from tkinter import *
results in importing everything that exists in tkinter module
Writing:
import tkinter
results in importing tkinter module but if you do so, in order to be able to call any method you will have to use:
tkinter.function_name()
Related
it doesn't work
i want it to get a number then click those button bellow and then get the result in the message box what should i do?!
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
win=Tk()
#here is my problem
def household():
global s
global math
math="multiply"
x=int(E.get())
s=(x/100)*500
b="your bill is:"+str(s)
messagebox.showinfo("result",b)
def commercial():
global s
x=int(E.get())
if x<=4000000:
s=(x/100)*750
household()
commercial()
E=Entry(win,bg="#87CEFA")
b1=Button(win,text="Household",bg="#4169E1",command=household)
b3=Button(win,text="commercial",bg="#4169E1",command=commercial)
E.place(x=100,y=85,width=100,height=20)
b1.place(x=100,y=165,width=100,height=30)
b3.place(x=150,y=165,width=100,height=30)
You're calling household() and commercial() before you define E. Because you are doing a wildcard import (from tkinter import *) you're importing the constant E from tkinter, which is defined as the string "e".
The solution is:
don't do wildcard imports import tkinter with import tkinter as tk and then use tk. as a prefix for all tkinter objects (tk.Entry(...), tk.Button(...), etc)
Make sure your code runs in the correct order. If you have functions that depend on a variable, make sure those functions aren't called before the variable is defined.
I am currently learning Python 3.xx (3,8 currently to be more specific) as first language and am seeing all the time something like
from tkinter import *
from tkinter import font
Now my question here is...:
when you import from tkiner *(therefore all) why do you import again certain elements? Should it not be included in all ( * )?
Thank you in advance for your time and effort spent on answering my question and have a wonderfull day.
The second line will import tkinter.font, which is a submodule.
import * won't import submodules.
>>> from tkinter import *
>>> font
>>> from tkinter import font
>>> font
<module 'tkinter.font' from 'lib/python3.7/tkinter/font.py'>
Another case where * would not import everything is when the module has an __all__ attribute. (Search for "public names" in the documentation here.)
This question already has an answer here:
Why use Python code "from tkinter import ttk"? [duplicate]
(1 answer)
Closed 4 years ago.
I am learning tkinter and noticed that people import multiple things sometimes.
from tkinter import *
from tkinter import ttk
I was wondering why people do this for many modules, not just tkinter. I always thought that import * meant that you were importing everything from a module. So why do people import more items?
tkinter.ttk is a submodule of tkinter. Submodules aren't guaranteed to be loaded by import *; if you want them, it's safest to import them yourself. (Whether submodules are loaded by import * depends on the presence and contents of an __all__ list, as well as what imports __init__.py performs and what imports have already been performed.)
This question already has answers here:
Python: `from x import *` not importing everything
(3 answers)
Closed 6 years ago.
Python applications using TK GUIs usually call the following import statements:
from tkinter import *
from tkinter import ttk
Isn't calling from tkinter import ttk redundant? I thought ttk would already be imported by calling from tkinter import *
Please explain why ttk needs to be imported separately?
(I already understand that it has "improved widgets". I want to understand why the improved widgets aren't accessible from the from tkinter import * call.)
If you go to your python installed location, you will find that in the python library, tkinter is a folder instead of a .py file. So when you use from tkinter import *, you actually only imported things in your-python-location/lib/tkinter/__init__.py. Things like ttk are actually separate files in tkinter folder(e.g. lib/tkinter/ttk.py, lib/tkinter/scrolledtext.py etc.) Therefore, from tkinter import * and from tkinter import tkk are different commands that import things from different module.
I've been following a tutorial for the tkinter interface in python and it uses the following piece of code to declare a root widget for the program which then has children widgets:
root = Tk()
I'm getting the following error when trying to interpret this piece of code:
Global name Tk() is not defined
I'm fairly sure this is because tkinter has changed since the tutorial; I cannot find any other tutorials that do not use snippets of code like this so they wouldn't work either.
The question I have is in context simple, but searching, I cannot find the answer;
How can I bypass this: what has changed to the syntax of tkinter and what is the new method of sorts to declare a root widget? additionally it would be brilliant if anybody has the knowledge of tkinter to warn me whether the way in which you can add children widgets to the root has changed as well.
Thank you for any and all replies ~ Michael
You probably forgot from Tkinter import * at the top.
Alternatively, there's
import Tkinter
or
import Tkinter as tk
Edit: Generally, you want to use the idiom from <library> import <module>, so for your specific example from Tkinter import Tk would work.
which just allows you type tk.Button, for example, rather than Tkinter.Button throughout your code. And if you're using Python 3.x, the library is lowercase.
import tkinter
For general importing questions, I've seen the Importing Python Modules link referenced a lot on SO.
You seem to have named the file tkinter.py. You cannot name a file with the module you are importing. Python will try to import from your existing file instead of tkinter module. There will be module name collison. Try to rename your file name.
from Tkinter import *
root = Tk()