1

我正在尝试创建一个带有 3 个按钮的窗口。当用户单击其中一个按钮时,将调用分配的函数。这是我的代码,我尝试了我在文档中阅读的所有内容,但我似乎无法克服它!

我还添加了程序调用的函数,它们工作得很好,只是添加它们以供参考。此外,buttons() 函数只被调用一次,它是一个没有参数的简单调用。

def buttons():
    root.geometry('1000x1000')

    btncreation = tkinter.Button(root, text='Order by Creation',
                                 command=creation)
    btncreation.pack()

    btnmodified = tkinter.Button(root, text='Order by Last Modified',
                                 command=lastmodified)
    btnmodified.pack()

    btnaccessed = tkinter.Button(root, text='Order by Last Accessed',
                                 command=lastaccessed)
    btnaccessed.pack()


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

另外,请不要格式化我的问题的答案。我只想要一些诚实的上帝帮助,而不是关于如何“更好地格式化我的问题”的 5 条评论。如果您需要更多信息,我很乐意提供。但我没有回答关于格式的咸味评论:)

import os
import tkinter.filedialog
import pathlib
from os import rename
from tkinter import simpledialog
import getpass
import tkinter as tk

root = tkinter.Tk()

# 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 show(root):
    root.update()
    root.deiconify()


def buttons():
    root.geometry('1000x1000')

    btncreation = tkinter.Button(root, text='Order by Creation',
                                 command=creation)
    btncreation.pack()

    btnmodified = tkinter.Button(root, text='Order by Last Modified',
                                 command=lastmodified)
    btnmodified.pack()

    btnaccessed = tkinter.Button(root, text='Order by Last Accessed',
                                 command=lastaccessed)
    btnaccessed.pack()




hide(root)

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

# 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

# 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()

root.mainloop()
4

1 回答 1

3

hide(root)您可以在调用buttons()say,之前或之后使用 but 开始show(root),所以:

# Rest of your code
hide(root)
# Rest of your code

show(root)
buttons()
于 2021-03-23T11:57:10.300 回答