[PYTHON] Classify frequently used file / folder dialogs

Classify file / folder dialogs

I often create file dialog screens during work. Create the following elements each time.

I have classified it so that it can be easily reused as my memorandum. Below is the source code. It also includes class inheritance (for your own memorandum).

import os 
import csv
import tkinter as tk
from tkinter import filedialog


class Dialog_menu_screen(object):

	def __init__(self,screen_title,screen_size,path_label,dialog_btn,start_btn):
		self.screen_title = screen_title
		self.screen_size = screen_size
		self.path_label = path_label
		self.dialog_btn = dialog_btn
		self.start_btn = start_btn
	

	#Main screen display
	def show_main_screen(self):
		root = tk.Tk()
		root.title(self.screen_title)
		root.geometry(self.screen_size)

		#Widget creation
		#You can read the method with arguments with lambda.
		path_label = tk.Label(root,text=self.path_label)
		path_box = tk.Entry(root,width=45)
		dialog_btn = tk.Button(root,text=self.dialog_btn,command=lambda:self.show_dialog(path_box))
		start_btn = tk.Button(root,width=10,text=self.start_btn,command=lambda:self.execute_button(path_box.get()))
		
		#Widget position
		path_box.grid(row=0,column=1,padx=10,pady=10)
		dialog_btn.grid(row=0,column=2,padx=10,pady=10)
		path_label.grid(row=0,column=0,padx=10,pady=10)
		start_btn.grid(row=1,column=2,padx=10,pady=10)

		root.mainloop()

	
	#Show file dialog
	def show_dialog(self,path_box):
		file_type = [('csv text','*.csv')] 
		folder_place = 'C:\\pg'
		file_path = filedialog.askopenfilename(filetypes = file_type, initialdir = folder_place) 

		#If you don't press the cancel button
		if len(file_path) > 0 :
			path_box.delete(0,tk.END)
			path_box.insert(tk.END,file_path)

	
	#Display csv data.
	def execute_button(self,path_box):
		except_list = []
		with open(path_box,mode='r') as f:
			read_file = csv.reader(f)
			list_data = [row for row in read_file]
			print(list_data)
			return list_data


class Dialog_directory_screen(Dialog_menu_screen):
	#Show folder dialog
	def show_dialog(self,path_box):
		folder_place = 'C:\\pg'
		folder_path = filedialog.askdirectory(initialdir = folder_place) 

		#If you don't press the cancel button
		if len(folder_path) > 0 :
			path_box.delete(0,tk.END)
			path_box.insert(tk.END,folder_path)


	def execute_button(self,path_box):
		print(os.listdir(path_box))

	
if __name__ =="__main__":
	screen_title = "read csv"
	screen_size = "500x90"
	path_label = "File location:"
	dialog_btn = 'File location...'
	start_btn = 'Run'

	csv_instance = Dialog_menu_screen(screen_title,screen_size,path_label,dialog_btn,start_btn)
	csv_instance.show_main_screen()


	screen_title = "Browse folder contents"
	directry_instance = Dialog_directory_screen(screen_title,screen_size,path_label,dialog_btn,start_btn)
	directry_instance.show_main_screen()


that's all.

Recommended Posts

Classify frequently used file / folder dialogs
[Linux] Frequently used Linux commands (file operation)
[Linux] Frequently used Linux commands (folder operation)
pyenv Frequently used commands
Frequently used tmux commands
Frequently used Linux commands
Frequently used Linux commands
Frequently used linux commands
Frequently used pip commands
Frequently used ps command options
[Python] Frequently used library code
Frequently used subpackages of SciPy
Python frequently used code snippets
Frequently used commands in virtualenv