hashy
hashy

Reputation: 305

console application main thread is not in main loop using pysimplegui

For few days now, I have been researching on how to fix this issue. Basically I have a console application where in certain stages, I call pysimplegui to create a notification window or:

  1. main console program that needs to always work on the background
  2. if capture I.e a keystroke, create an alert window. On this stage, I need the main console program to still be capturing keystroke while a pysimplegui window is created. Hence why I am using a thread to open the pysimplegui window in a new thread.

How I have developed my program.

if (threading.active_count() < 2):
    wt = threading.Thread(target=createwindow, name="noty", args=(argumnets,),
                                          daemon=True)
    wt.setDaemon(True) # just to be safe
    wt.start()
    wt.join()

create window:

def createalertwindow(Attack):
    # I have removed this part of the code where I design the gui window just to make easy to understand
    e, v = win.read(timeout=5000)
    if (e == "e"):
        print("e button clicked")
    elif (e == "Ok"):
        win.close()
    # close first window
    win.close()

Now Every time I run above code I get below exception errors:

Exception ignored in: <function Variable.del at 0x000001EACB37CCA0> Traceback (most recent call last): File "C:\Users\Abdul\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 350, in del if self._tk.getboolean(self._tk.call("info", "exists", self.name)): RuntimeError: main thread is not in main loop Exception ignored in: <function Variable.del at 0x000001EACB37CCA0> Traceback (most recent call last): File "C:\Users\Abdul\AppData\Local\Programs\Python\Python39\lib\tkinter_init.py", line 350, in del if self._tk.getboolean(self._tk.call("info", "exists", self._name)): RuntimeError: main thread is not in main loop

I read many question already out there, but found none that could fix the issue for me I.e I tried using:

plt.switch_backend('agg')

wt = threading.Thread(target=createwindow, name="noty", args=(argumnets,),
                                          daemon=True)
wt.setDaemon(True)
...

I may be note worthy to mention again that my main program is a console application not a gui application.

I am using threading library for threading but am okay to switch if I have better options and am using pysimplegui for create gui windows.

Upvotes: 0

Views: 1619

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

It looks like you cannot call PySimpleGUI/tkinter in another thread. Here, try to set main program as one thread and called in PySimpleGUI/tkinter. The same, remember that don't call PySimpleGUI directly in your main_program and use method window.write_event_value to generate an event, then do it in your event loop.

Example code,

from time import sleep
import threading
import PySimpleGUI as sg

def hello():
    layout = [[sg.Text("Hello, my friend !")]]
    window = sg.Window("Hello", layout, keep_on_top=True, modal=True)
    window.read(timeout=1000, close=True)

def main_program():
    count = 5
    while count > 0:
        window.write_event_value("Hello", None)
        sleep(3)
        count -=1
    window.write_event_value(sg.WINDOW_CLOSED, None)

layout = [[]]

window = sg.Window("Title", layout, alpha_channel=0, finalize=True)
threading.Thread(target=main_program, daemon=True).start()
while True:

    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == "Hello":
        hello()

window.close()

Upvotes: 0

Related Questions