Resources/pointers for writing maintable, testable Python

Terry Reedy tjreedy at udel.edu
Thu May 19 18:45:30 EDT 2016


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






More information about the Python-list mailing list