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) *
Related
Question 1: I have a non useful window that appears when using tkinter in spyder.
Any solution for this issue ?
Question 2: Why there is a warning message on 'from tkinter import *' ?
Code:
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter import messagebox
box = Tk()
name = askstring('Name','What is your name?')
messagebox.showinfo('Hello!','Hi, {}'.format(name))
box.mainloop()
The "non useful" window is simply box.
messagebox will open a new window. So you can just remove box if you don't intend to use it further.
It's usually not recommended to import everything from a module because it could cause name conflicts with other modules or built-in function:
import tkinter as tk
from tkinter.simpledialog import askstring
name = askstring('Name','What is your name?')
tk.messagebox.showinfo('Hello!','Hi, {}'.format(name))
The additional window is the instance of Tk most often named root cause every other window or widget is a child of the root window. You will need it to initiate your messagebox but if you don't want to look at it you have several choices.
My personal recommendation is to us overrideredirect which will discard it from the taskbar and use withdraw to actually hide it in the screen/monitor. But you may prefer wm_attributes('-alpha', 0) over it to make it opaque/transparent.
Avoiding wildcard imports is recommanded because of name clashes/collisions. For example tkinter has a PhotoImage class, so does pillow. If you have wildcard imports on both, on PhotoImage will overwrite the other in the global namespace.
Code:
import tkinter as tk
from tkinter.simpledialog import askstring
from tkinter import messagebox
box = tk.Tk()
box.overrideredirect(True)
box.withdraw()
name = askstring('Name','What is your name?') #blocks the code block
messagebox.showinfo('Hello!','Hi, {}'.format(name)) #shows message
box.after(5000, box.destroy) #destroy root window after 5 seconds
box.mainloop()#blocks until root is destroyed
For the first question answer is that you don't need to create box because function askstring create frame on it's own. So if the whole program is just to ask for the name and to greet user, you are perfectly fine with just this piece of code:
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter import messagebox
name = askstring('Name','What is your name?')
messagebox.showinfo('Hello!','Hi, {}'.format(name))
And for the second question you need to post what warning you get.
Here I am using from tkinter import * inside a function and when I am running the code it is showing me a SyntaxError.
Please tell me how I can use from tkinter import * inside a function.
You should move the import statements to the beginning of the file, at the top. You are also importing tkinter twice.
I have a problem that annoys me. I am currently building a small app with a Tkinter GUI.
On the front page, I want some introductory text in either a text or a scrolledtext widget. The code examples I've come across uses keywords such as INSERT, CURRENT and END for indexation inside the widget.
I have literally copy pasted the below code into my editor, but it doesn't recognise INSERT (throws error: "NameError: name 'INSERT' is not defined"):
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
I can get the code to work if I change [INSERT] with [1.0], but it is very frustrating that I cannot get INSERT to work, as I've seen it in every example code I've come across
Use tk.INSERT instead of only INSERT. Full code is shown.
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(tk.INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
You don't need to use the tkinter constants. I personally think it's better to use the raw strings "insert", "end", etc. They are more flexible.
However, the reason the constants don't work for you is that you're not directly importing them. The way you're importing tkinter, you need to use tk.INSERT, etc.
INSERT could not be used directly.
You can use it in the past just because you used this in the past:
from tkinter import * # this is not a good practice
INSERT,CURRENT and END are in tkinter.constants.Now in your code,you even didn't import them.
If you want to use them,you can use
from tkinter.constants import * # not recommended
...
txt.insert(INSERT,'You text goes here')
Or
from tkinter import constants
...
txt.insert(constants.INSERT,'You text goes here') # recommend
If didn't want to import them,you can also use:
txt.insert("insert",'You text goes here')
Edit:I found in the source code of tkinter,it had import them,reboot's answer is also OK.
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.
In my past programming i used the following code:
from tkinter import *
Gui = Tk()
but someone told me that importing * was not good for many reasons but now when i want to import
from tkinter import geometry
it says geometry not a module thing (name).
and when i do:
import tkinter
tkinter.geometry(500x500)
it says 'module' object has no attribute 'geometry'
Can someone explain me how to import with tkinter all the different ways?? Not only for geometry but most of the tkinter modules...???
That's because the module tkinter doesn't have a geometry function. It's the Tk instances that do.
Here's a good way to accomplish what you're 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 a non-ideal way to import tkinter
As an aside, whoever told you that from tkinter import * was a bad idea was correct - when you do that, you load all of tkinter's namespace into your module's namespace.
If you do that, you can end up with unpleasant namespace collisions, like this:
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've overwritten the reference to tkinter's Label widget - so you can't create any more Labels! Of course you shouldn't be capitalizing local variables like that anyways, but why worry about avoiding those namespace problems when you can do this instead:
import tkinter as tk
This ^^^^ import method is also preferable because if at some point you want to swap Tkinter out for another module with a similar implementation, instead of combing through your code for all elements of the Tkinter module, you can just go like this:
#import tkinter as tk
import newTkinter as tk
And you're all set. Or, if you want to use another module that happens to have some of the same names for its classes and methods, the following would cause chaos:
from tkinter import *
from evilOverlappingModule import *
but this would be fine:
import tkinter as tk
import evilOverlappingModule as evil
The reason that from module import * is considered harmful is that it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can cause name collisions. For example, module codecs has an open method defined, and there is the built-in version, which take different arguments. If I write
from codecs import *
f = open(…)
which open will I get? Tkinter has a lot of symbols, and tkinter based programs tend to use them very heavily. better than the import * is
import tkinter as tk
which then allows the — still explicit, but easier to type and read:
tk.Tk().geometry(…)
If you * imported tkinter, essentially tkinter. is in the namespace, meaning that to access to tkinter modules without worrying about prefixing it with tkinter.. In this case, geometry("500x500") is a method of Tk(), so you would use it like this
from Tkinter import *
Gui = Tk()
Gui.geometry("500x500")
Gui.mainloop()
Similar objects, such as various widgets, etc. are used the same. For example, a label would be made like this:
from Tkinter import *
Gui = Tk()
label= Label(Gui, text="Hello World!")
label.pack()
Gui.mainloop()
I don't know why someone said that importing * wasn't good cause that isn't true, it's actually better then just importing tkinter. Importing * will make the programming easier. With just tkinter you would need to type tkinter. before doing something, or if you do it as tk, you would need to do tk., from tkinter import * is the best what you can do.
Instead of doing:
from tkinter import *
You can do:
from tkinter import Tk, Canvas #The widgets you want to use
Or:
import tkinter as tk
Here’s a quick answer: Tkinter doesn’t have a geometry function. You can use the geometry function with instances of the Tk() class.
Example:
import tkinter as tk # Use the module name ’Tkinter’ if you have Python 2
root = tk.Tk()
root.geometry(‘500x500’) # You can change the size
# Other code goes here
root.mainloop()
Just like the geometry function, the mainloop, pack, grid, place, config etc. functions are used with the instances of the class Tk()
Thank You! Hope that works!