Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108847

Re: Resources/pointers for writing maintable, testable Python

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Resources/pointers for writing maintable, testable Python
Date 2016-05-19 18:45 -0400
Message-ID <mailman.44.1463697947.27390.python-list@python.org> (permalink)
References (3 earlier) <ffa86da4-f7f1-4a02-9fba-0bd6ca68e074@googlegroups.com> <nhkpa0$crf$1@ger.gmane.org> <mailman.35.1463675017.27390.python-list@python.org> <909ee5e9-ae16-4ebe-a6c1-c838ff3bb8fd@googlegroups.com> <nhlfmb$n5p$1@ger.gmane.org>

Show all headers | View raw


On 5/19/2016 4:10 PM, Mike Driscoll wrote:
> On Thursday, May 19, 2016 at 11:23:53 AM UTC-5, Terry Reedy wrote:

>> In my case, I learned better how to test IDLE from a user perspective.
>> For tkinter apps, an external program such as Selenium is not needed.
>> Tk/tkinter have the simulated event generation and introspection needed
>> to simulate a user hitting keys, clicking mouse buttons, and reading the
>> screen.

> I am curious. Where is this documented? Are you referring to calling
 > the invoke() method on each widget?

For widget commands, yes.  (And thanks for reminding of the method.)

For events:
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html
(An indispensible tkinter reference) says:
'''
w.event_generate(sequence, **kw)

     This method causes an event to trigger without any external 
stimulus. The handling of the event is the same as if it had been 
triggered by an external stimulus. The sequence argument describes the 
event to be triggered. You can set values for selected fields in the 
Event object by providing keyword=value arguments, where the keyword 
specifies the name of a field in the Event object.

     See Section 54, “Events” for a full discussion of events.
'''
This omits some essentials.  tcl.tk/man/tcl8.6/TkCmd/event.htm
has much more, including the need to put focus on Text and Entry.

 From Stackoverflow, I learned that .update() is needed *before* 
.event_generate.  The following works.

import tkinter as tk
root = tk.Tk()
def prt():
     print('Handler called')
button = tk.Button(root, text='Click', command=prt)
button.place(x=20, y=20)
def ev(e):
     print(e.x, e.y)
button.bind('<ButtonPress-1>', ev)
button.update()
button.event_generate('<ButtonPress-1>', x=0, y=0)
button.event_generate('<ButtonRelease-1>')
button.invoke()
entry=tk.Entry(root)
entry.place(x=20, y=50)
entry.focus_force()
entry.update()
entry.event_generate('<Key-a>')

The event is reported, the handler is called, and 'a' is inserted. 
(Inserting text could be done more easily, but some key events such as 
<Key-End> do have text equivalents.)

-- 
Terry Jan Reedy


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Resources/pointers for writing maintable, testable Python Andrew Farrell <armorsmith42@gmail.com> - 2016-05-18 17:23 -0400
  Re: Resources/pointers for writing maintable, testable Python Mike Driscoll <kyosohma@gmail.com> - 2016-05-19 08:33 -0700
    Re: Resources/pointers for writing maintable, testable Python Jacob Scott <jacob.scott@gmail.com> - 2016-05-19 09:10 -0700
    Re: Resources/pointers for writing maintable, testable Python Terry Reedy <tjreedy@udel.edu> - 2016-05-19 12:23 -0400
      Re: Resources/pointers for writing maintable, testable Python Mike Driscoll <kyosohma@gmail.com> - 2016-05-19 13:10 -0700
        Re: Resources/pointers for writing maintable, testable Python Terry Reedy <tjreedy@udel.edu> - 2016-05-19 18:45 -0400

csiph-web