20

I found a great website that discusses functional testing of a Python GUI application using IronPython, but I want to use Tkinter and am having a hard time making the transition between the libraries.

Michael shows this example for IronPython:

class FunctionalTest(TestCase):

    def setUp(self):
        self.mainForm = None
        self._thread = Thread(ThreadStart(self.startMultiDoc))
        self._thread.SetApartmentState(ApartmentState.STA)
        self._thread.Start()
        while self.mainForm is None:
            Thread.CurrentThread.Join(100)

        def invokeOnGUIThread(self, function):
            return self.mainForm.Invoke(CallTarget0(function))

I'm having a hard time translating that into how I would hook into a Tkinter based application that would have the base setup:

from tkinter import *
from tkinter import ttk

root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()

I'm thinking you would want to run a method on the main root object in the second thread as well but I'm not seeing an equivalent method to the mainForm.Invoke(). Maybe I'm thinking about it wrong. Maybe functional testing GUI applications in this manner is not so common?

Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
Wes Grant
  • 829
  • 7
  • 13

1 Answers1

1

To a Tkinter-based application, the approach would be different. In a Tkinter-based application you can to use the trace method of a StringVar or IntVar. Like this:

from tkinter import * from tkinter import ttk

def on_value_change(*args):
    print("Value changed to:", var.get())

root = Tk()
var = StringVar()
var.trace("w", on_value_change) # detect changes in the variable
ttk.Entry(root, textvariable=var).grid()
root.mainloop()
Dmitrii Malygin
  • 144
  • 2
  • 2
  • 12