简体   繁体   中英

Can't get pyinstaller to work on 2 different files

Hi all I am a maths teacher. I just started to code in python to make apps for my students. The problem I am expierinecing is the following. I do not getmy scripts to convert propperly to an exe. Whenever I Use pyinstaller (This command: pyinstaller --onefile --clean -w Stelsels.py} I ge an exe. When I run it, I get this message: failed to execute phyton script. I have entered my entire project as code. (It might not be the best code as I am a beginner) because I am not sure what you guys need to help.

I have used pyinstaller succesfully in the past on the second script. Since I added graphs with mathplotlib, it wont make the exe properly anymore. And I also get the failed to execute python script when I try to convert the second script as well.

So is the problem in the mathplotlib library or am I just doing something horribly wrong as a noob? Thank you to whomever uses their time to help me figure this out.

from tkinter import *
from random import randint
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from tkinter import messagebox

FONT = ("Tahoma", 16)
WIDTH = 800
HEIGTH = 700
COLOR = '#3492d3'


class Stelsels:

    def __init__(self, height, width):
        self.height = height
        self.width = width
        self.initUI()
        self.a1 = 0
        self.b1 = 0
        self.a2 = 0
        self.b2 = 0

    def get_a1(self):
        return self.a1

    def get_a2(self):
        return self.a2

    def get_b1(self):
        return self.b1

    def get_b2(self):
        return self.b2

    def set_a1(self, a1):
        self.a1 = a1

    def set_a2(self, a2):
        self.a2 = a2

    def set_b1(self, b1):
        self.b1 = b1

    def set_b2(self, b2):
        self.b2 = b2

    def initUI(self):

        app = Tk()
        app.title('Stelsels')

        image_right = PhotoImage(file='Right.png')
        image_wrong = PhotoImage(file='Wrong.png')
        image_empty =  PhotoImage(file='empty.png')
        frame = Frame(app, height=self.height, width=self.width)
        frame.pack()

        label_title = Label(frame, text='Los het stelsel op', font=FONT)
        label_equation1 = Label(frame, font=FONT)
        label_equation2 = Label(frame,  font=FONT, justify='left')
        Label_bracket = Label(frame, text='{', font=("Tahoma", 50))
        label_answer_x = Label(frame, text='x=', font=FONT, justify='right')
        label_answer_y = Label(frame, text='y=', font=FONT, justify='right')
        label_right_wrong = Label(frame, font=FONT)
        

        label_title.place(relx=0.2, rely=0.05, relwidth=0.6, relheight=0.1)
        label_equation1.place(relx=0.1, rely=0.20,
                              relwidth=0.3, relheight=0.05)
        label_equation2.place(relx=0.1, rely=0.25,
                              relwidth=0.3, relheight=0.05)
        Label_bracket.place(relx=0.12, rely=0.19,
                            relwidth=0.05, relheight=0.11)
        label_answer_x.place(relx=0.730, rely=0.5,
                             relwidth=0.1, relheight=0.05)
        label_answer_y.place(relx=0.730, rely=0.56,
                             relwidth=0.1, relheight=0.05)
        label_right_wrong.place(relx = 0.75, rely = 0.75,
                                relwidth = 0.2, relheight = 0.2)
        

        entry_answer_x = Entry(frame, borderwidth=5,
                               font=FONT, justify="center")
        entry_answer_y = Entry(frame, borderwidth=5,
                               font=FONT, justify="center")

        entry_answer_x.place(relx=0.800, rely=0.5,
                             relwidth=0.1, relheight=0.05)
        entry_answer_y.place(relx=0.800, rely=0.56,
                             relwidth=0.1, relheight=0.05)

        graph_container = Figure()
        graph = graph_container.add_subplot(111)
        canvas = FigureCanvasTkAgg(graph_container, master=app)
        canvas.get_tk_widget().place(relx=0, rely=0, relwidth=1, relheight=1)
        toolbar = NavigationToolbar2Tk(canvas, app)
        canvas.get_tk_widget().place(relx=0.025, rely=0.325, relwidth=.7, relheight=.575)

        def click_new():
            graph_container.clf()
            canvas.draw()
            label_right_wrong.config(image = image_empty)
            self.set_a1(randint(-9, 9))
            self.set_b1(randint(-9, 9))
            self.set_a2(randint(-9, 9))
            self.set_b2(randint(-9, 9))
            if (self.get_a1() == 0 and self.get_b1() == 0) or (self.get_a2() == 0 and self.get_b2() == 0) or (self.get_a1() == self.get_a2() and self.get_b1() == self.get_b2()):
                click_new()
            if self.get_b1() >= 0:
                label_equation1.config(
                    text='y = ' + str(self.get_a1())+'x +' + str(self.get_b1()))
            else:
                label_equation1.config(
                    text='y = ' + str(self.get_a1())+'x ' + str(self.get_b1()))
            if self.get_b2() >= 0:
                label_equation2.config(
                    text='y = ' + str(self.get_a2())+'x +' + str(self.get_b2()))
            else:
                label_equation2.config(
                    text='y = ' + str(self.get_a2())+'x ' + str(self.get_b2()))

            

        click_new()

        def click_plot_graph():
            x = np.arange(-20, 20, .1)
            y1 = self.get_a1()*x + self.get_b1()
            y2 = self.get_a2()*x + self.get_b2()
            graph_container.clf()
            graph.plot(x, y1, color="red", label='y=' +
                       str(self.get_a1())+'x + ' + str(self.get_b1()))
            graph.plot(x, y2, color="blue", label='y=' +
                       str(self.get_a2())+'x + ' + str(self.get_b2()))
            graph.axhline(y=0, xmin=0, xmax=1, color="black", label='x-as')
            graph.axvline(x=0, ymin=0, ymax=1, color="black", label='y-as')
            graph.grid()
            graph.set_xlabel("x-as")
            graph.set_ylabel("y-as")
            graph.set_xlim(-10, 10,1)
            graph.set_ylim(-10, 10,1)

            graph_container.add_axes(graph)
            canvas.draw()
            toolbar.update()

        def click_check():

            x_string = entry_answer_x.get()
            y_string = entry_answer_y.get()
            allNumbers = True
            
            try:
                if '/' in x_string:
                    x = float(int(x_string[:x_string.find('/')])/int(x_string[x_string.find('/')+1:]))
                else:
                    x = float(x_string)
                if '/' in y_string:
                    y = float(int(y_string[:y_string.find('/')])/int(y_string[y_string.find('/')+1:]))
                else:
                    y = float(y_string)
            except:
                allNumbers = False
                messagebox.showwarning(
                    title="Opgepast!", message="Vul a.u.b. getallen in")
            if allNumbers:
                if (truncate(y) == truncate(self.get_a1()*x+self.get_b1()) and truncate(y) == truncate(self.get_a2()*x+self.get_b2())):
                    label_right_wrong.config(image= image_right)
                else:
                    label_right_wrong.config(image= image_wrong)

        button_new = Button(text='Nieuw stelsel', command=click_new)
        button_plot_graph = Button(
            text='plot de grafiek', command=click_plot_graph)
        button_check = Button(
            text='Controleer je antwoord', command=click_check)

        button_new.place(relx=0.4, rely=0.19, relwidth=.2, relheight=.1)
        button_plot_graph.place(relx=0.7, rely=0.19, relwidth=.2, relheight=.1)
        button_check.place(relx=0.75, rely=0.6, relwidth=.2, relheight=.1)

        app.mainloop()

    def f(a_1, x):
        return a_1*x
