Tkinter import with PyCharm - python

I want to create a tkinter window using pycharm:
from tkinter import *
root = Tk()
root.mainloop()
Apparently PyCharm tells me that from tkinter import * is an unused import statement, and root = Tk() is an unresolved reference. What's confusing me is that the code works completely fine, a tkinter window shows up, no errors.
How do I fix this?
Edit: PyCharm shows these error whenever I import any other library I have.

from Tkinter import *
root = Tk()
thislabel = Label(root, text = "This is an string.")
thislabel.pack()
root.mainloop()
Use Tkinter not tkinter

from tkinter import*
works just fine. You just have to go to the next line and type something along the lines of
tk = Tk()
or any tkinter code and it will recognize it and work just fine.
from tkinter import*
tk = Tk()
btn = Button(tk, text="Click Me")
btn.pack()
tk.mainloop()
Does that code above work?
Hope this helps

At my case, the file that I was writing had the name "tkinter.py", when I imported the module "tkinter" what PyCharm did was import the file that I was writing, of course the message error: "Cannot find reference Tk in imported module tkinter" appeared. Its a dumb error, but check that you file not called same as module. ;)
EDIT:
If you use "from tkinter import * " you must run it like this:
from tkinter import *
root = Tk()
root.mainloop()
Note the uppercase "T" in "Tk".
If you use "import tkinter as tk" you must run it like this:
import tkinter as tk
root = tk.Tk()
root.mainloop()
Note the "tk" module (lowercase) before "Tk" (uppercase).

maybe check if you installed python in a virtual environment, if so you need to work your project there too

In the end I managed to fix this problem myself, here's what I did:
Deleted the ".idea" file associated with the project.
In PyCharm: File >> Open >> "path to project" >> Ok (reopen project)
Now it it looks as normal as it was before.

I could solve it by doing the following
Delete the .idea file.
Delete the __py_cache__ file.

in python2 it is
from Tkinter import *
and is python 3 it is
from tkinter import *
I hope this helps somehow.

I have found out!!
You are actually have to install tkintertoy to use tkinter in pycharm.

Related

Python tkinter simpledialog

I have a Python script which generates a GUI window with tkinter library. I'd like to make some of it's buttons display a prompt - small window to ask the user for some number (something like in JavaScript). I tried the following command:
x = tkinter.simpledialog.askstring
But it returns an error:
NameError: name 'tkinter' is not defined
and no prompt is generated, although I have imported the library in the script's beginning:
from tkinter import *
from tkinter import simpledialog
Other elements (buttons, labels etc.) in the main window work correctly. Please help.
askstring is part of tkinter.simpledialog so you might import it like so
from tkinter.simpledialog import askstring
usage example
import tkinter as tk
from tkinter.simpledialog import askstring
root = tk.Tk()
x = askstring("Title", "Prompt")
print(x)
root.mainloop()

why does my tkinter interface not show when i run it on python

This is my code please help me
from tkinter import*
root = Tk()
theLabel = Label(root, text='This is too easy')
theLabel.pack()
root.mainloop()
Could be due to the fact that you missed a space when importing the tkinter module. Python will be really strict with those imports
Try changing
from tkinter import*
into
from tkinter import *

Removing Base Window When Using Tkinter Python 3 (Askopenfiledialogue)

This is my first post on here, so bear with me as far as post etiquette goes. I've been struggling to get rid of the base Tkinter window that appears when I'm using the askopenfilename function of Tkinter. I've tried using Withdraw and destroy (in combination and alone) to fix this issue, but it seems to leave my code stuck in a loop and unable to continue to the next sections.
I have seen several solutions to this in Python 2, but I have no idea how they translate to python 3. Tkinter isn't a module I have a lot of experience with, so it is likely a simple oversight I am making.
Any suggestions or comments are much appreciated
Here is a sample of my code I am using (CSV module is for another section of my code)
import csv
from tkinter import filedialog
from tkinter import *
root= Tk()
root.filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
root.mainloop()
Use withdraw to hide root window, deiconify to show root window
import csv
from tkinter import filedialog
from tkinter import *
root=Tk()
# Hide Window
root.withdraw()
filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
# Show Window
root.deiconify()
root.mainloop()

How to clear Tkinter ListBox Python

How to clear a listbox when a button is clicked to re-populate it? The code below is giving me an error.
code:
self.listNodes.delete(0,END)
error:
NameError: name 'END' is not defined
Depending on how you imported tkinter you may have to put end in quotations
Try:
self.listNodes.delete(0,'end')
you can also use:
self.listNodes.delete(0,tk.END)
Replace:
self.listNodes.delete(0,END)
with:
self.listNodes.delete('0','end')
END is a variable of tkinter module, which suggests either a wildcard(from tkinter import *) import or from tkinter import END was supposed to be used.
You can use this is you imported tkinter as:
import tkinter as tk
self.listnodes.delete(0, tk.END)
Or you can do:
from tkinter import *
self.listnodes.delete(0, END)
Destruction is the best way to delete something.
self.listNode.destroy()
and then add Your data again to your ListBox.
You can Distroy list node like this
from tkinter import *
self.listnodes.delete(0, END) *

Visual Studio - NameError: name 'Tk' is not defined

I'm trying to display a simple window using the python Tkinter module in visual studio community 2015. Whenever I tried I get an error message
Here is the code:
from tkinter import *
root = Tk()
theLabel = Labe1(root, text="This is too easy")
theLabel1.pack()
root.mainloop()
Here is the error message:
NameError: name 'Tk' is not defined
How do I solve this problem?
You will get this result if you have some other module in your pythonpath named "tkinter". For example, if you name your program "tkinter.py", or if a file named "tkinter.py" is somewhere in your path.
The fix is to simply rename your file. When you do "import tkinter", it's importing your file rather than the tkinter module.
An easy way to check what was actually imported is to do this:
import tkinter
print("the imported file is", tkinter.__file__)
Looking through some of my other code I noticed one that did the same thing you are trying to do. I modified the your code to match what I had found and I was able to get it to work.
import tkinter as tk
from tkinter import *
root = tk.Tk()
Label(root, text="This is too easy").grid(row=0,column=0)
mainloop()
I have started to use grid instead of pack because it allows more control over the placement of your items.
I have noticed that I get the same error sometimes. Importing tkinter itself is the only way I have found to get around it.

Categories

Resources