Mac下tkinter按钮无法显示文字解决+Mac下打包app

Mac下tkinter Button按钮无法正常显示文字,Mac下打包app [Errno 63] File name too long

Posted by dzt on June 15, 2019

前言

打包py文件为exe文件 + 把以前自己的脚本使用tkinter打包

换了Mac之后,准备把之前的脚步打包为Mac可用的app,发现在Mac下tkinter的Button按钮无法正常显示文字

解决

截取相关部分代码 源码点击前言中的链接

import tkinter as tk
root = tk.Tk()
root.title("---NginxLog分析器---")
lf = tk.LabelFrame(root, text='请选择收件人')
lf.pack()

rb1 = tk.Checkbutton(lf, text='dzt', variable=dzt)
rb2 = tk.Checkbutton(lf, text='dyh', variable=dyh)
rb3 = tk.Checkbutton(lf, text='dj', variable=dj)
rb1.grid(row=0, column=0, padx=11)
rb2.grid(row=0, column=1, padx=11)
rb3.grid(row=0, column=2, padx=11)

#  问题就就出在tk.Button
btn = tk.Button(root, text='选择文件', command=files)
btn2 = tk.Button(root, text='一件发送邮件', command=file)

lb.pack()
lb2.pack()

btn.pack()
btn2.pack()

root.geometry('500x200+800+400')
# root.maxsize(500, 300)
# root.minsize(500, 300)
root.mainloop()

引入tkinter.ttk,替换tk.Button为ttk.Button

from tkinter import ttk
import tkinter as tk
root = tk.Tk()
root.title("---NginxLog分析器---")
lf = tk.LabelFrame(root, text='请选择收件人')
lf.pack()

rb1 = tk.Checkbutton(lf, text='dzt', variable=dzt)
rb2 = tk.Checkbutton(lf, text='dyh', variable=dyh)
rb3 = tk.Checkbutton(lf, text='dj', variable=dj)
rb1.grid(row=0, column=0, padx=11)
rb2.grid(row=0, column=1, padx=11)
rb3.grid(row=0, column=2, padx=11)

#  问题就就出在tk.Button 修改为ttk.Button
btn = ttk.Button(root, text='选择文件', command=files)
btn2 = ttk.Button(root, text='一件发送邮件', command=file)

lb.pack()
lb2.pack()

btn.pack()
btn2.pack()

root.geometry('500x200+800+400')
# root.maxsize(500, 300)
# root.minsize(500, 300)
root.mainloop()

打包为Mac可用的app

  1. pip install py2app
  2. py2applet –make-setup mac_tk.py
  3. python setup.py py2app

但是在我执行第三步的时候就报错了

[Errno 63] File name too long: '/Users/yanshigou/github/MyScripts/GetIP/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/distutils/command/build/build/bdist.macosx-10.14-x86_64/python3.7-standalone/app/collect/encodings/unicode_internal.pyc'
(tk_nginx_env)

这个问题真的很老了,问题是py2app试图在包含正在构建的代码引用的模块的另一个文件夹内生成builddis t文件夹。

由于py2app默认将builddist文件夹放在当前工作目录中,这意味着如果您在与代码引用的其他模块相同的工作目录中编写构建脚本,则会遇到麻烦。

我通过使用py2app选项来强制在下面的当前目录的父级中创建builddist目录来修复问题,但是你可以将它们放在你选择的任何地方并实现相同的目的:

打开第二步生成的setup.py

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['mac_tk.py']
DATA_FILES = []
OPTIONS = {}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

修改创建build和dist目录的路径到上一级目录以解决问题

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup
import os
from pathlib import Path

APP = ['mac_tk.py']
DATA_FILES = []
OPTIONS = {
    "bdist_base": os.path.join(str(Path(os.getcwd()).parent), 'build'),
    "dist_dir": os.path.join(str(Path(os.getcwd()).parent), 'dist')
    
}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

就成功解决 [Errno 63] File name too long的问题

这个问题在单独打包一个没有其他依赖的文件时可能不会出现

应用其他设置

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup
import os
from pathlib import Path

APP = ['mac_tk.py']
APP_NAME = "tk_nginx"
DATA_FILES = []
OPTIONS = {
    "bdist_base": os.path.join(str(Path(os.getcwd()).parent), 'build'),
    "dist_dir": os.path.join(str(Path(os.getcwd()).parent), 'dist'),
    'iconfile': 'mac_tk.icns',
    'plist': {
        'CFBundleName': APP_NAME,
        'CFBundleDisplayName': APP_NAME,
        'CFBundleVersion': "2.2.0",
        'CFBundleShortVersionString': "2.2.0",
        'NSHumanReadableCopyright': "Copyright © 2019, Yanshigou, All Rights Reserved"
    }

}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

其他设置显示效果如图