Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good example of functional testing a Tkinter application?

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?

like image 949
Wes Grant Avatar asked Dec 01 '25 02:12

Wes Grant


1 Answers

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()
like image 102
Dmitrii Malygin Avatar answered Dec 02 '25 16:12

Dmitrii Malygin