Tkinter:使用Pyinstaller将脚本编译为可执行文件后进行图像加载

时间:2018-11-27 12:04:11

标签: python tkinter

我正在学习如何使用Tkinter创建GUI。 我从https://www.youtube.com/watch?v=_lSNIrR1nZU学习了使用视频。 我可以像视频中一样重新创建词汇表。 我的问题是;在使用pyinstaller编译python脚本使其成为可执行文件之后,该应用不会运行(它说它无法加载图片),但是如果我注释掉该图片的代码行,然后使用pyinstaller重新编译它,该应用程序作为可执行文件运行。
PS:您可以使用任何图像,但将其命名为“ me.gif”,并且应与脚本位于同一工作目录中。
使用pyinstaller编译脚本后如何加载图像?

这是代码:

from tkinter import *

def click ():
    entered_text = textentry.get() 
    output.delete(0.0, END)
    try:
        definition = my_compdictionary[entered_text]
    except:
        definition = "sorry there is no word like that please try again"
    output.insert (END, definition)

window = Tk()
window.title("My Dictionary")
window.configure(background="black")

photo1 = PhotoImage(file="me.gif")
Label (window, image=photo1, bg="black") .grid (row=0, column=0, sticky=E)

Label (window, text="Enter the word you would like a definition for:", bg="black", fg="white", font="none 12 bold") .grid(row=1, column=0, sticky=W)

textentry = Entry (window, width=20, bg="white")
textentry.grid (row=2, column=0, sticky=W)

Button(window, text="SUBMIT", width=6, command=click) .grid (row=3, column=0, sticky=W)

Label (window, text="\nDefinition:", bg="black", fg="white", font="none 12 bold") .grid(row=4, column=0, sticky=W)

output = Text (window, width=75, height=6, wrap=WORD, background="white")
output.grid(row=5, column=0, columnspan=2, sticky=W)

my_compdictionary = {'algorithm': 'step by step instructions to complete a task', 'bug': 'piece of code that causes a program to fail'}

Label (window, text="click to exit:", bg="black", fg="white", font="none 12 bold") .grid(row=6, column=0, sticky=W)

def close_window ():
    window.destroy()
    exit()

Button(window, text="Exit", width=14, command=close_window) .grid(row=7, column=0, sticky=W)

window.mainloop()

0 个答案:

没有答案