Кнопки больше не отображаются после того, как я изменил код, чтобы он соответствовал вызовам моих функций.

Я пытаюсь добиться, чтобы 3 кнопки отображались в одном окне. Каждая кнопка вызывает разные функции. Если нажать одну кнопку, она должна вызвать назначенную функцию. При нажатии одной кнопки окно со всеми кнопками должно исчезнуть, и программа завершится.

Есть идеи, что я делаю неправильно и как получить правильный код для того, чего я пытаюсь достичь?

Спасибо!

      import os
import tkinter.filedialog
import pathlib
from os import rename
from tkinter import simpledialog, Button
import getpass

# File types list
ftypes = [
    ('Python code files', '*.py'),
    ('Perl code files', '*.pl;*.pm'),
    ('Java code files', '*.java'),
    ('C++ code files', '*.cpp;*.h'),
    ('Text files on mac', '*.rtf'),
    ('Text files', '*.txt'),
    ("PDF files", "*.pdf"),
    ('All files', '*'),
]


# The function renames and relocates selected files depending on the sorting selected
def creation():
    lst.sort(key=os.path.getctime)
    num = 1
    for line in lst:
        dst = newpath + name + str(num) + suffix
        rename(line, dst)
        num += 1


def lastmodified():
    lst.sort(key=os.path.getmtime)
    num = 1
    for line in lst:
        dst = newpath + name + str(num) + suffix
        rename(line, dst)
        num += 1


def lastaccessed():
    lst.sort(key=os.path.getatime)
    num = 1
    for line in lst:
        dst = newpath + name + str(num) + suffix
        rename(line, dst)
        num += 1


def hide(root):
    root.withdraw()


def buttons():
    root.geometry('100x100')

    btncreation = Button(root, text='Order by Creation', bd='5',
                         command=creation)
    btncreation.pack(side='top')

    btnmodified = Button(root, text='Order by Last Modified', bd='5',
                         command=lastmodified)
    btnmodified.pack(side='top')

    btnaccessed = Button(root, text='Order by Last Accessed', bd='5',
                         command=lastaccessed)
    btnaccessed.pack(side='top')
    root.mainloop()


root = tkinter.Tk()

hide(root)

# Window to select files to order
filez = tkinter.filedialog.askopenfilenames(title='Select files to rename (must be all of the same type!)')

'''
for line in filez:
    time.ctime(os.path.getctime(line))
'''

# List populated with file names
lst = list(filez)

# Saves the extension of the selected files
suffix = pathlib.Path(lst[0]).suffix


# Window to get user input on file naming
root = tkinter.Tk()
hide(root)
root.geometry("400x240")

ROOT = tkinter.Tk()

ROOT.withdraw()

# Asks user for file names, while loop won't break if no name is given
USER_INP = simpledialog.askstring(title="File Names",
                                  prompt="Name your files:")

while USER_INP == '':
    USER_INP = simpledialog.askstring(title="File Names",
                                      prompt="Name your files:")

# Gets username for path use
username = getpass.getuser()

name = USER_INP

# Asks user for folder name, while loop won't break if no name is given
USER_INP2 = simpledialog.askstring(title="Folder Name",
                                   prompt="Name your folder:")

while USER_INP2 == '':
    USER_INP2 = simpledialog.askstring(title="Folder Name",
                                       prompt="Name your folder:")

foldername = USER_INP2

# Creates new folder for ordered files, if folder exists, it uses it
newpath = r'/Users/' + username + '/Desktop/' + foldername + '/'
if not os.path.exists(newpath):
    os.makedirs(newpath)

buttons()

0 ответов

Другие вопросы по тегам