def truncate(n):
    return int(n * 1000) / 1000


def main():

    g = Stelsels(HEIGTH, WIDTH)


if __name__ == "__main__":
    main()
import numpy as np

from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

from tkinter import *
from tkinter import messagebox

from Pythagoras import Pythagoras

FONT = ("Tahoma", 16)
WIDTH = 800
HEIGTH = 700

class VKV:

    def __init__(self, height, width):
        self.height = height
        self.width = width
        self.initUI()

    def initUI(self):

        app = Tk()

        frame = Frame(app, height=self.height, width=self.width)
        frame.pack()

        inframe = Frame(frame)
        inframe.place(relwidth=1, relheight=0.30)

        label_value_a = Label(inframe, text="Waarde a", font=FONT)
        label_value_b = Label(inframe, text="Waarde b", font=FONT)
        label_value_c = Label(inframe, text="Waarde c", font=FONT)
        label_equation1 = Label(inframe, text="x² +", font=FONT)
        label_equation2 = Label(inframe, text="x +", font=FONT)
        label_equation3 = Label(inframe, text="= 0", font=FONT)

        label_value_a.place(relx=0.025, relwidth=0.3, relheight=0.5)
        label_value_b.place(relx=0.35, relwidth=0.3, relheight=0.5)
        label_value_c.place(relx=0.5, relwidth=0.3, relheight=0.5)
        label_equation1.place(relx=0.275, rely=0.5, relwidth=0.1, relheight=0.5)
        label_equation2.place(relx=0.6, rely=0.5, relwidth=0.1, relheight=0.5)
        label_equation3.place(relx=0.89, rely=0.5, relwidth=0.1, relheight=0.5)

        entry_value_a = Entry(inframe, borderwidth=5, font=FONT, justify="center")
        entry_value_b = Entry(inframe, borderwidth=5, font=FONT, justify="center")
        entry_value_c = Entry(inframe, borderwidth=5, font=FONT, justify="center")

        entry_value_a.place(relx=0.1, rely=0.5, relwidth=0.15, relheight=0.5)
        entry_value_b.place(relx=0.42, rely=0.5, relwidth=0.15, relheight=0.5)
        entry_value_c.place(relx=0.74, rely=0.5, relwidth=0.15, relheight=0.5)

        # Create the output
        outframe = Frame(frame)
        outframe.place(rely=0.3, relx=0.7, relwidth=0.3, relheight=0.50)
        label_output = Label(outframe, bg="#787878", relief="sunken")
        label_output.place(rely=0.3, relwidth=.9, relheight=0.4)

        # Create Graph
        graph_container = Figure()
        graph = graph_container.add_subplot(111)
        canvas = FigureCanvasTkAgg(graph_container, master=app)
        canvas.get_tk_widget().place(relx=0, rely=0, relwidth=1, relheight=1)
        toolbar = NavigationToolbar2Tk(canvas, app)
        canvas.get_tk_widget().place(relx=0.025, rely=0.325, relwidth=.6, relheight=.575)

        def click():

            a = entry_value_a.get()
            b = entry_value_b.get()
            c = entry_value_c.get()
            allNumbers = True
            try:
                a = float(a)
                b = float(b)
                c = float(c)
            except:
                allNumbers = False
                messagebox.showwarning(
                    title="Opgepast!", message="Vul a.u.b. getallen in")

            if allNumbers:

                # write text into outputbox
                label_output.config(text=Pythagoras(a, b, c), justify="left", font=("Tahoma", 12))

                # make an points around the top of the parabola
                x = np.arange(-(b/(2*a))-40, -(b/(2*a))+40, .1)

                # clear container
                graph_container.clf()

                # set up axes
                graph.plot(x, f(a, b, c, x), color="red")
                graph.axhline(y=0, xmin=0, xmax=1, color="black")
                graph.axvline(x=0, ymin=0, ymax=1, color="black")
                graph.grid()
                graph.set_title(str(a) + "x²+ "+str(b) + "x " + str(c), pad=15)
                graph.set_xlabel("x-as")
                graph.set_ylabel("y-as",)
                graph.set_xlim([-(b/(2*a))-10, -(b/(2*a))+10])
                graph.set_ylim([f(a, b, c, -(b/(2*a)))-10,
                            f(a, b, c, -(b/(2*a)))+10])

                for coordinat in Pythagoras_backend(a,b,c):
                    graph.scatter(coordinat, 0, marker = "o", color = "blue")
                

                graph_container.add_axes(graph)
                canvas.draw()
                toolbar.update()

        button_bereken = Button(outframe, text="Antwoord:", font=FONT, command=click, justify="left")

        button_bereken.place(rely=.05, relwidth=0.9, relheight=0.2)

        app.mainloop()


