Re: Strang thing in tkinter, and pdb too?

2017-06-12 Thread Christian Gollwitzer

Am 12.06.17 um 06:03 schrieb Terry Reedy:

On 6/11/2017 10:06 PM, jf...@ms4.hinet.net wrote:

 D:\Temp\widget-tour-py3>python canvasruler.py
 can't invoke "event" command: application has been destroyed
 while executing
 "event generate $w <>"
 (procedure "ttk::ThemeChanged" line 6)
 invoked from within
 "ttk::ThemeChanged"


I have seen this too.  It is not specific to any python/tkinter code, 
but is specific to exiting with some things destroyed and something not.

This must be coming from tcl or tk, not python or tkinter.


I can confirm that this is a Tk error message, with the following cause:
In Tk, the root window with the name ".", which in Python comes to live 
with the Tk() function, determines the lifetime of a program. If you  do:


root=Tk()

an empty window appears. If now the user closes this window with the "x" 
button on top, Tk stops - because usually this means that the program 
should stop - but Tcl does continue, in order to clean up stuff before 
leaving. If now further Tk commands are sent, they give the mentioned error:


>>> import Tkinter
>>> root=Tkinter.Tk()
# here the window was closed using the "x" button

>>> root

# however the Tk() object still exists,
# which is a Tcl interpreter with a dysfunct Tk

>>> root.update()
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", 
line 1019, in update

self.tk.call('update')
_tkinter.TclError: can't invoke "update" command:  application has been 
destroyed

>>>

This behaviour makes the root window special in the sense that the whole 
application stops, when it is closed, because closing this window exits 
the mainloop().


This makes sense for many applications, however sometimes it does not. 
If you wish to create many windows which pop up by themselves and the 
user can close them freely, then one should withdraw the main window:


root=Tkinter.Tk()
root.withdraw()

now the main window disappears, the user can't incidentally close it. 
One can then create additional toplevel windows using


t=Tkinter.Toplevel()

and place anything there. These can be closed without destroying the 
application / exiting the main loop.


Christian

--
https://mail.python.org/mailman/listinfo/python-list


Re: Strang thing in tkinter, and pdb too?

2017-06-12 Thread jfong
Terry Reedy於 2017/06/12 UTC+8 12:04:18PM wrote:
> Right.  I got this with IDLE tests before using ttk.  Good luck tracing 
> this to its origin.

A little progress. If I remove temp.destroy() at line 34 then that message is 
gone. hmm...trying to find another way of doing it:-)

--Jach Fong
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Strang thing in tkinter, and pdb too?

2017-06-11 Thread Terry Reedy

On 6/11/2017 10:06 PM, jf...@ms4.hinet.net wrote:

I had donwload wdiget-tour-py3.tar.gz examples from this site:
http://www.hullsvle.ch/moodle/mod/resource/view.php?id=6697
and run one of its scripts canvasruler.py, I get stange result.

First, when I run it in the cmd box, although I get a message in the box,
but everything else is fine. The GUI works correctly.

 D:\Temp\widget-tour-py3>python canvasruler.py
 can't invoke "event" command: application has been destroyed
 while executing
 "event generate $w <>"
 (procedure "ttk::ThemeChanged" line 6)
 invoked from within
 "ttk::ThemeChanged"


I have seen this too.  It is not specific to any python/tkinter code, 
but is specific to exiting with some things destroyed and something not.

This must be coming from tcl or tk, not python or tkinter.


Second, I try to find out where is this message comes from. I insert
a statement "import pdb; pdb.set_trace()" at the source line 33:

 33 import pdb; pdb.set_trace()
 34 #
 35 # Note : to execute the Tk command, I need to create a Toplevel window
 36 # I guess I could use the toplevel create in widget.py, buth then this
 37 # module would not run stand-alone
 38 #
 39 temp = Tk()
 40 STIPPLE_BITMAP = temp.tk.eval(
 41  'image create bitmap @%s' % STIPPLE_BITMAP_FILE )
 42 ## Now I can delete the spurious window
 43 temp.destroy()
 44
 45
 46 TAB_NORMAL_STYLE = {'fill':'black', 'stipple':''}
 47 TAB_INRANGE_STYLE = {'fill':'green', 'stipple':''}
 48 TAB_OUTOFRANGE_STYLE = {'fill':'red', 'stipple':STIPPLE_BITMAP}
 49
 50
 51 class RulerCanvas (Canvas):

and run this script under interpreter, it stops at the point correctly and
I can step through to reach the line 51 without seeing that message appear.

 D:\Temp\widget-tour-py3>python
 Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 
bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import canvasruler
 > d:\work\python\widget-tour-py3\canvasruler.py(39)()
 -> temp = Tk()
 (Pdb) next
 > d:\work\python\widget-tour-py3\canvasruler.py(40)()
 -> STIPPLE_BITMAP = temp.tk.eval(
 (Pdb) until 51
 > d:\work\python\widget-tour-py3\canvasruler.py(51)()
 -> class RulerCanvas (Canvas):
 (Pdb)

Third, I moved the set_trace() to line 50 and run again. This time I saw that 
message appears in the cmd box.

 D:\Temp\widget-tour-py3>python
 Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 
bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import canvasruler
 > d:\work\python\widget-tour-py3\canvasruler.py(51)()
 -> class RulerCanvas (Canvas):
 (Pdb) can't invoke "event" command: application has been destroyed
  while executing
 "event generate $w <>"
 (procedure "ttk::ThemeChanged" line 6)
 invoked from within
 "ttk::ThemeChanged"

 (Pdb) list
  46 TAB_NORMAL_STYLE = {'fill':'black', 'stipple':''}
  47 TAB_INRANGE_STYLE = {'fill':'green', 'stipple':''}
  48 TAB_OUTOFRANGE_STYLE = {'fill':'red', 'stipple':STIPPLE_BITMAP}
  49
  50 import pdb; pdb.set_trace()
  51  -> class RulerCanvas (Canvas):
  52
  53 def __init__(self, master ):
  54 Canvas.__init__(self, master, width=200+RULER_LENGTH, 
height=100 )
  55 self.draw_ruler()
  56 self.draw_well()
 (Pdb)

My problem is why the difference? and how to find out where that message comes 
from?

PS. I also run this test under a Win8.1/64bit machine, get the same result.
PSS. I had scan files in the whole example directory and found there is
  no any file using ttk module.


Right.  I got this with IDLE tests before using ttk.  Good luck tracing 
this to its origin.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: strang thing:

2011-09-12 Thread Steven D'Aprano
(I sent this reply a week ago, but it seems to have disappeared. So trying
again.)


On Tue, 6 Sep 2011 06:18 pm 守株待兔 wrote:

 when i  add    (date,open,high,low,close,vol,adjclose) = (row[0], row[1],
 row[2], row[3],row[4], row[5], row[6]) change the code into

Here you define a new variable called open, which has the value of row[1].
This shadows (hides) the built-in function also called open, so later
when you do this:

     file = open(filename,'r')
 TypeError: 'str' object is not callable

Python uses your string variable open instead of the built-in function.

The best solution is to avoid using the name open, instead call it open_
(underscore at the end is the usual convention to avoid shadowing
built-ins). Or open_value or any other appropriate name.

Another solution is to save a reference to the open built-in first:

my_open = open
open = something
file = my_open(filename, r)




-- 
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strang thing:

2011-09-12 Thread Chris Angelico
On Tue, Sep 13, 2011 at 1:28 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 The best solution is to avoid using the name open, instead call it open_
 (underscore at the end is the usual convention to avoid shadowing
 built-ins). Or open_value or any other appropriate name.


This is why every good programmer keeps a thesaurus handy. I just now
had a concept collision on the word 'cancel', and did a quick search
to come up with 'annul' as an alternative. Although in this case I
didn't search my thesaurus, I actually looked for Magic: The Gathering
cards... yeah, I'm a nerd, aren't you? :)

There's lots of synonyms for open, and it's entirely possible that one
will work. Otherwise, Steven's other solution works just fine too, and
you can go humorous with that too:

sesame = open
open = something
file = sesame(filename, r)

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strang thing:

2011-09-06 Thread Chris Angelico
2011/9/6 守株待兔 1248283...@qq.com:
     file = open(filename,'r')

 when i  add    (date,open,high,low,close,vol,adjclose) = (row[0], row[1],

You're assigning to the name open, which is shadowing the built-in
of the same name. The second time through the loop, you're not calling
the usual open() function, you're trying to call your string. That's
what your error is telling you.

Hope that helps!

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strang thing:

2011-09-06 Thread Wolfgang Rohdewald
On Dienstag 06 September 2011, 守株待兔 wrote:
 (date,open,high,low,close,vol,adjclose) = (row[0],
 row[1], row[2], row[3],row[4], row[5], row[6]) print  
 row[0], row[1], row[2], row[3],row[4], row[5], row[6]
 
 
 the wrong  output is :
 file = open(filename,'r')
 TypeError: 'str' object is not callable

you reassigned open to be a string

-- 
Wolfgang
-- 
http://mail.python.org/mailman/listinfo/python-list