Python Pyinstaller exe 打开多个 tkinter 实例 window、Python Firebase [视频]

Python Pyinstaller exe opens multiple instance of tkinter window, Python Firebase [Video]

我有一个抓取脚本,其中我在 ui 中使用了 tkinter。当我 build exe(使用 pyinstaller)并打开它时它运行良好,但是当我关闭它时,它会打开 tkinter Window 的多个实例。我无法粘贴完整的代码。所以我粘贴了我正在使用的所有 tkinter 代码。

这是完整代码Github Gist here

import requests
from lxml import html
from tkinter import *
import tkinter as ttk
import re
import datetime
import os
from firebase import firebase
import hashlib
#import App as App
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


#Region Tk
root = Tk()
root['height'] = 400
root['width'] = 600
global firebase
firebase = firebase.FirebaseApplication('#######URL####',None)
f1 = Frame(root)
f1["height"] = root["height"]
f1["width"] = root["width"]
root.title("JD Scraper - Gear Up Studio ")
Label(f1,text = "Input Url : Example : https://www.justdial.com/Ahmedabad/Gyms ").grid(row=0,column = 0,)

def getBool(event):
    print(boolvar.get())
#Check Button
global boolvar
boolvar = BooleanVar()
boolvar.set(False)
boolvar.trace('w', lambda *_: print("The value was changed"))
cb = Checkbutton(f1, text = "Tele Phone number", variable = boolvar)
cb.bind("<Button-1>", getBool)
cb.grid(row=1, column=1)

global key_filled
key_filled = Entry(f1,width=50)
key_filled.grid(row=2,column=0)
key_filled.focus_set()
global activate_button
activate_button = Button(f1 , text="Active Now")
activate_button.bind("<Button-1>",activate_key)
activate_button.grid(row=2, column=1)


result = Label(f1, width=50)
result.grid(row=1,column=2)
global submit_button
submit_button = Button(f1 , text="Scrape Now")
submit_button.bind("<Button-1>",button_clicked)
submit_button.grid(row=1, column=0)
submit_button.config(state=NORMAL)
key_validation()
f1.pack()
root.mainloop()

演示 Video here

我可以看到 cmd window 弹出 运行 你的 exe 使用这个命令 -w 删除它并编译为 onefile 到 dist文件夹使用 -F 将其编译为单个文件,执行此操作以解决问题

pyinstaller -w -F replace this with your script name.py

它会把文件编译成一个文件给你,使用就是res

我在使用 firebase 和 pyqt5 时遇到了完全相同的问题。经过多次尝试,我检查了 firebase 库。在 init 中有 close_process_pool() 函数,当程序退出并关闭所有多处理池时调用该函数。在 tkinter 和 pyqt 的情况下,我们不需要这个函数,因为所有进程都是主 GUI 线程的子进程,所以简单地删除该函数就可以解决问题。 将初始化文件更改为此将解决问题。

import atexit
#from .async import process_pool
from firebase import *


'''
@atexit.register
def close_process_pool():
    """
    Clean up function that closes and terminates the process pool
    defined in the ``async`` file.
    """
    process_pool.close()
    process_pool.join()
    process_pool.terminate()
    '''