0

I created a gui program using python and tkinter. The first time I run this program, self.start_thread() is running well but after I open another frame and back again to first frame, self.start_thread() can't run again. How do I solve this problem?

class HomePage():

    def __init__(self, master):
        self.master = master
        self.master.geometry('800x480')
        self.master.title("welcome")
        self.listbox = Listbox(self.master)
        #listbox.insert(1, self._var)
        self.listbox.pack()

        label = Label(self.master, text="HomePage")
        label.pack()
        button3 = Button(self.master, text="Page One",
                            command=self.pindah_ke_halaman_lain)
        button3.pack()






        self.start_thread()



    def start_thread(self):
        t=threading.Thread(target=self.hitung)
        t.start()

    def pindah_ke_halaman_lain(self):
        global stop_threads_1
        stop_threads_1 = True
        root = Toplevel(self.master)
        PageOne(root)

    def hitung(self):
        global stop_threads_1
        i=0
        while(True):
            print("saya "+ str(i))
            i+=1
            if(stop_threads_1== True):
                break



class PageOne():

    def __init__(self, master):
        self.master = master
        self.master.geometry('800x480')
        self.master.title("enroll")
        btn_back = Button(self.master, text="HomePage",
                            command=lambda:self.pindah_to_Homepage()).grid(row=33,column=1)



    def pindah_to_Homepage(self):
        self.master.destroy()
        #root2=Toplevel(self.master)
James Coyle
  • 9,922
  • 1
  • 40
  • 48
  • 1
    If you want to switch between frames, check this [answer](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter). You are using `self.master.destroy()` in **PageOne**, which destroys the main thread. Rather than doing this, destroy the **Toplevel widget - PageOne** to revert back to your main window (hide the main window when you switch to PageOne) – FrainBr33z3 Apr 23 '19 at 09:03
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Apr 23 '19 at 17:04
  • @FrainBr33z3 thanks for response, i was trying link which you share. but i still couldn't solve my problem about how to run again thread in homePage after i open and close PageOne. i use self.master.destroy() to close PageOne and HomePage be the only one frame/window was showing up. i am new in this field. – Yogi Januardi Apr 24 '19 at 02:15
  • @stovfl thanks for suggestion. but i still have no idea how to solve my code. i need this self.start_thread() will run again, after i close Pageone. – Yogi Januardi Apr 24 '19 at 02:31
  • @YogiJanuardi *"i use self.master.destroy() to close PageOne"*: So stop the `Thread` also. If the `Thread` has only to run as long `PageOne` is visible, move the `Thread` to `class PageOne():` – stovfl Apr 24 '19 at 07:20
  • @stovfl thank you so much. you save my time. thank you. it's solved. i use wait_window to ask if the PageOne has been closed yet? – Yogi Januardi Apr 24 '19 at 07:59

0 Answers0