Tkinter.filedialog does not work with Toastnotifier

Asked

Viewed 67 times

0

I am creating a virtual assistant that communicates with the user through notification messages. I researched a lot on the internet about and found the win10toast.

inserir a descrição da imagem aqui

So far the program is working perfectly, however today, I was implementing a function for the user to define directories for the wizard perform searches of programs and others on the computer.

Code to select a directory:

from tkinter import Tk
from tkinter.filedialog import askdirectory,askopenfilename

def getDirectory():

    Tk().withdraw()
    path = askdirectory()
    return path

The problem is that it does not work. It does not open a window to select a directory. I have tested remove from the code the part that uses the win10toast and worked the function getDirectory().

Code to display wizard message:

from win10toast import ToastNotifier

class MessageBox(object):

    def __init__(self,icon=None):
        self.__icon = icon
        self.__toastNotifier = ToastNotifier()

    def send(self,title,message,duration=5,threaded=True):

        # Se eu retirar este método a função getDirectory funciona.
        self.__toastNotifier.show_toast(
            title,
            message,
            icon_path=self.__icon,
            duration=duration,
            threaded=threaded
        )

Example of execution:

messageBox = MessageBox() #Inicializar um objeto de ToastNotifier não gera problemas para o getDirectory.

# Se eu retirar essa linha, o getDirectory funciona.
messageBox.send("Jarvis","Aperte Windows + S para falar.",threaded=False) #Envia mensagem

command = assistant.listen() #Retorna uma string

if command.lower() == "adicione caminho":
    path = getDirectory()

    if path and not path in paths:
        paths.append(path)

What could be generating this problem and how can I solve it ? I know there are similar problems this and that there is a solution, but in my case this solution does not work.

  • the above codex does not have the aao call "win10toast", and therefore "works perfectly". I don’t see how anyone can help you unless you set a minimum example with code that present the problem (in other words: including initialization and call to win10toast, the way you are doing)

  • Ok I’ll put the question, but I don’t think it’s very necessary because win10toast has only one class with 3 methods being one to appear the notification, another to check if the notification is still active and another to perform something while destroying the notification.

  • like I said - without it, the code works, so there’s nothing to answer

  • Searching, I found similar problems that had solution as for example this: https://github.com/pywinauto/pywinauto/issues/517. However in my case this solution did not work, so I wondered if someone who understands in depth about how the filedialog works can answer me.

1 answer

0


I still can not understand the cause of the problem but doing several tests, I managed to find the solution to my program.

The problem for some reason was caused when I passed the parameter thread the argument False. This argument makes the message, in this case the method show_toast, is executed within a Thread. Passing this parameter the value True the function getDirectory works perfectly.

But now there is a doubt in the air. I know that Tkinter should always run in the main thread, that is, in the main execution. But why was he giving problem if what was running out of the main thread was the method show_toast ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.