仅在菜单中选择选项时才打开对话框

时间:2019-12-28 20:14:51

标签: python tkinter dialog

我接受了一些项目,并将它们组合到一个菜单脚本中,您可以在其中选择要执行的操作,程序将相应运行。 当我在PyCharm IDE中运行它时,一切都很好,但是当我尝试从CMD(Windows 10)运行它时,即使没有/没有人要求它,对话框也会打开。

如果将root = tk.Tk()放在类中,我会得到

  

RecursionError:调用Python对象时超出了最大递归深度

感谢您的帮助! 代码:

import tkinter as tk
from tkinter import filedialog

class ShowFiles:

    root = tk.Tk()

    def __init__(self):
        self.path = path
        self.folder = folder
        self.filelocation = filelocation
        self.filenames = filenames
        self.root = root
        self.user = user
        self.root = root


        def show_files():

        try:
            # Opens a folder dialog box.
            path = filedialog.askdirectory(parent=root,
                                       initialdir=os.getcwd(),
                                       title="Select A Folder:")
            folder = path
            # User choose a filename to show on Desktop.
            savename = input("Name the file: ")
            filelocation = f"c:\\Users\\Gilush\\Desktop\\{savename}.txt"
            # Get the file names in the folder.
            filenames = os.listdir(folder)

            # Writing to file
            with open(filelocation, 'ab') as file:
                for name in filenames:
                    file.write(f"Name: {name}\n"
                               f"Folder Path: {folder}\n"
                               f"######################################################\n".encode())

            print(colored(f"[+] Done! file {savename}.txt has been saved on your Desktop! [+] \n", 'cyan'))
            # Closing the Dialog Box
            root.destroy()
            # Return to Menu
            main_menu()

        except NameError:
            print(colored("No Folder Selected.", 'red'))
            ShowFiles.show_files()

    def restart():

        try:
            user = input("Would you like to continue? [Y/n] ")

        except:
            print("Invalid Input! try [y] or [n] ")
            ShowFiles.restart()

        if user.upper() == "y":
            ShowFiles.show_files()

        else:
            print(colored("Have A Nice Day! \n", 'yellow',attrs=['bold']))
            menu()

0 个答案:

没有答案