I work on Python 3.10.2 and I just start to work with tkinter and I always have a problem with .after() method. For example with this program :
import tkinter as tk
def initialisation(compteur) :
compteur=compteur+1
print(compteur)
root.after(2000,initialisation, compteur)
root=tk.Tk()
initialisation(0)
root.mainloop()
I have this error :
1
invalid command name "1720125185344initialisation"
while executing
"1720125185344initialisation"
("after" script)
2
3
It works but I still have this error 1 time. I have looked for solutions but I don't understand.
Thank you in advance for your help.
try adding del root at the end of the script:
import tkinter as tk
def initialisation(compteur) :
compteur=compteur+1
print(compteur)
root.after(2000,initialisation, compteur)
root=tk.Tk()
initialisation(0)
root.mainloop()
del root
Related
I have a simple code (test.py) to generate a popup window as shown below. It works fine when I run it directly in console.
import tkinter as tk
from tkinter.messagebox import showinfo
def popup():
print("Hello World!")
root1 = tk.Tk()
b1 = tk.Button(root1, text="Print", command=popup)
b1.pack(fill='x')
root1.mainloop()
But when I call this code (test.py) from another py script by
exec(open('test.py').read())
It gives error message "can't invoke “event” command: application has been destroyed". I have checked the prior discussion on this topic, but it doesn't seem to help my case.
can't invoke "event" command: application has been destroyed
Can anybody please help? Thanks!
I am using Atom to do this, I am asking how to view a variable in the console while also having the TKinter GUI loaded.
My code is as follows:
import random
import tkinter as tk
root = tk.Tk()
rd1 = random.randint(1, 20)
root.mainloop()
Running the program only shows the tk window but I am wondering how I can view the outcome of rd1 from the console, without having to display it in the GUI.
The console becomes a black ver and will only display text when closing the GUI.
Text as follows:
Process returned 0 (0x0) execution time : 1.298 s
Press any key to continue . . .
printing it will show it in the console; sometime it is buffered, so you can add the optional argument flush=True to the print statement.
import random
import tkinter as tk
root = tk.Tk()
rd1 = random.randint(1, 20)
print(rd1, flush=True) # flush will output immediately
root.mainloop()
I am a newby to python and am struggling with the basic code below. I am uncertain why I am not getting a Tk window to open. The code I have thus far is:
import tkinter as tk
from tkinter import tkk
class Application(tk.Tk):
"""Application root window"""
if __name__ == "__main__":
app = Application()
app.mainloop()
The error I receive back from my terminal is:
ImportError: cannot import name 'tkk' from 'tkinter'
(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py)
Any ideas?
Try this out instead: from tkinter import ttk.
I am trying to use tkinter with ibPy. I am using Spyder (Spyder 2.3.0). When I enter the sample program
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
I receive the message:
File "/Users/Ameade/Tkinter.py", line 8, in <module>
from tkinter import *
ImportError: No module named tkinter
Do you know where I can get this module? I am working on a Mac (OSX 10.9.4).
It seems you named your sample program file Tkinter.py. You should change this name to something else and it should work.
EDIT
As Kevin said, give to the file any other name but not the name of a python module (the extension must remain .py), so you could name it my_amazing_program.py. And keep the content of the file the same as you originally posted it - if you're using python 3+:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If you're using python 2+ change tkinter to Tkinter on the import line:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
You may need to check the version of python you are using.
import tkinter works on python 3(instead of Tkinter)
I tried to execute the following code in my system, and the window does not respond if the submit button is clicked
import Tkinter as tk
from Tkinter import *
top=Tk()
text=Text(top)
def onsubmit():
a=v.get()
ea.textbox(text=a)
v=StringVar()
t=Entry(top, textvariable=v)
submit=Button(top,text="SUBMIT",command=onsubmit)
t.grid(row=0,column=0)
submit.grid(row=0,column=1)
text.grid(row=1,column=0)
top.mainloop()
If you run your script from terminal or just look at the text output of your program in some other way you will see the following error (just after pressing the button):
NameError: global name 'ea' is not defined
The error is on the second line of onsubmit function. Here is the working version:
def onsubmit():
a=v.get()
text.insert(INSERT, a)