Tkinter exe没有按预期工作

时间:2018-01-22 15:04:13

标签: python tkinter scipy exe cx-freeze

目前,我正在开展Tkinter计划。这个程序工作正常,但在我将其转换为exe文件后,我遇到了一个问题。代码如下: 将tkinter导入为tk

import numpy as np 
import scipy 
import pandas as pd
import math 
import os 
import numpy.core._methods, numpy.lib.format 
from tkinter.ttk import Progressbar

class Main(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)


        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, Calculatepage,Final):

            frame = F(container, self)

            self.frames[F] = frame
            self.geometry("700x400")

            self.wm_title('Test')
            self["bg"]="white"

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.update_page()
        frame.tkraise()    

    def show_frame2(self, cont):

        frame = self.frames[cont]

        frame.tkraise()

        frame.update_page()
        self.update()
        page=self.get_page(cont) 
        value = page.value

        frame = self.frames[Final]
        frame.update_page(value)
        frame.tkraise()      


    def get_page(self, page_class):
        return self.frames[page_class]

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self["bg"]="white"
        frame2 = tk.Frame(height=150, bd=10,bg="green")
        frame2.pack(fill=tk.X)

        button1 = tk.Button(self, text='Calculate', width=25, height=2, bg="green",fg="white",command=lambda: controller.show_frame2(Calculatepage))

        button1.place(x=250,y=100)

    def update_page(self): # empty method but I need it
            pass   

class Calculatepage(tk.Frame):


    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self["bg"]="white"
        self.controller=controller

        self.label1=tk.Label(self, text="Loading...",bg="white",fg="green",font="verdana 10")
        self.label1.pack()

        self.label2 = tk.Label(self, text="",bg="white",fg="green",font="verdana 20")
        self.label2.pack()

    def update_page(self):

        A = 10 
        b = 2
        self.value= scipy.linalg.solve(A, b)

class Final(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self["bg"]="white"
        self.controller=controller

        label1=tk.Label(self, text="output is equal to:",bg="white",fg="green",font="verdana 10")
        label1.pack()

        self.label2 = tk.Label(self, text="",bg="white",fg="green",font="verdana 20")
        self.label2.pack()

    def update_page(self,xvalue):

        output = xvalue 

        self.label2.config(text ="%s"%(output)) 

app = Main()
app.mainloop()

如您所见,在此示例中,我确实有一个开始屏幕。按下按钮时,进行计算。当发生这种情况时,用户将看到一个屏幕,其中显示消息"正在加载..."。计算完成后,输出将显示在新屏幕上。正如所描述的那样,当我在python中运行这个程序时,它运行正常。我也可以正常将程序转换为exe文件。但是,当我运行exe文件时,程序将保留在页面上,其中包含"正在加载..." (Calculationpage)并且永远不会继续到最后一个显示页面。我收到以下错误:AttributeError: module 'scipy' has no attribute 'linalg'。有人知道如何解决这个问题吗?

我使用的cx_freeze文件是:

import cx_Freeze
import sys
import matplotlib 
import os 

base = None 


executables = [cx_Freeze.Executable("Test.py",base=base,icon="test.ico")]    

cx_Freeze.setup(
        name = "User",
        option = { "build_exe":{"packages": ["tkinter","matplotlib","scipy.linalg"]}},
        version="0.01",
        executables=executables) 

0 个答案:

没有答案