python - 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()
解决方案
我在 MacOS 10.14.16 中遇到了同样的问题。
我尝试了很多方法,这个版本对我有用:
- Python 3.7.0
- tkinter 8.6.7(8.6.8 不工作)
- py2app 0.19
与蟒蛇
推荐阅读
- python-3.x - pandas 使用分类字符串值对 x 轴进行排序
- json - 比较 2 个 JSON 文件并在值匹配时创建一个新键
- r - How do I convert hex to numbers keeping the sign (+/-) in R?
- c++ - 打印 reference_wrapper 的向量
- python - 如何让 python 游戏,Flappy Bird 的管道出现?
- swift - 如何在 SwiftUI 中关闭 ResearchKit 模态视图?
- javascript - How can I avoid updating all instances of my function component with the useState hook in React.js?
- c# - Xamarin Android revert back window flags?
- c# - I need help understanding bool = false and the meaning of -1
- c++ - Why do I get the wrong address when referencing a global variable?