tags:

views:

64

answers:

1

Hi all -

I'm a Python(3.1.2)/emacs(23.2) newbie teaching myself tkinter using the pythonware tutorial found here. Relevant code is pasted below the question.

Question: when I click the Hello button (which should call the say_hi function) why does the inferior python shell (i.e. the one I kicked off with C-c C-c) wait to execute the say_hi print function until I either a) click the Quit button or b) close the root widget down? When I try the same in IDLE, each click of the Hello button produces an immediate print in the IDLE python shell, even before I click Quit or close the root widget.

Is there some quirk in the way emacs runs the Python shell (vs. IDLE) that causes this "lagged" behavior? I've noticed similar emacs lags vs. IDLE as I've worked through Project Euler problems, but this is the clearest example I've seen yet.

FYI: I use python.el and have a relatively clean init.el...

(setq python-python-command "d:/bin/python31/python")

is the only line in my init.el.

Thanks,

Mike

=== Begin Code===

from tkinter import *

class App:

    def __init__(self,master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print("hi there, everyone!")

root = Tk()

app = App(root)

root.mainloop()
+2  A: 

I'd guess that not being attached to a tty, the Python interpreter (via C stdio) switches to block buffered from line buffered and doesn't flush stdout until it closes. Running os.isatty(1) in an "Inferior Python:run Shell Compile" buffer returns false, thus adding weight to this guess.

def say_hi(self):
    print("hi there, everyone!")
    sys.stdout.flush()

May make a difference.

msw
That worked on the print issue. For some strange reason, the inferior buffer hangs on quit/close. A different issue, though...thanks much for your help.
MikeRand
The event loop in Tkinter has some odd properties that I've never explored in depth. I know that Tkinter under IDLE behaves oddly, I'm guessing because of dueling event loops.
msw
Good perspective...something for when I get much better at this. Thanks again.
MikeRand