1

I'm currently in the process of developing a modular neural network program that has a Tkinter UI module, a neural network module and a "main" module that allows for the two to communicate.

The issue is that I need to retrieve a list of network parameters (so as to instantiate the neural network class) from a UI class attribute but only once it has been updated (via user interaction with widgets).

A while loop that runs based on whether parameters = [] is not feasible as there is no way, that I'm aware of, to update the window at the same time without multithreading but Tkinter isn't thread-safe.

Apologies for any technical inaccuracies, I am relatively new to programming and its jargon.

UI.py

train = Button(self.parameterframe, text="Train", command=lambda: self.train())
        train.place(x=257, y=150)

    def train(self):

        parameters = [self.epochs.get(), self.layer1neurons.get(),
                      self.layer2neurons.get(), self.batchsize.get(),
                      self.learningrate.get()]

        for p in parameters:

            try:

                if p == self.learningrate.get():
                    float(p)
                else:
                    int(p)

            except ValueError:

                messagebox.showerror(title="Error", message="Please ensure that all parameters are of the correct type.")
                return

        self.parameters = parameters

Main.py

def refresh():
    root = Tk()
    interface = UI.create_ui(root)
    while True:
        root.update()
        time.sleep(0.1)

thread = thread.Thread(target=refresh())
thread.start()

#Then a while  loop for retrieving parameters followed by a .join() statement.

Please be aware that the above code is purely experimental and is just to give you an idea of what I'm trying to achieve.

  • Can you please provide a minimal example of what you're trying to do? It's not clear what you mean by "UI class attribute has been set" - are you asking about something changing a font or a color? Or, are you asking about when they change values in an entry widget or an object on a canvas, etc.? – Bryan Oakley Dec 09 '19 at 18:27
  • 1
    Tkinter not being thread-safe doesn't mean you can't use threads — it just means that all usage and interaction with it must take place within only one of them. This is often the main thread, but it doesn't have to be. The threads can communicate with each other through a `Queue` or by shared global resouces (if steps are taken to ensure concurrent access to them can't occur). – martineau Dec 09 '19 at 18:47
  • @BryanOakley To clarify, I meant when `self.parameters` changes value from an empty list to a list of valid parameters via inputs to entry widgets. – Ethan Lamming Dec 09 '19 at 19:24
  • So, not a widget attribute but a custom attribute of a custom class? – Bryan Oakley Dec 09 '19 at 19:31
  • @martineau I'm aware of this, however, I'm struggling to find a way to use them for my desired purpose. Without updating the window via `root.update()`, it's impossible to enter the parameters into the entry widgets but all Tkinter logic seemingly needs to be constrained to the main thread so it doesn't seem to be the case that I can retrieve the parameters in a different thread. Surely a minimum of two threads are needed for this and both rely on Tkinter logic of some form? – Ethan Lamming Dec 09 '19 at 19:33
  • @BryanOakley That's correct, yes. I'll edit the details above to provide an example of how I've tried to approach this. – Ethan Lamming Dec 09 '19 at 19:34
  • Ethan: You can change/update options of an existing widget using `widget.config(option=value, ...)`. It's also usually not necessary to call `root.update()`. – martineau Dec 09 '19 at 19:39
  • Ethan: I think my answer to the question [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete) might be helpful. – martineau Dec 09 '19 at 19:45
  • @martineau I actually found your response to [How do you run your own code alongside Tkinter's event loop?](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) to have fixed the issue. Thank you! – Ethan Lamming Dec 12 '19 at 15:29
  • Ethan: That wasn't my response — I just made some minor edits to user @Kevin's answer. Regardless, glad to hear the problem is resolved. – martineau Dec 12 '19 at 15:38

0 Answers0