python cx_Freeze askopenfile [英] python cx_Freeze askopenfile

查看:137
本文介绍了python cx_Freeze askopenfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下文件(Python 3.7)完成了应做的所有事情.当我按下打开文件"按钮时,它会打开"askopenfilename"对话框.

Following file (Python 3.7) does everything it is supposed to do. When I press the "Open file" button it opens "askopenfilename" dialog.

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import messagebox
from os import path

class GUI(tk.Tk):
    def __init__(self):
        self.win=tk.Tk()
        self.create_widgets()

    def exitfcn(self):
        result = tk.messagebox.askquestion('Warning', 'Exit?')      
        if result == 'yes':
            self.win.destroy()

    # Button callback
    def getFileName(self):
        self.fDir = path.dirname(__file__)
        self.fname=fd.askopenfilename(parent=self.win, initialdir=self.fDir)
        if self.fname != '':
            self.getFile(self.fname)

    def getFile(self, file):
        print(self.fname)

    def create_widgets(self):
        self.mainButtons=ttk.Frame(self.win)
        self.mainButtons.grid(column=0, row=0, padx=100, pady=200)

        # Adding a Button - Open file
        self.openFilebtn=ttk.Button(self.mainButtons, text='Open file', command=self.getFileName)
        self.openFilebtn.grid(column=0, row=0, padx=10, pady=20)

        # Adding a Button - Exit
        self.exitbtn=ttk.Button(self.mainButtons, text='Exit', command=self.exitfcn)
        self.exitbtn.grid(column=0, row=1)

gui = GUI()
gui.win.mainloop()

当我将cx_Freeze(5.1.1)与以下安装文件一起使用时:

When I use cx_Freeze (5.1.1) with following setup file:

import os
import sys
from tkinter import filedialog
from cx_Freeze import setup, Executable

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

build_exe_options={
        'packages':['tkinter', 'tkinter.filedialog', 'os'],
        'include_files':[
        os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
        os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),]
            }

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

executables=[Executable("dialog_test.py", base=base)]

setup(name='dialog_test',
version=0.1,
description='Test file for cx_Freezer',
options={"build_exe": build_exe_options},
executables=executables
)

退出"按钮有效,打开文件"按钮无法打开"askopenfilename"对话框.问题出在哪里?

"Exit" button works, "Open file" button doesn’t open "askopenfilename" dialog. Where can be the problem?

推荐答案

作为替代方案,您可以尝试使用 pyinstaller 而不是cx_freeze来创建冻结的应用程序.当我用pyinstaller冻结脚本时,它将创建一个冻结的应用程序,并带有一个正常工作的文件打开对话框.

As an alternative you could try using pyinstaller instead of cx_freeze to create the frozen application. When I freeze your script with pyinstaller, it creates a frozen application with a properly working file-open dialog.

您可以通过pip安装pyinstaller软件包:

You can install the pyinstaller package through pip:

pip install pyinstaller

然后,您可以仅使用以下命令来创建冻结的应用程序:

and afterwards you can create the frozen application with just the command:

pyinstaller dialog_test.py

这篇关于python cx_Freeze askopenfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