生成的可执行文件打开报错如下
网上大部分错误关于font设置,这里我设置过多次都没能成功解决问题。
Traceback (most recent call last):
File "Windos.py", line 87, in <module>
File "Windos.py", line 24, in __init__
File "Windos.py", line 40, in random_pic
File "captcha\image.py", line 58, in write
File "captcha\image.py", line 228, in generate_image
File "captcha\image.py", line 199, in create_captcha_image
File "captcha\image.py", line 164, in _draw_character
File "captcha\image.py", line 120, in truefonts
File "captcha\image.py", line 121, in <listcomp>
File "PIL\ImageFont.py", line 855, in truetype
File "PIL\ImageFont.py", line 852, in freetype
File "PIL\ImageFont.py", line 211, in __init__
OSError: cannot open resource
源代码:
import tkinter
import tkinter.font
from tkinter import Label
from PIL import Image, ImageTk
from captcha.image import ImageCaptcha
import random
# todo 窗口实例化对象
img_png = None
# todo 因为你在开发的时候上面的一段话会写在一个函数里面封装起来,
# 这个时候导致img_open是局部的,所以函数运行结束就被回收了,
# 所以显示的是空白,解决方案也很简单,将这个变量声明为全局即可。
class Windowing:
panel: Label
def __init__(self):
self.str = ""
self.randomStr = ""
self.my_window = self.build_window()
self.random_pic()
self.add_panel()
self.text = self.bulid_text()
self.add_button()
def random_pic(self):
code_list = []
for i in range(10):
code_list.append(str(i))
for i in range(65, 91):
code_list.append(chr(i))
for i in range(97, 123):
code_list.append(chr(i))
myslice = random.sample(code_list, 6)
verification_code = ''.join(myslice)
image = ImageCaptcha()
image.write(verification_code, 'out.png')
self.randomStr = verification_code
@staticmethod
def build_window():
it_window = tkinter.Tk() # 创建窗口实例
it_window.title("user interface") # 设置窗口名称
it_window.geometry("400x300") # 设置窗口大小
it_window.config(bg='#66CCFF') # 谁在窗口背景颜色
return it_window
def add_panel(self):
self.panel = tkinter.Label(self.my_window, text="请输入验证码", bg="#FFFFFF", font=("Arial", 16), width=20,
height=4) # 创建显示面板
self.panel.pack() # 将显示面板放置到窗口当中
img_open = Image.open('D:\\pythonProject\\KeShiHua\\out.png')
global img_png
img_png = ImageTk.PhotoImage(img_open)
label_img = tkinter.Label(self.my_window, image=img_png)
label_img.pack() # 将图片放在窗口中
def bulid_text(self):
ffont = tkinter.font.Font(size=25)
text = tkinter.Text(self.my_window, height=1, width=10, font=ffont)
text.pack()
return text
def get_text(self):
res = self.text.get("1.0", "end")[:-1]
self.str = res
self.judge_end()
def add_button(self):
ffont = tkinter.font.Font(size=25)
button = tkinter.Button(self.my_window, height=1, width=10, text='读取输入内容', font=ffont, command=self.get_text)
button.pack()
def judge_end(self):
if self.str.lower() == self.randomStr.lower():
self.my_window.destroy()
def run(self):
self.my_window.mainloop()
return
if __name__ == "__main__":
window = Windowing()
window.run()
你不设置字体试试能否打包成功,我怀疑是导入的静态图片,或者图片处理模块的问题