rebar
rebar

Reputation: 90

Trying to launch single threaded window, but two are launching

I'm trying to launch a threaded window because I have background processes that are running, but two identical windows are launching under the same thread and I'm not sure why. I'm guessing this code can be optimized! thx for any suggestions.

6/1: I made the modification as suggested to not run two windows in the main loop, and that works. The 2nd piece, is that once the button is clicked, the window is destroyed "self.root.destroy()", but if I try to open another window it will not open. I do a check before trying to launch the new window and there is only the main thread running, so the first thread is gone. Not sure what's going on and I'm not able to debug with threads running.

from Tkinter import *
import ttk
import threading
import thread
from pdctest.test_lib.hid_util import *

class GUI():
    def __init__(self, root):
        self.root = root # root is a passed Tk object
        self.root.title("GUI--->user instruction window")
        self.frame = Frame(self.root)
        self.frame.pack()
        self.label0 = Label(self.frame, text=" ")
        self.label0.configure(width=50, height=1)
        self.label0.pack()

        self.label = Label(self.frame, text="GUI--->execute a SKP on HS within 3s...")
        self.label.configure(width=50, height=1)
        self.label.pack()

        self.button = Button(self.root, text=" GUI--->then click this button ", command=self.buttonWasPressed, fg = 'black', bg = 'dark orange')
        self.button.configure(width=40, height=5)
        self.button.pack(pady=25)

    def removethis(self):
        print("GUI--->in removethis")
        self.root.destroy()

    def buttonWasPressed(self):
        print( "GUI--->inside buttonWasPressed: the button was pressed! " + '\r')
        self.removethis()
        print("GUI--->leaving buttonWasPressed")

    def guiSKP(self):
        #root = Tk()
        #window = GUI(root)
        #root.mainloop()
        # modified 6/1, fixes multi-window issue
        self.root.mainloop()

class GUI_SKP_Thread(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
    def run(self):
        root = Tk()
        window = GUI(root)
        window.guiSKP()

def launch_SKP_thread():
    print("beginning launch_SKP_thread" + '\r')
    thread_SKP = GUI_SKP_Thread("GUI_SKP_Thread")
    thread_SKP.start()
    print("exiting launch_SKP_thread" + '\r')

def whatsRunning():
    t = threading.enumerate()
   print("--------------->whatsRunning")
   print t

if __name__ == '__main__':
    launch_SKP_thread()
    # trying to launch 2nd window
    whatsRunning()
    time.sleep(4)
    whatsRunning()
    launch_SKP_thread()

Upvotes: 0

Views: 59

Answers (1)

ErrorAtLine0
ErrorAtLine0

Reputation: 189

I't not really sure what you're trying to do with the program. But I'm sure about one thing, that is, you are trying to run two frames in the same mainloop. You defined window twice in different locations, meaning that there are two frames/windows. Once in guiSKP in GUI, and once in run in GUI_SKP_Thread.

root = Tk()
window = GUI(root)

So, you should change the text in guiSKP from:

root = Tk()
window = GUI(root)
root.mainloop()

To simply:

self.root.mainloop()

I added the self to make sure it runs its own root.mainloop()

Upvotes: 2

Related Questions