[英]Python / cx_Freeze: askopenfile / askdirectory /// filedialog
[英]python cx_Freeze askopenfile
跟隨文件(Python 3.7)完成了它應該做的所有事情。 當我按下“打開文件”按鈕時,它會打開“ askopenfilename”對話框。
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)與以下安裝文件一起使用時:
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”對話框。 問題出在哪里?
或者,您可以嘗試使用pyinstaller
而不是cx_freeze
創建凍結的應用程序。 當我使用pyinstaller
凍結腳本時,它將創建一個凍結的應用程序,其中包含一個正常工作的文件打開對話框。
您可以通過pip
安裝pyinstaller
軟件包:
pip install pyinstaller
然后,您可以使用以下命令創建凍結的應用程序:
pyinstaller dialog_test.py
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.