1

シンプルな 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()

何か案は ?ディレクトリが存在するディレクトリであることを確認するものを上書きして、フォルダ内のファイルが存在するかどうかも確認する方法はありますか? これをリンクして警告ウィンドウを開く方法は?

よろしくお願いします!

4

2 に答える 2

1

このコードでは:

warn_res = new_study_info.configure_traits(view='warning')
if warn_res:
  ui_info.ui.context["object"].study = new_study

configure_traitsユーザーが [OK] をクリックすると true を返し、それ以外の場合は false を返すという線に沿って何かを想定しているようです。これはまったくそうではありませんconfigure_traits。(これは実際には部分的に正しいかもしれないと思いますが、の戻り値はconfigure_traits、私が見つけることができるどのドキュメントにも指定されていません)。

正確にconfigure_traitsは、そのモデル (コンテキスト) オブジェクトに対応するビューを作成し、このビューを画面に表示してから、メイン スレッドを引き継ぐイベント ループを開始します (そのため、ダイアログが終了するまで制御は返されません)。

やろうとしていることconfigure_traitsを実行するために、制御フローを実行するために、またはその戻り値に依存しようとすべきではありません。代わりに、特性の豊富なイベント処理システムを使用する必要があります。これは、要求したタスクを直接解決するためではなく、イベント処理と、やりたいことに必要な特定のハンドラー操作を説明することを目的とした簡単な例です (いくつかの違いがあります。あなたの例のように、より多くのテキストを書き、3番目のやや余分なダイアログを追加します):

class WarningWindow(Handler):
  finished=Bool
  notify=Event

  def init_info(self,info):
    self.finished=False

  #this is a method defined on the handler that executes when the window closes
  #
  #is_ok is True if the user closed the window ok and False if the user closed 
  #the window some other way such as clicking cancel or via the window manager
  def closed(self,info,is_ok):
    self.finished=is_ok
    self.notify=True

  view=View(Label('WARNING: YOU WILL BE EATEN BY A GRUE'),buttons=OKCancelButtons)

class StudyMenu(Handler):
  warning_window=Instance(WarningWindow)
  dir=Directory
  make_study_button=Button('Make new study')
  info=Instance(UIInfo)

  def make_study(self):
    print "now make the study"

  def _make_study_button_fired(self):
    if os.path.exists(os.path.join(self.dir,'conf.txt')):
      warning_window.edit_traits()  #note, not configure_traits

  @on_trait_change('warning_window:notify')
  def warning_window_listen(self):
    if self.warning_window.finished:
    #user is OK to overwrite the conf file
       self.make_study()
    else:
    #user does not want to overwrite
       self.info.ui.dispose() #this discards the window
                              #which is the main window in this example
       print "everything is terrible!"
       sys.exit(666)

  #this is a handler method that executes when the window opens. its primary
  #purpose here is to store the window's UIInfo object in the model object.
  def init_info(self,info):
    self.info=info

StudyMenu().configure_traits()
于 2014-02-25T00:00:55.330 に答える