0

I am trying to start a function with a new thread and everytime i start the function, the tkinter application opens up again. For me to see the function running i need to quit the new application that opened and then it starts. Is there something in my code that is calling the application again and not allowing my startReceive function to run instantly when i start the thread?

import tkinter as tk
from udpTransfer1 import UdpClass
from threading import Thread
from tkinter import messagebox
import multiprocessing, time, signal

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.alert = ''
        self.pack()
        self.alive = False
        self.create_widgets()
        self.c = UdpClass()
        self.threadExists= False
        self.st = None

    def create_widgets(self):
        # 1 button
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Save UDP"
        self.hi_there["command"] = self.run
        self.hi_there.pack(side="top")

        # 2 button
        self.b2 = tk.Button(self)
        self.b2["text"] = "Stop UDP"
        self.b2["command"] = self.b2Action
        self.b2.pack(side="top")

        # 3 button
        self.quit = tk.Button(self, text="QUIT", fg="red")
        self.quit["command"] = self.kill
        self.quit.pack(side="bottom")

    def kill(self):
        if self.threadExists == True:
            # Alert Box
            # self.alert = messagebox.showinfo("ALERT").capitalize()
            self.alert = messagebox._show(None,'UDP has to be stopped first!')
        else:
            root.destroy()

    def run(self):
        # self.st = Thread(target=self.c.startReceive)
        # self.st = multiprocessing.Process(target=self.c.startReceive)
        self.st = multiprocessing.Process(target=startReceive)
        self.st.start()
        if (self.st.is_alive()):
            print("Thread open")
        self.threadExists = True

    def b2Action(self):
        if self.threadExists:
            # self.c.terminate()
            terminate()
            self.st.terminate()
            time.sleep(.1)
            if not(self.st.is_alive()):
                print("Thread Killed")
            self.threadExists = False
        else :
            self.alert = messagebox._show(None, 'No Detection of UDP Running!')

def startReceive():
    print('started')
    try:
        isRunning = True
        # csvfile = open(self.filename, 'w')
        # spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        # start_time = time.time()
        while isRunning:
            print('done')
            time.sleep(1)
    except:
        print('out')


def terminate():
    # self.socket.close()
    # isRunning = False
    print('stopped')

root = tk.Tk()
app = Application(master=root)
app.master.title("UDP Saving")
app.master.geometry('200x200')
app.master.maxsize(200, 500)
app.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Aboogie
  • 450
  • 2
  • 9
  • 27
  • 1
    I notice you don't use `if __name__ == “__main__”:` Explained [here](http://stackoverflow.com/a/419185/2800918) – CAB Oct 24 '16 at 14:49
  • You're not multithreading, you're multiprocessing — which is why you need the `if __name__ == "__main__":` to prevent code that should only rung in the main thread from running everytime a new subprocess is started. – martineau Oct 24 '16 at 15:04
  • If i were to start a new thread instead of a process, would this be better? – Aboogie Oct 25 '16 at 14:03

1 Answers1

0

Not 100% sure, but this might be the issue:

def run(self):

    # self.st = Thread(target=self.c.startReceive)
    # self.st = multiprocessing.Process(target=self.c.startReceive)
    self.st = multiprocessing.Process(target=startReceive)
    self.st.start()
    if (self.st.is_alive()):
        print("Thread open")

    self.threadExists = True

I think You should put self.threadExists = True inside previous if.

def run(self):

    # self.st = Thread(target=self.c.startReceive)
    # self.st = multiprocessing.Process(target=self.c.startReceive)
    self.st = multiprocessing.Process(target=startReceive)
    self.st.start()
    if (self.st.is_alive()):
        print("Thread open")

        self.threadExists = True
Fejs
  • 2,734
  • 3
  • 21
  • 40