Asad Ur Rehman
Asad Ur Rehman

Reputation: 21

Tkinter + MultiProcessing: Tkinter Keeps Opening Windows

I have written this code to basically opens a GUI made in Tkinter and also runs the scheduler both together every thing is working fine as long as i am running this code on PyCharm...

Now if I convert the code to exe using "PyInstaller", After i run the code the GUI keeps opening again and again until I terminate the task..

from multiprocessing import Process
import tkinter as tk

# IMPORTING SCHEDULER AND ADDREDDITACCOUNT GUI

from app.scheduler import schedule_upload
from app.addRedditAccount import AddRedditAccount


if __name__ == "__main__":

    window = tk.Tk()
    AddRedditAccount(window)

    schedule = Process(name='schedule', target=schedule_upload)
    schedule.start()

    window.mainloop()

Upvotes: 2

Views: 928

Answers (1)

Alexander
Alexander

Reputation: 17355

This is happening because you are trying to use multiprocessing in an executable. I am assuming you are using a Windows OS as well. All you need to do is add a call to freeze_support from the multiprocessing module.

For example:

from multiprocessing import Process, freeze_support  # <-- added this
import tkinter as tk

# IMPORTING SCHEDULER AND ADDREDDITACCOUNT GUI

from app.scheduler import schedule_upload
from app.addRedditAccount import AddRedditAccount
freeze_support()   # <--- added this


if __name__ == "__main__":

    window = tk.Tk()
    AddRedditAccount(window)

    schedule = Process(name='schedule', target=schedule_upload)
    schedule.start()

    window.mainloop()

Recompile and your executable should work fine.

You can learn more about freeze_support in the python docs here

Upvotes: 4

Related Questions