Python 特金特:;同时弹出两个窗口

Python 特金特:;同时弹出两个窗口,python,tkinter,Python,Tkinter,作为一名初学者,我不熟悉tkinter,也不知道如何改进以下代码,这些代码的功能应该是这样的: 运行renamer_v.py文件后,将弹出一个窗口。它在顶部显示一个简短的描述,下面是一个橙色的按钮“单击我”。单击按钮,然后弹出第二个窗口以选择文件夹。文件夹中除隐藏文件和子文件夹外的所有文件标题都将提供一个序列化前缀 问题是主窗口和第二个窗口现在同时弹出。然而,后者被设计成在用户点击按钮后出现 这是我的来源代码。 重命名器_V1.py: import win32file import win32c

作为一名初学者,我不熟悉tkinter,也不知道如何改进以下代码,这些代码的功能应该是这样的: 运行renamer_v.py文件后,将弹出一个窗口。它在顶部显示一个简短的描述,下面是一个橙色的按钮“单击我”。单击按钮,然后弹出第二个窗口以选择文件夹。文件夹中除隐藏文件和子文件夹外的所有文件标题都将提供一个序列化前缀

问题是主窗口和第二个窗口现在同时弹出。然而,后者被设计成在用户点击按钮后出现

这是我的来源代码。 重命名器_V1.py:

import win32file
import win32con
import tkinter as tk
from tkinter import Button
from clicked import Clicked

root=tk.Tk()
root.geometry("550x200")
label=tk.Label(root,font=("Arial Bold",15),
              text='Please select a directory to rename files in the folder:')
label.pack()
c=Clicked()
btn=Button(root,font=("Arial",15),bg='orange',text="Click Me",command=c.clicked)
btn.pack()

c.clicked()
file_lists=os.listdir(c.file_path)
n=0

for file in file_lists.copy():
    oldname=c.file_path+os.sep+file
    file_flag=win32file.GetFileAttributesW(oldname)
    is_hiden=file_flag & win32con.FILE_ATTRIBUTE_HIDDEN

    if os.path.isdir(oldname) or is_hiden:
        continue
    else:
        oldname=c.file_path+os.sep+file
        newname=c.file_path+os.sep+'('+str(n+1)+')'+file
        os.rename(oldname,newname)
        n+=1
label=tk.Label(root,text=str(n)+' file(s) renamed.')
label.pack()
root.mainloop()
单击.py:


from tkinter import filedialog

class Clicked:
    file_path=None

    def __init__(self):
        print()    

    def clicked(self):
        self.file_path=filedialog.askdirectory(title='ReNamer')

该应用程序已经完成,现在它可以在MacOS下完美地工作。代码如下:

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

root=tk.Tk()
class ReName():
    def __init__(self):
        self.n = 0
        self.label1=tk.Label(root)

    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()
        btn.config(state='disabled')

ins=ReName()
root.geometry("550x200")
label=tk.Label(root,font=("Arial Bold",15),
               text='Please select a directory to rename files in the folder:')
label.pack()
btn=Button(root,font=("Arial",15),bg='orange',text="Click Me",command=ins.rename)
btn.pack()
root.mainloop()


这是因为您在创建
btn
@acw1668之后调用了
c.clicked()
:如果我删除
c.clicked()
并再次运行,它会提示:TypeError:不支持+:'NoneType'和'str'的操作数类型您应该将代码块放在
btn.pack()
label=tk.label(..)之间
进入一个函数,然后将此函数分配给
命令
选项的
btn
@acw1668 Thx很多。我会试试你的建议。@kafka:读一读