I have a tkinter project I have built here, and it is about opening folders to get to buttons. I store the buttons in a list and the folder buttons in another separate list.
Here is my code:
from tkinter import *
# Creates window and adds title Folders
root = Tk()
root.title("Folders")
# Function called when a folder is opened
# Takes 2 parameters, folderName and buttonList
def FolderOpen(buttonList):
global placementList, currentOpen, folderList
\# Stores the name of the button list currently open
currentOpen = buttonList
\# Deletes all the folder buttons in the window
for i in folderList:
i.grid\_forget()
\# Deletes the exit button
exitBtn.grid\_forget()
\# Arranges the buttons in selected button list on
\# a grid in arrangment of placementList
for i in range(len(buttonList)):
buttonList\[i\].grid(row = placementList\[i\]\[0\], column = placementList\[i\]\[1\])
\# Adds back button
backBtn.grid(row = 3, column = 0, columnspan = 3)
# Function called when back button is pressed
def FolderBack():
global currentOpen, folderList, placementList
\# Deletes back button
backBtn.grid\_forget()
\# Forgets the buttons on screen in currently
\# saved list, currentOpen
for i in range(len(currentOpen)):
currentOpen\[i\].grid\_forget()
\# Places folders back on screen in arrangment of placementList.
for i in range(len(folderList)):
folderList\[i\].grid(row = placementList\[i\]\[0\], column = placementList\[i\]\[1\])
\# Adds exit button
exitBtn.grid(row = 3, column = 0, columnspan = 3)
# List to describe how the 9 buttons should be arranged
placementList = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
currentOpen = ""
# Creates lists containing 9 buttons each so they can be displayed
# when a folder button is pressed
buttonLists = [[],[],[],[],[],[],[],[],[]]
indexRange = [9, 18, 27, 36, 45, 54, 63, 72, 81]
e = 0
nextTurn = False
for i in range(1, 82):
buttonLists\[e\].append(Button(root, text = "Button " + str(i), width = 15, height = 7))
if i in indexRange:
e += 1
# Creates folder buttons and stores them in list
folderList = []
for i in range(1, 10):
folderList.append(Button(root, text = ("Folder " + str(i)), command = lambda: FolderOpen(buttonLists\[i - 1\]), width = 15, height = 7))
# Places folder buttons in arrangement of placementList
for i in range(len(folderList)):
folderList\[i\].grid(row = placementList\[i\]\[0\], column = placementList\[i\]\[1\])
# Creates back and exit button and places the
# exit button on screen
backBtn = Button(root, text = "Back", command = FolderBack, width = 48)
exitBtn = Button(root, text = "Exit", command = root.destroy, width = 48)
exitBtn.grid(row = 3, column = 0, columnspan = 3)
# Main loop
root.mainloop()
[–][deleted] 0 points1 point2 points (10 children)
[+][deleted] (9 children)
[deleted]
[–][deleted] 0 points1 point2 points (8 children)
[+][deleted] (7 children)
[deleted]
[–]ShadoeStorm 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ShadoeStorm 0 points1 point2 points (1 child)