我正在使用 enthought traitsui 和 traits 模块来制作一个简单的 GUI。
我现在拥有的代码如下所示。如果新 Study_info 实例的“基本目录”在选择之前包含名为“conf.txt”的文件,我正在寻找一种弹出警告的方法。然后,如果 study_info.base 目录不包含“conf.txt”文件,或者用户同意在弹出警告时继续,我将创建一个新的 Study 实例。
目前,我在单击“新研究窗口”窗口的“确定”按钮后检查文件是否存在于文件夹中。我想知道有没有办法让之前的警告弹出(在目录浏览窗口中点击“确定”之后),这样如果用户点击“取消”,他/她可以直接点击“浏览”再次选择另一个文件夹(无需返回“主窗口”窗口)。现在,用户必须点击“New Study”才能选择另一个文件夹。
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()
有任何想法吗 ?有没有办法覆盖任何检查该目录是否存在的目录,以便它还检查文件夹内的文件是否存在?如何链接这个以打开警告窗口?
提前谢谢了 !