[英]tkinter browser keep showing up when run exe file create by pyinstaller
我在设置使用 Tkinter GUI 和 Pyinstaller 的应用程序时遇到问题。 当我在 Visual Studio Code 上运行它时,我的代码可以正常工作,但是当我使用 Pyinstaller 从我的代码构建 a.exe 时,只要我点击按钮,Tkinter 浏览器就会一直显示。 我已经尝试了一些方法,例如升级 Pyinstaller 或运行不同的命令,例如 Pyinstaller --onefile -w my python 文件或 Pyinstaller -w -F my python 文件......但它仍然不起作用。 下面是我的代码有人给我一些关于这个问题的建议吗? 非常感谢。
import pyscreenshot
from pytesseract import pytesseract
from tkinter import *
from PIL import Image as im
import tkinter as tk
#Creating a main_browser
main_browser = Tk()
# main_browser.title('read_info')
# main_browser.geometry("500x200")
#Giving a Function To The Button
#def on_click():
# label_read["text"] = "Read successfully"
def action():
#capture the screen
# pick the location
tec_info = pyscreenshot.grab(bbox=(50, 100, 450, 380))
bar_code = pyscreenshot.grab(bbox=(80, 360, 450, 450))
usb_info = pyscreenshot.grab(bbox=(30, 480, 450, 580))
wifi_info = pyscreenshot.grab(bbox=(550, 100, 800, 400))
bt_info = pyscreenshot.grab(bbox=(550, 380, 800, 630))
#all_screen = pyscreenshot.grab(bbox=(0, 50, 1060, 690))
# save image
tec_info.save(r"C:\test\Debug\tec_info.png")
bar_code.save(r"C:\test\Debug\barcode.png")
usb_info.save(r"C:\test\Debug\usb_info.png")
wifi_info.save(r"C:\test\Debug\wifi_info.png")
bt_info.save(r"C:\test\Debug\bt_info.png")
#all_screen.save("all_info.png")
# read text info and barcode from image
#1. tec_info
img_tec_info = im.open(r"C:\test\Debug\tec_info.png")
img_tec_info=img_tec_info.resize((800,560))
imgray_tec_info=img_tec_info.convert('L')
imgray_tec_info.save(r"C:\test\Debug\tec_info_bnw.png")
#2. barcode
img_barcode = im.open(r"C:\test\Debug\barcode.png")
img_tec_info=img_tec_info.resize((740,180))
imgray_barcode=img_barcode.convert('L')
imgray_barcode.save(r"C:\test\Debug\barcode_bnw.png")
#3. usb_info
img_usb_info = im.open(r"C:\test\Debug\usb_info.png")
img_tec_info=img_tec_info.resize((840,200))
imgray_usb_info=img_usb_info.convert('L')
imgray_usb_info.save(r"C:\test\Debug\usb_info_bnw.png")
#4. wifi_info
img_wifi_info = im.open(r"C:\test\Debug\wifi_info.png")
img_tec_info=img_tec_info.resize((500,600))
imgray_wifi_info=img_wifi_info.convert('L')
imgray_wifi_info.save(r"C:\test\Debug\wifi_info_bnw.png")
#5. bt_info
img_bt_info = im.open(r"C:\test\Debug\bt_info.png")
img_tec_info=img_tec_info.resize((500,500))
imgray_bt_info=img_bt_info.convert('L')
imgray_bt_info.save(r"C:\test\Debug\bt_info_bnw.png")
path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
#Define path to image
path_to_tec_info_bnw = r"C:\test\Debug\tec_info_bnw.png"
path_to_barcode_bnw = r"C:\test\Debug\barcode_bnw.png"
path_to_usb_info_bnw = r"C:\test\Debug\usb_info_bnw.png"
path_to_wifi_info_bnw = r"C:\test\Debug\wifi_info_bnw.png"
path_to_bt_info_bnw = r"C:\test\Debug\bt_info_bnw.png"
#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract
#Open image with PIL
img_tec_info_bnw = im.open(path_to_tec_info_bnw)
img_barcode_bnw = im.open(path_to_barcode_bnw)
img_usb_info_bnw = im.open(path_to_usb_info_bnw)
img_wifi_info_bnw = im.open(path_to_wifi_info_bnw)
img_bt_info_bnw = im.open(path_to_bt_info_bnw)
#Extract text from image
try:
text_tec_info = pytesseract.image_to_string(img_tec_info_bnw)
text_barcode = pytesseract.image_to_string(img_barcode_bnw)
text_usb_info = pytesseract.image_to_string(img_usb_info_bnw)
text_wifi_info = pytesseract.image_to_string(img_wifi_info_bnw)
text_bt_info = pytesseract.image_to_string(img_bt_info_bnw)
except:
text_tec_info = 'NONE'
text_barcode = 'NONE'
text_usb_info = 'NONE'
text_wifi_info = 'NONE'
text_bt_info = 'NONE'
text_tec_info_split=text_tec_info.split("\n")
text_barcode_split=text_barcode.split("\n")
text_usb_info_split=text_usb_info.split("\n")
text_wifi_info_split=text_wifi_info.split("\n")
text_bt_info_split=text_bt_info.split("\n")
#text_tec_info_split=re.split(': | ', text_tec_info)
text_tec_info_split_filter_strim=[]
text_tec_info_split_filter_strim_upper=[]
text_tec_info_split_filter=[i for i in text_tec_info_split if len(i) >7]
text_barcode_split_filter=[i for i in text_barcode_split if len(i) >7]
text_usb_info_split_filter=[i for i in text_usb_info_split if len(i) >10]
text_wifi_info_split_filter=[i for i in text_wifi_info_split if len(i) >7]
text_bt_info_split_filter=[i for i in text_bt_info_split if len(i) >7]
try:
if '178' in text_barcode_split_filter[0] and len(text_barcode_split_filter[0])>=16 or len(text_barcode_split_filter[0])==14:
text_barcode_split_filter[0].strip()
i=(text_barcode_split_filter[0].find("178"))
if i==1:
text_barcode_split_filter[0]=text_barcode_split_filter[0][1:-1].replace(" ", "")
elif i==1:
text_barcode_split_filter[0]=text_barcode_split_filter[0][2:-1].replace(" ", "")
elif i==0:
text_barcode_split_filter[0]=text_barcode_split_filter[0][-1].replace(" ", "")
else:
text_barcode_split_filter[0]=text_barcode_split_filter[0]
except:
text_barcode_split_filter=[i for i in text_barcode_split if len(i) >7]
try:
for ele in text_tec_info_split_filter:
if ':' in ele:
ele1=ele.replace(":", "")
if '¢'in ele1:
ele1=ele1.replace("¢", "c")
if ',' in ele:
ele1=ele.replace(",", ".")
text_tec_info_split_filter_strim.append(ele1)
for ele in text_tec_info_split_filter_strim:
ele2=ele.upper()
text_tec_info_split_filter_strim_upper.append(ele2)
except:
text_tec_info_split_filter_strim_upper=text_tec_info_split_filter
with open(r"C:\test\Debug\read_all_info.txt", 'w') as f:
f.write("---tec_info---\n")
for ele in text_tec_info_split_filter_strim_upper:
f.write(ele)
f.write("\n")
f.write("---barcode_info---\n")
for ele in text_barcode_split_filter:
f.write(ele)
f.write("\n")
f.write("---usb_info---\n")
for ele in text_usb_info_split_filter:
f.write(ele)
f.write("\n")
f.write("---wifi_info---\n")
for ele in text_wifi_info_split_filter:
f.write(ele)
f.write("\n")
f.write("---bt_info---\n")
for ele in text_bt_info_split_filter:
f.write(ele)
f.write("\n")
#on_click()
# main_browser.destroy()
#Creating The Button
btn = Button(main_browser, text="CLICK ME",
width=20,
height=2, command=action)
#label_read = Label(main_browser, text="Read information",
#font=('Calibri 20 bold'))
#label_read.pack()
#btn.place(x=100, y=20)
#put on screen
btn.pack(side=tk.BOTTOM)
main_browser.mainloop()
#NB:This programme Will Print Something In The Terminal
#Check My Profile To See How We Print On The Screen Or Type In Google "Tkinter label_read"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.