python - Why does my window reopen when closed? -
i testing friend's tkinter
code, , came across bizarre behaviour.
when utilize windows 'x' button (top-right of window) close tk window (class/method?), closes, reopens sec afterwards. sec click of 'x' button closes window. here's code window (assume button commands defined):
from tkinter import * import os, string, random def generate(): length = 13 chars = string.ascii_letters + string.digits + '!@#$%^&*()' random.seed = (os.urandom(1024)) master = tk() master.title("secure password generator") master.geometry("310x24") master.maxsize(310,24) master.minsize(310,24) text = text(master) ranpass = '' in range(length): ranpass += random.choice(chars) text.insert(insert,"your secure password %s." % ranpass) text.config(state=disabled) text.pack() root = tk() root.title ("sleak test") root.geometry("100x500") app = frame(root) app.grid() button5 = button(app) button5.grid() button5.configure(text = "password generator", fg='green', bg='black', command=generate) root.mainloop() try: root.destroy() except tkinter.tclerror: pass
is there reason have click close button on window twice?
edit: made illustration work. sorry guys! second edit: ok, strange. works perfectly, means there must wrong code elsewhere. got strange.
this may or may not problem, but, quoting tkinter book
:
note: python development environments have problems running tkinter examples one. problem enviroment uses tkinter itself, , mainloop phone call , quit calls interact environment’s expectations. other environments may misbehave if leave out explicit destroy call. if illustration doesn’t behave expected, check tkinter-specific documentation development environment.
…
the destroy phone call required if run illustration under development environments; explicitly destroys main window when event loop terminated. development environments won’t terminate python process unless done.
what means if launch app within idle or tkinter-based ide—or, on windows, in cases, any graphical ide—you may have add together root.destroy()
after root.mainloop()
.
if suspect may problem, , you're using ide, first thing test running problem outside ide. open cmd.exe ("dos prompt") window, , this:
cd c:\path\to\your\program c:\path\to\python\pythonw.exe yourscript.py
if works properly, problem. next step add together root.destroy()
, create sure works both command line , idle.
however, note in cases lead tkinter raising , catching exception , logging spurious error message it. if you're building application distribution, might want like:
root.mainloop() try: root.destroy() except tkinter.tclerror: # when destroy isn't necessary, it's illegal. , # don't know within app whether it's necessary or not. pass
python python-3.x tkinter tk
No comments:
Post a Comment