I'm trying to create a window with 3 buttons. When a user clicks on one of the buttons, the assigned function is called. Here is my code, I tried everything from what I read in the documentation, but I can't seem to get over it!
I also added the functions called by the program, they work perfectly, just adding them for reference. Also, the buttons() function is called only one time and it's a simple call without argument.
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
Functions called by buttons:
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
Also, please, please, refrain from formatting answers on my question. I just want some honest to god help and not 5 comments on how to "better format my question". If you need more info, I'll be glad to give it. But I'm not answering salty comments on the formatting :)
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()
You can start by using hide(root)
but before or after calling buttons()
say, show(root)
, so:
# Rest of your code
hide(root)
# Rest of your code
show(root)
buttons()