Is there a good example of functional testing a Tkinter application?
Asked Answered
B

1

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?

Bucksaw answered 26/11, 2013 at 21:21 Comment(1)
Related: How do I run unittest on a Tkinter app?Grig
C
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()
Creative answered 23/3, 2023 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.