首页 > 解决方案 > Python:虽然.py运行正常,但APP窗口全黑

问题描述

我编写了一个代码,它将序列号作为前缀添加到所选文件夹中的文件名中。在 Pycharm 下一切正常(见图 1)。 图1 但是,在.py文件通过以下步骤转换为.app后,窗口全黑(图2)。 图 2 执行以下操作: (1)pip install py2app (2)py2applet --make-setup EasyReNamer.py (3)python setup.py py2app 然后 EasyReNamer.app 在 dist 文件夹中可用。这是我的代码:

import os
from tkinter import filedialog
import tkinter as tk
from tkinter import Button

class ReName():
    def __init__(self):
        self.root = tk.Tk()
        self.root['background']='white'
        self.n = 0
        label = tk.Label(self.root, font=("Arial Bold", 15),text='Please select a directory to rename files in the folder:')
        self.label1=tk.Label(self.root)
        label.pack()
        self.btn = Button(self.root, font=("Arial", 15), relief='raised',highlightbackground='orange', text="Click Me", command=self.rename)
        self.btn.pack()

    def widget_position(self,root,width,height):
        self.width=self.root.winfo_screenwidth()
        self.height=self.root.winfo_screenheight()
        x=(self.width-width)/2
        y=(self.height-height)/2
        self.root.geometry('%dx%d+%d+%d' % (width,height,x,y))
        self.root.grid()

    def get_db_configure(self):
        self.root.title('Easy ReNamer Version 1.0')
        self.root.resizable(0, 0)

    def rename(self):
        file_path = filedialog.askdirectory(title='ReNamer')
        self.file_lists = os.listdir(file_path)
        for self.file in self.file_lists.copy():
            self.oldname = file_path + os.sep + self.file
            if os.path.isdir(self.oldname) or self.file.startswith('.'):
                continue
            else:
                self.newname = file_path + os.sep + '(' + str(self.n + 1) + ')' + self.file
                os.rename(self.oldname, self.newname)
                self.n+=1
        self.label1.config(text='{} file(s) renamed'.format(self.n))
        self.label1.pack()
        self.btn.config(state='disabled')
        self.btn.pack()

ins=ReName()
ins.widget_position(ins.get_db_configure(),500,80)
ins.root.mainloop()

标签: pythonmacospy2app

解决方案


我在 MacOS 10.14.16 中遇到了同样的问题。

我尝试了很多方法,这个版本对我有用:

  1. Python 3.7.0
  2. tkinter 8.6.7(8.6.8 不工作)
  3. py2app 0.19

与蟒蛇


推荐阅读