ceccopierangiolieugenio / pyTermTk

Python Terminal Toolkit - a Spiced Up TUI Library 🌶️
https://ceccopierangiolieugenio.github.io/pyTermTk/
MIT License
587 stars 22 forks source link

How to set custom event for close icon on window #222

Open smartkid2024 opened 7 months ago

smartkid2024 commented 7 months ago

How can I write a custom method for the close icon for example if I press it and then exit from TermTK self._stop_progress_bar_thread = True TTk().quit()

image

ceccopierangiolieugenio commented 7 months ago

you can just connect the closed signal to the quit slot

from TermTk import TTk, TTkWindow

root = TTk()

win = TTkWindow(parent=root, pos=(0,0), size=(40,20))
win.closed.connect(root.quit)

root.mainloop()
smartkid2024 commented 7 months ago

I also need to stop the thread i mean self._stop_progress_bar_thread = True does it work with code? and it's my code structure. it works if not thread start so I need to stop thread first

class MyCheckLicense(TTkWindow):

    def __init__(self, root):
        self.root = root

 ## Event Methods ##
    pyTTkSlot(str)
    def onCheckBtnClick(self):
        ...
    def onCheckInfoBtnClick(self):
        ...

root = TTk(title="Check License")
win = MyCheckLicense(root)
root.layout().addWidget(win)
win.closed.connect(root.quit)

root.mainloop()

Also I tried this code but not worked:

class MyCheckLicense(TTkWindow):

    def __init__(self, root):
        self.root = root
        ...
        self.closed.connect(self.onQuit())

    ## Event Methods ##
    pyTTkSlot(str)
    def onQuit(self):
        self._stop_progress_bar_thread = True
        TTk().quit()
ceccopierangiolieugenio commented 7 months ago

About:

       TTk().quit()

You are creating new root object so you are not quitting the one active. Use this instead:

        TTkHelper.quit()
class MyCheckLicense(TTkWindow):

    def __init__(self, root):
        # This is not required, 
        # Is it there any reason you are saving it here
        # instead of using the standard initialisation? 
        self.root = root
        # the only reason I can think of is to use it to quit
        # instead of 
        #     TTk().quit()
        # you can call
        #     self.root.quit()
        # but this is more consistent
        #     TTkHelper.quit()
        ...
        # when you connect a slot you have to reference the method
        # not calling it like:
        #        self.closed.connect(self.onQuit())
        self.closed.connect(self.onQuit)

    ## Event Methods ##
    pyTTkSlot(str)
    def onQuit(self):
        self._stop_progress_bar_thread = True
        # Changed based on my previous comment
        TTkHelper.quit()
ceccopierangiolieugenio commented 7 months ago

Did you had a look at my example here? https://github.com/ceccopierangiolieugenio/pyTermTk/issues/213#issuecomment-1887567958 If you use the TTkTimer you should not worry about cleaning the thread

smartkid2024 commented 7 months ago

Yes, I saw but the subject is not only run a timer. It is connected to a server and gets information in the background and it may take time (so the timer is only a simulation)