ModuleNotFoundError: No module named 'tkSimpleDialog' - Python Shell - python

I'm trying to run this code which I got here:
from tkinter import *
import tkSimpleDialog
import tkMessageBox
root = Tk()
w = Label(root, text= "My Program")
w.pack()
tkMessageBox.showinfo("Welcome", "Add welcome message here")
but this
error occured:
I already look here ModuleNotFoundError: No module named x, but I can't understand the answers. I'm still new about these stuff.

If you are using python 3, you will need to import it as tkinter.simpledialog. Likewise, MessageBox has been changed to tkinter.messagebox.

Related

How to properly import tcl file

I am trying to use the Azure theme in a tkinter application i made. I used the code from github to import the theme(https://github.com/rdbende/Azure-ttk-theme). I put the "azure.tcl" file in the folder with the python file and got the error "_tkinter.TclError: couldn't read file "./theme/light.tcl": no such file or directory". I then tried making a simpler tkinter program with the same ttk theme import code from before but this time in it's own folder with the tcl file. I still got the same error message. When i download the file from github, i ran the example python file and worked just fine.
Here is the code i use to import the file.
import tkinter as tk
from tkinter import Label, ttk
root = tk.Tk()
root.tk.call("source", "azure.tcl")
root.tk.call("set_theme", "dark")
root.geometry("300x300")
L1 = Label(root, text="Hello world")
L1.grid(row=1, column=1)
root.mainloop()
If you do not have this "TKinterModernThemes" library, then you should also install it with below code:
pip install TKinterModernThemes
Then, you can keep your script as it is but just a few arrangements.
Importing Library:
import TKinterModernThemes as TKMT
Threat like window.root is your tk.Tk() object:
import tkinter as tk
from tkinter import Label, ttk
import TKinterModernThemes as TKMT
window = TKMT.ThemedTKinterFrame("%yourProjectName","azure","dark")
window.root.geometry("300x300")
L1 = Label(window.root, text="Hello world")
L1.grid(row=1, column=1)
window.root.mainloop()

I'm getting a NameError saying name 'Tk' is not defined when I'm trying to use Tkinter on VsCode

When I'm trying to run a script to see if I can use tkinter on VsCode it throws a NameError saying name 'Tk' is not defined. Furthermore I can run it on IDLE and it runs just fine. I've been searching around to see if I can fix it but I still can't get it working. Do you have any idea what I'm doing wrong?
Here is the code:
from tkinter import *
root = Tk()
myLabel = Label(root, text = 'Hello World!')
myLabel.pack()
Do NOT name your file tkinter.py because the tkinter module you are trying to import is actually importing the file itself. And since there's no function called Tk in your file, you are getting that error. Rename the file to something else.
For example, rename it to gui.py.
Also, it's better to be explicit rather than implicit in python. So instead of
# Pollutes your namespace
# May clash with the functions you define or functions of other libraries that you import
from tkinter import *
root = Tk()
...
you should use
import tkinter as tk
root = tk.Tk()
...
Here's an example of how it can clash with other namespaces:
from tkinter import *
root = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)
This results in an error:
Traceback (most recent call last):
File "stackoverflow.py", line 98, in <module>
Label1 = Label(gui, text=Label)
TypeError: 'str' object is not callable

ImportError or AttributeError While Using Tkinter

