◐ Shell
clean mode source ↗

Message 298017 - Python tracker

We already know that setting StringVar font_name triggers a change to changes.  We also know that manually clicking or Up or Down triggers <<ListBoxSelect>>, which sets font_name and redraws the box.  The challenge is to do something that will trigger the virtual event.  Then we can write a test methods something like

    def test_sample(self):
        fontlist = configure.fontlist
        if fontlist.size():
            name0 = fontlist.get(0).lower()
            # fontlist.selection_set(0)  # or something
            print('\n', changes)  # temp to testing that changes changes
            self.assertEqual(changes['main']['EditorWindow']['font'], name0)
But selection_set does not trigger the event.  After fiddling around with various bindings event_generate()s and update()s and update_idletasks(), I concluded, again, that either event_generate or its documentation is badly broken.  The only thing I got to work in hours of experiments is this:

import tkinter as tk
root = tk.Tk()
seq = '<ButtonRelease-1>'
root.bind(seq, lambda event: print('generated', event))
root.update_idletasks()  # or update()
root.event_generate(seq)
# update here fails

Adding a widget and binding to the widget always failed.

Here is my attempt using Serhiy's simulate_mouse_click.  (This goes in test_configdialog.FontTabTest.

    def test_sample(self):
        fontlist = configure.fontlist
        if fontlist.size():
            name0 = fontlist.get(0).lower()
            fontlist.see(0)
            x, y, dx, dy = fontlist.bbox(0)
            fontlist.bind('<ButtonRelease-1>', lambda event: print(event))
            mouse_click(fontlist, x + dx//2, y + dy//2)
            fontlist.update()
            print('\n', changes)  # temporary, see if changes has anything
            self.assertEqual(changes['main']['EditorWindow']['font'], name0)

Serhiy, do you have any idea why I cannot get event_generate to work for a listbox, even with your function? I have tried somewhere close to 20 variations.