シンプルな GUI を作成するために、enthought traitsui および traits モジュールを使用しています。
私が今持っているコードを以下に示します。新しい Study_info インスタンスの「ベース ディレクトリ」に「conf.txt」というファイルが含まれている場合に、それを選択する前に警告を表示する方法を探しています。study_info.base ディレクトリに「conf.txt」ファイルが含まれていない場合、または警告が表示されたときにユーザーが続行することに同意した場合は、新しい Study インスタンスを作成します。
現在、「新規スタディウィンドウ」ウィンドウの「OK」ボタンをクリックした後、ファイルがフォルダに存在するかどうかを確認しています。以前に (ディレクトリ参照ウィンドウで [OK] をクリックした直後に) 警告ポップアップを表示して、ユーザーが [キャンセル] をクリックした場合に [参照] を直接クリックできるようにする方法があるかどうか疑問に思いました。もう一度別のフォルダを選択するには (「メイン ウィンドウ」ウィンドウに戻らずに)。ここで、ユーザーは別のフォルダを選択するために「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()
何か案は ?ディレクトリが存在するディレクトリであることを確認するものを上書きして、フォルダ内のファイルが存在するかどうかも確認する方法はありますか? これをリンクして警告ウィンドウを開く方法は?
よろしくお願いします!