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

问题描述

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

解决方法

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

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

show(root)
buttons()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...