I'm using enthought traitsui and traits modules to make a simple GUI.
The code I have for now is shown bellow. I'm looking for a way to pop-up a warning if the "base directory" of a new Study_info instance contains a file called "conf.txt" before selecting it. I then create a new Study instance if the study_info.base directory doesn't contains the "conf.txt" file or if the user agrees to proceed when the warning pops-up.
Currently, I check if the file exists in the folder after clicking on the 'OK' button of the "New study window" window. I wondered if there's a way to make the warning pop-up before (just after clicking on "Ok" in the directory browsing window), so that if the user clicks on "Cancel", he/she can directly click on "browse" again to select another folder (without going back to the "Main window" window). Now, the user has to click on "New Study" in order to select another folder.
from traitsui.api import *
from traits.api import *
import os
class Study_info(HasTraits):
base_directory = Directory(exists=True)
new_study_view = View('base_directory',title="New study window", buttons=['OK','Cancel'],kind='modal')
warning_msg = '\nWarning: Folder already contains configuration file.\n\nProceed ?\n'
warning = View(Item('warning_msg',show_label=False,style='readonly'),title='Warning',kind='modal',buttons = ['OK','Cancel'])
class Study(HasTraits):
def __init__(self, study_info):
self.base_directory = study_info.base_directory
# plus some other processing stuff
view = View(Item('base_directory',style='readonly'))
class study_handler(Handler):
def new_study(self, ui_info):
new_study_info = Study_info()
ns_res = new_study_info.configure_traits(view='new_study_view')
if ns_res and os.path.exists(new_study_info.base_directory):
new_study = Study(new_study_info)
if os.path.exists(os.path.join(new_study.base_directory,'conf.txt')):
warn_res = new_study_info.configure_traits(view='warning')
if warn_res:
ui_info.ui.context["object"].study = new_study
else:
ui_info.ui.context["object"].study = new_study
class GUI(HasTraits):
study = Instance(HasTraits)
new_study = Action(name="New Study",action="new_study")
view = View(Item('study',style='custom',show_label=False),buttons = [new_study], handler = study_handler(),title="Main window",resizable=True)
g = GUI()
g.configure_traits()
Any ideas ? Is there a way to overwrite whatever checks that the directory is a directory that exists, so that it also checks if the file inside the folder exists ? How to link this to open the warning window ?
Many thanks in advance !