Admittedly I am a noob with Python but I know enough to know that something stupid is going on here and I cannot find any help on google. All I want to do is use Tkinter for a simple GUI but it's pretty hard to do that when I get an Import error any time I try to import and use a function from one.
I followed a tutorial for this one with my own naming etc:
#testUI.py
from tkinter import *
from test2 import nameit
root = Tk()
root.title('Scott Window')
root.geometry('500x350')
greet = nameit('John')
mylabel = Label(root, text=greet)
mylabel.pack(pady=20)
root.mainloop()
#test2.py
def nameit(name):
greeting = name
return greeting
Using this yields:
ImportError: cannot import name 'nameit' from 'test2'
The other way that I have tried is using just "import"
from tkinter import *
import test2
root = Tk()
root.title('Scott Window')
root.geometry('500x350')
greet = test2.nameit('John')
mylabel = Label(root, text=greet)
mylabel.pack(pady=20)
root.mainloop()
This yields an error as well:
AttributeError: module 'test2' has no attribute 'nameit'
I am really at a loss as of what to do, again I'm almost positive it's something stupid but I cannot for the life of me find anything on google, on stackoverflow or anywhere else.
10000 Lifetimes of prosperity to anyone who can help me with this. Thank you!
The example you provided worked on my end, however, this problem may be because of some of the imports you are making in your test2.py file.
For example, in testUI.py you are importing test2 and in test2.py you are importing testUI. You need to find away to break this cycle and reduce the dependence.
An example is shown below:
# testUI.py
from tkinter import *
from test2 import nameit
root = Tk()
root.title('Scott Window')
root.geometry('500x350')
greet = nameit('John')
mylabel = Label(root, text=greet)
mylabel.pack(pady=20)
root.mainloop()
# test2.py
import testUI # Extra import statement
def nameit(name):
greeting = name
return greeting
When you run test2.py, you get the desired result.
When you run testUI.py, however, you get the following error:
from test2 import nameit
ImportError: cannot import name 'nameit' from partially initialized module 'test2' (most likely due to a circular import)

Labels not defined in tkinter app

I'm trying to make a basic window with the text "t" inside using Tkinter, however when running the code the shell spits out "NameError: name 'Label' is not defined". I'm running Python 3.5.2.
I followed the tutorials but the problem is in the label = Label(root, text="test") line.
import tkinter
root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)
label = Label(root, text="test")
label1.pack()
root = mainloop()
Is the label function different in 3.5.2?
You never imported the Label class. Try tkinter.Label
Check the import statements for those tutorials
Maybe they imply from tkinter import *
import tkinter
root = tkinter.Tk()
sheight = root.winfo_screenheight()
swidth = root.winfo_screenwidth()
root.minsize(width=swidth, height=sheight)
root.maxsize(width=swidth, height=sheight)
label = tkinter.Label(root, text="test")
label1.pack()
root = tkinter.mainloop() # <- prob need to fix this as well.
Because you didn't do from tkinter import * you need to invoke the Label from the tkinter module.
Alternatively you can do:
from tkinter import *
...
label = Label(root, text="test")
stumbled across the same Problem. Most beginners guides seem to mess up here.
I had to use a second line in the configuration:
!/usr/bin/python3
import tkinter
from tkinter import *
...
On Windows Operating System your code should run well but on macOS, you will get a problem. I don't know why something like that happen. Anyway try:
import tkinter,
from tkinter import*
And run
After that just write:
from tkinter import *
or
import tkinter
(not both this time)
It's a typo error...
Nothing to do with the import statements.
Label = with a capital L not l
Label(root, text="Username").place(x=20,y=20)
capitalize l

Choosing a file in Python3

Where is the tkFileDialog module in Python 3? The question Choosing a file in Python with simple Dialog references the module using:
from Tkinter import Tk
from tkFileDialog import askopenfilename
but using that (after changing Tkinter to tkinter) in Python 3 gets:
Traceback (most recent call last):
File "C:\Documents and Settings\me\My Documents\file.pyw", line 5, in <module>
import tkFileDialog
ImportError: No module named tkFileDialog
The python 2.7.2 doc (docs.python.org) says:
tkFileDialog
Common dialogs to allow the user to specify a file to open or save.
These have been renamed as well in Python 3.0; they were all made submodules of the new tkinter package.
but it gives no hint what the new names would be, and searching for tkFileDialog and askopenfilename in the 3.2.2 docs returns nothing at all (not even a mapping from the old names to the new submodule names.)
Trying the obvious doesn't do jack:
from tkinter import askopenfilename, asksaveasfilename
ImportError: cannot import name askopenfilename
How do you call the equivalent of askopenfilename() in Python 3?
You're looking for tkinter.filedialog as noted in the docs.
from tkinter import filedialog
You can look at what methods/classes are in filedialog by running help(filedialog) in the python interpreter. I think filedialog.LoadFileDialog is what you're looking for.
You can try something like this:
from tkinter import *
root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "E:/Images",title = "choose your file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)
root.withdraw()

Categories

Resources