swyveu
swyveu

Reputation: 135

Multiple Tkinter windows pop up at runtime after failed attempts

First of all, I'm quite new at Python, Stackoverflow and programming in general so please forgive any formal errors I might have made as I'm still trying to grasp many of the required conceptual programming protocols.

Here's the issue: I'm trying to work around a specific, seemingly simple problem I've been having when using Tkinter: Whenever I'm fiddling with some code that confuses me, it generally takes many attempts until I finally find a working solution. So I write some code, run it, get an error, make some changes, run it again, get another error, change it again... and so on until a working result is achieved.

When the code finally works, I unfortunately also get additional Tkinter main windows popping up for every failed run I've executed. So if I've made, say, 20 changes before I eventually achieve working code, 20 additional Tkinter windows pop up. Annoying...

Now, I was thinking that maybe handling the exceptions with try/except might avoid this, but I'm unsure how to accomplish this properly.

I've been looking for a solution but can't seem to find a post that addresses this issue. I'm actually not really sure how to formulate the problem correctly... Anybody has some advice concerning this?

The following shows a simple but failed attempt of how I'm trying to circumvent this. The code works as is, but if you make a little typo in the code, run it a couple of times, then undo the typo and run the code again, you'll get multiple Tkinter windows which is what I'm trying to avoid.

Any help is, of course, appreciated... (btw, I'm using Python 2.7.13.)

import Tkinter as tk

class App(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self)
        self.root = parent
        self.canvas = tk.Canvas(self)
        self.canvas.pack(expand=1,fill='both')
        self.bindings()

    def click(self,e):
        print 'clicked'

    def bindings(self):
        self.root.bind('<1>',self.click)

def main():
    root = tk.Tk()
    app = App(root)
    app.pack()
    root.mainloop()

if __name__ == '__main__':
    try:
        main()
    except:
        print 'Run failed...'

Upvotes: 1

Views: 249

Answers (1)

swyveu
swyveu

Reputation: 135

Alright, great. The issue has indeed nothing to do with Tkinter or Python but with the IDE itself. Thank you Ethan for pointing that out.

PyScripter has several modes or engines. I've been running scripts with its internal engine which is faster but does not reinitialize with every run. I believe this causes the problem. The remote engine, on the other hand, does reinitialize with every run. This avoids the failed run popups.

A more in-depth explanation from the PyScripter manual below:

Python Engines:

  • Internal

It is faster than the other options however if there are problems with the scripts you are running or debugging they could affect the reliability of PyScripter and could cause crashes. Another limitation of this engine is that it cannot run or debug GUI scripts nor it can be reinitialized.

  • Remote

This the default engine of PyScripter and is the recommended engine for most Python development tasks. It runs in a child process and communicates with PyScripter using rpyc. It can be used to run and debug any kind of script. However if you run or debug GUI scripts you may have to reinitialize the engine after each run.

  • Remote Tk

This remote Python engine is specifically created to run and debug Tkinter applications including pylab using the Tkagg backend. It also supports running pylab in interactive mode. The engine activates a Tkinter mainloop and replaces the mainloop with a dummy function so that the Tkinter scripts you are running or debugging do not block the engine. You may even develop and test Tkinter widgets using the interactive console.

  • Remote Wx

This remote Python engine is specifically created to run and debug wxPython applications including pylab using the WX and WXAgg backends. It also supports running pylab in interactive mode. The engine activates a wx MainLoop and replaces the MainLoop with a dummy function so that the wxPython scripts you are running or debugging do not block the engine. You may even develop and test wxPython Frames and Apps using the interactive console. Please note that this engine prevents the redirection of wxPython output since that would prevent the communication with Pyscripter.

When using the Tk and Wx remote engines you can of course run or debug any other non-GUI Python script. However bear in mind that these engines may be slightly slower than the standard remote engine since they also contain a GUI main loop. Also note that these two engines override the sys.exit function with a dummy procedure.

Upvotes: 1

Related Questions