gui-host-lookup/tkinter-example-passing-variables-between-windows.py

83 lines
2.2 KiB
Python
Raw Normal View History

import os
import tkinter as tk
from tkinter import filedialog
class MainFrame:
def __init__(self, master):
self.master = master
# Variables
self.file_list = [] # Files list
self.import_state = tk.DISABLED # Enable import on correct fp
# Class Frames
FileSelect(master, self.file_list, self.import_state)
LoadFiles(master, self.file_list, self.import_state)
class FileSelect:
def __init__(self, master, files, button_state):
self.files = files
self.button_state = button_state
# Frame
self.frame = tk.LabelFrame(master, text="File Selection")
self.frame.grid(row=0, column=0)
# Select directory button
tk.Button(self.frame, text="Open:", command=self.directory_path).grid(
row=0, column=0
)
# Tkinter Elements...
# Directory Selection
def directory_path(self):
# Select filepath
directory = filedialog.askdirectory(initialdir="/", title="Select a directory")
os.chdir(directory)
# Populate list of files
for tb in os.listdir(directory):
self.files.append(tb)
# Update GUI, activate button in different class if condition met
if len(os.listdir(directory)) == 0:
# Update GUI Labels...
self.button_state = tk.DISABLED
print(self.files, self.button_state)
elif len(self.files) == len(os.listdir(directory)):
# Update GUI Labels...
self.button_state = tk.NORMAL
print(self.files, self.button_state)
class LoadFiles:
def __init__(self, master, files, button_state):
self.files = files
self.button_state = button_state
# Frame
self.frame = tk.LabelFrame(master, text="File loading")
self.frame.grid(row=1, column=0)
# Load button
self.import_files_button = tk.Button(
self.frame, text="Import TB", command=self.print_files
)
self.import_files_button.grid(row=0, column=0, sticky="EW")
# Tkinter Elements...
def print_files(self):
print(self.files, self.button_state)
if __name__ == "__main__":
root = tk.Tk()
MainFrame(root)
root.mainloop()