def f(a, b, c, x):
    return a*x**2 + b*x+c

def Pythagoras_backend(a, b, c):
    output = []
    D = float(b) ** 2 - (4 * float(c) * float(a))
    if a==0 or D<0:
        return output
    elif D > 0:
        x_1 = round((-float(b) + D ** (1 / 2)) / (2 * float(a)), 5)
        x_2 = round((-float(b) - D ** (1 / 2)) / (2 * float(a)), 5)
        output.append(x_1)
        output.append(x_2)
    else:
        x = round(-float(b) / (2 * float(a)), 5)
        output.append(x)
    return output


def main():

    g = VKV(HEIGTH, WIDTH)


if __name__ == "__main__":
    main()

I've encountered this problem before. Firstly you should try this methods:

1 ) İf there is print() function in the your code you must delete it because you wrote pyinstaller... -w... w means is windowed. İf your program is windowed mode you can't run print() function. Otherwise you take this error:

2 ) Your app has some.png images. So you must specify full path of images. Sample: image_right = PhotoImage(file='C:\\Users\\Administrator\\Desktop\\Right.png')

If there is a problem, you can contact me. Good days.

I got some information about this error. You must import a library as below:

import pkg_resources.py2_warn

Then you must write this command for convert exe:

pyinstaller --hidden-import pkg_resources.py2_warn -n exe_file_name -w -F --log-level DEBUG your_file.py

İf I can helped you lucky me.

I eventually fixed it by doing this:

pip install --upgrade pyinstaller

Didn't update pyinstaller. Facepalm.... It works now. Thank you for everyone who was kind enough to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM