弹出窗口打开时Python脚本停止

时间:2019-07-30 01:15:25

标签: python-3.x popup exit

我每10秒钟运行一次python函数,该函数将信息写入json文件,如果发生某个事件,我选择显示弹出消息。 问题在于该功能在弹出消息显示的时间内停止了应做的事情,这就是问题所在。 因为它将漂移引入了我的功能。

在选定的时间后,我设法关闭了弹出消息,即使缩短了功能停止的时间,它仍然不是理想的结果。


def popupmsg(title, msg):
    popup = tk.Tk()
    popup.wm_title(title)

    tk.Label(popup, text=msg).pack(side="top", fill="x", pady=10)
    tk.Button(popup, text="Okay", command = popup.destroy).pack()

    # I managed to close the pop up here
    popup.after(5000, lambda: popup.destroy())     # time in ms
    popup.mainloop()


def TimedFx(threads, intervalo_pings, file_in, file_out):

    # Logeamos tiempo
    starttime=time.time()

    # Archivo de in & out
    old_file = "{}.json".format(file_out)


    while True:
        # print("tick")
        print(writeCo_Desco('./', threads, file_in, file_out))
        with open(old_file, 'r') as old:
            data = json.load(old)
            if (len(data[-1:][0])) > 4:

                # Lista de conectados y desconectados nuevos
                nuevos_co = []
                nuevos_desco = []


                if 'nuevos_conectados' in (data[-1:][0]):
                    nuevos_co.append(data[-1:][0]['nuevos_conectados'])
                    for i in nuevos_co:
                        if i not in data[-5:][0]['conectados']:
                            print("nuevos conectados {}".format(i))
                            popupmsg("nuevo conectado", '{}'.format(i))


                elif 'nuevos_desconectados' in (data[-1:][0]):
                    nuevos_desco.append(data[-1:][0]['nuevos_desconectados'])
                    for i in nuevos_desco:
                        if i not in data[-5:][0]['desconectados']:
                            print("nuevo desconectado {}".format(i))
                            popupmsg("nuevo desconectado", '{}'.format(i))


        # Avoid drifting

        time.sleep(intervalo_pings - ((time.time() - starttime) % intervalo_pings))

我想避免在显示味精弹出窗口时函数停止运行。

1 个答案:

答案 0 :(得分:0)

让弹出窗口由单独的线程处理。

这意味着:

在弹出功能开始时,派生一个线程,并让该线程处理弹出窗口。主线程立即返回。

相关问题