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()
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 have two scripts that both work:
import tkinter
root = tkinter.Tk()
root.configure(bg='blue')
root.mainloop()
and
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello world!")
text.pack()
root.mainloop()
I want to combine the two scripts to print the text on a blue background, but moving anything from one script to another seems to break it.
I can't figure out if it's about root = tkinter.Tk() vs root = Tk(), or import tkinter vs from tkinter import *, or something entirely different. I can't find a successful combination.
I'm using Ubuntu and Python 3.6.9.
Because you use two different styles when importing tkinter, you will need to modify the code from one file when moving to the other. The code in your first example is the preferred way to do it because PEP8 discourages wildcard imports.
When when you copy the code from the second example, you'll need to add tkinter. to every tkinter command (tkinter.Tk(), tkinter.Text(root), tk.INSERT, etc.
Personally I find import tkinter as tk to be a slight improvement. I find tk.Tk() to be a little easier to type and read than tkinter.Tk().
You should know that:
from tkinter import *
will import all the attribute in the tkinter.But if you also define some variable in your script.It will be covered by your new variable.So we don't recommend you to use that.(If you used both from tkinter.ttk import * and from tkinter import *.Some default widgets of tkinter will be covered by ttk widgets.)
Just like Mr.Bryan said,I'd like to use import tkinter as tk,too.
Tkinter doesnt contain any tk attribute.
import tkinter
window = tkinter.Tk()
win.mainloop()
While running this code it gives me an error saying
module 'tkinter' has no attribute 'Tk'
Did you named your python file tkinter.py or Tkinter.py ? Try to rename it. It may be the cause.
if the file name is tkinter.py in program
import tkinter
it will imports the our file name which is overriders the content there is not Tk() module, so it throw the error
Python 3.x
import tkinter
window = tkinter.Tk()
window.mainloop()
import tkinter
raiz= tkinter.Tk()
raiz.mainloop()
remember that the file name cannot be tkinter.py
Try copying the file to the Python path in C drive (in my case)
And the folder should not contain any other file named Tkinter.py or similar for Code click here
try Tk instead of tk
it worked for me, if you think you are importing wrong,try:
import tkinter
tkinter._test()
In my case, the error occurred in top =tk.Tk()
The simple trick I used was to change the uppercase K in 'TK' to lowercase k
import tkinter as tk
import tkinter.filedialog as fd
from tkinter import *
import PIL
from PIL import ImageTk
from PIL import Image
top =tk.Tk()
top.geometry('800x600')
top.title('Image Processing')
top.configure(background='#CDCDCD')
Your python script name is must not be tkinker.py python could prioritize your script as tkinker and that couldn return that error.
it's capital 'T' and small 'k' =>> 'Tk' not capital K make sure, small mistake
Traceback (most recent call last):
File "/home/pi/sudoku.py", line 3, in <module>
from _tkinter import Tk, Canvas, Frame, Button, BOTH, TOP, BOTTOM
ImportError: cannot import name 'Tk'
I am trying to program a GUI based sudoku game with tkinter. The tutorial I found is in python2, and i've been working to translate it to python 3. The error I keep getting is that Tk, tk, cannot be imported.
Does anyone know why?
I am NEW to coding and programming and yes i've googled it.
From the python documentation: "The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers."
https://docs.python.org/2/library/tkinter.html
I think what you are looking for is something like this:
from tkinter import Tk, Canvas, Frame, Button, BOTH, TOP, BOTTOM
In the import statement it is enough to just say:
import _tkinter as Tk
This imports everything including Canvas, Frame and Button classes. If you do want to import specific classes you need to specify it with a . like this:
import _tkinter.Canvas as TkCanvas
the alias (Tk or TkCanvas) you can choose yourself. Just pick something short and recognisable, also picking the same one as the tutorial makes the tutorial easyer to follow.
Good luck!
I'm trying to make a python tkinter application and I'm having a lot of trouble finding a list of what arguments are available for creating a Tk object.
example:
from tkinter import *
root = Tk(<args>)
root.mainloop()
Where can I find a full list of the arguments available?
For example I know I can give the window a title after it is already created by saying root.title("title"), but can I give the window a title in the constructor directly?
I used the documentation, which can be accessed via IDLE, Is this what you looking for?