can't invoke “event” command: application has been destroyed - python

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!

Related

tkinter .after() invalid command name

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

How to run Python script with a custom button

I want to run the Python script when I press the button. This code only opens the script when I click the button.
import sys
import os
import tkinter
top=tkinter.Tk()
def helloCallBack():
os.system('img.py')
B=tkinter.Button(top,text="Run",command= helloCallBack)
B.pack()
top.mainloop()
How to make it run the img.py
button=Button(top, text="Run",command=helloCallBack)
This will run the function that you define fine.
If you want a python script to appear
def helloCallBack():
os.system('python img.py')

tkinter Message works when run by itself, but when run from my main module it fails

I have a tkinter application that opens a message widget and displays the content of some log files my main application makes. for some reason, this works when i run the extension which opens the window just by clicking it, or running it through command line, but when imported by my main module, i got the following error:
RuntimeError: Calling Tcl from different appartment
I tried simply pasting the code into the main module, but to the same effect. i really do not know what is going on. the code of the message is below.
from tkinter import *
def msg():
error='Sorry, no logs available.'
string=''
win=Tk()
win.title('Log')
try:
num=0
a=open('C:\\ProgramData\\luck\\log.dat')
lines=a.readlines()
a.close()
except:
string=error
while True:
try:
lines[num]=lines[num].replace('|',' ')
lines[num]=lines[num].strip()
lines[num]=lines[num]+'\n'
num+=1
except IndexError:
break
if string!=error:
for line in lines:
string+=line
msg=Message(win, text=string)
msg.config(bg='gray',font=('arabic',16,'normal'))
msg.pack()
mainloop()
msg()
i would put the code of the main module here, but it is pretty long.
Reorganize your code just a bit.
from tkinter import *
def msg(win):
...
if __name__ == '__main__':
root = Tk()
# root.withdraw() # uncomment after revision below
msg(root)
If you import file, call msg(root) where root is the already created Tk() object. Of course, adding the message object to your existing root window is probably not what you want. So revise msg to create a new Toplevel containing the message. Something like
top = Toplevel(win)
top.title('Log')
...
msg = Message(top, text=string)
And uncomment root.withdraw() so you do not see the empty root window when you run the file directly.

Python PyInstaller Tkinter Button Tied to Messagebox Does Nothing

I have a tiny button with a question mark in my app. When clicked, it displays the doc string from a function in my app which helps to explain what the app does.
The problem is, I build a single .exe using pyinstaller and this question mark button does nothing when clicked.
Recreation Steps
save the file below as run.py
open command-prompt
paste pyinstaller.exe --onefile --windowed run.py
go to dist folder and run single exe
click ? button and notice it does nothing
run.py below
import tkinter as tk
from tkinter import ttk
''' pyinstaller.exe --onefile --windowed run.py '''
def someotherfunction():
'''
This is some message that I
want to appear but it currently doesn\'t
seem to work
when I put try to launch a messagebox
showinfo window...
'''
pass
def showhelpwindow():
return tk.messagebox.showinfo(title='How to use this tool',
message=someotherfunction.__doc__)
root = tk.Tk()
helpbutton = ttk.Button(root, text='?', command=showhelpwindow, width=2)
helpbutton.grid(row=0, column=3, sticky='e')
root.mainloop()
My setup:
PyInstaller 3.2
Windows 7
Python 3.4.2
I tried adding --noupx option but that didn't fix it.
EDIT:
I removed --windowed option this time and the console is now showing me an error when I click this button.
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1533, in __call__
File "run.py", line 156, in showhelpwindow
AttributeError: 'module' object has no attribute 'messagebox'
Line 3 below is the solution. I needed to import the tkinter.messagebox module explicitly.
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
''' pyinstaller.exe --onefile --windowed run.py '''
def someotherfunction():
'''
This is some message that I
want to appear but it currently doesn\'t
seem to work
when I put try to launch a messagebox
showinfo window...
'''
pass
def showhelpwindow():
return tk.messagebox.showinfo(title='How to use this tool',
message=someotherfunction.__doc__)
root = tk.Tk()
helpbutton = ttk.Button(root, text='?', command=showhelpwindow, width=2)
helpbutton.grid(row=0, column=3, sticky='e')
root.mainloop()

OUTPUT textbox to a window

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)

Categories