1

I got this error in only one type OS/machine but in other it's ok. Do you have any idea how I can fix it ? In all machines , I run with python 3.7.4. The problem that there is a widget in loop with thread. If I remove the thread or loop then it's ok.

But in problematic machine , the default python is 2.7 but I run with 3.7.4

Exception in thread Thread-1: Traceback (most recent call last):   File "/usr/pkgs/python3/3.7.4/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()   File "/usr/pkgs/python3/3.7.4/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)   File "example_gui_2.py", line 45, in test
    textbox.config(state=NORMAL)   File "/usr/pkgs/python3/3.7.4/lib/python3.7/tkinter/__init__.py", line 1485, in configure
    return self._configure('configure', cnf, kw)   File "/usr/pkgs/python3/3.7.4/lib/python3.7/tkinter/__init__.py", line 1476, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: out of stack space (infinite loop?)

The code example_gui_2.py

import tkinter as tk
import subprocess
import threading
import sys
from functools import partial


def run(textbox=None):
    threading.Thread(target=test, args=[textbox]).start()


def test(textbox=None):
    # using the Python executable to run demo_print.py
    p = subprocess.Popen([sys.executable, "-u", "demo_print.py"], stdout=subprocess.PIPE, bufsize=1, text=True)
    while p.poll() is None:
        msg = p.stdout.readline().strip()  # read a line from the process output
        if msg:
            textbox.config(state=NORMAL)
            textbox.insert(tk.END, msg + "\n")
            textbox.see(tk.END)
            textbox.update_idletasks()
            textbox.config(state=DISABLED)


if __name__ == "__main__":
    fenster = tk.Tk()
    fenster.title("My Program")
    textbox = tk.Text(fenster)
    textbox.grid(row=0, column=0)
    scrollbar = tk.Scrollbar(fenster, orient=tk.VERTICAL)
    scrollbar.grid(row=0, column=1, sticky="ns")

    textbox.config(yscrollcommand=scrollbar.set)
    scrollbar.config(command=textbox.yview)

    start_button = tk.Button(fenster, text="Start", command=partial(run, textbox))
    start_button.grid()

    fenster.mainloop()
Alex
  • 29
  • 5
  • You cannot do UI stuff in a secondary thread. That's simply the primary rule of every GUI framework on every platform. The problem is, your operations are sending messages to the current thread, but the main loop is not listening there. You can run your subprocess in a thread, but you must use something like `textbox.after` to send the results to a function in the main thread. – Tim Roberts Nov 22 '22 at 18:46
  • Can you show me how it should be please ? @TimRoberts – Alex Nov 22 '22 at 18:49

0 Answers0