如何在Tkinter中同时调用多个按钮

2024-06-02 04:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个有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()

Tags: pathnameforostkinterdeflineroot
1条回答
网友
1楼 · 发布于 2024-06-02 04:32:57

您可以从使用hide(root)开始,但在调用buttons()之前或之后,比如说,show(root),因此:

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

show(root)
buttons()

相关问题 更多 >