首页 > 解决方案 > 想着特质。动态更新 GUI 中的列表

问题描述

好的。所以我一直遇到这个障碍,我无法根据我正在做的事情来更新 GUI。我进行了广泛的搜索并尝试阅读,但我几乎无所适从。我得到的最接近的是从“myclass.uncorrex_thing”中删除该项目,然后运行“edit_traits”,但这只会在旧的 GUI 上创建一个新的 GUI...

摘要:我从 .csv 中获取了一个文件名列表,其中有很多每天都在变化的项目。我只想能够从 GUI 上的列表中选择文件名,按下对文件执行某些操作的按钮并从 .csv 列表中检查该文件名,然后使用更新的 .csv 更新 GUI 上的下拉列表

到目前为止,这是我的代码

class My_List(HasTraits):
    tracker = RecordKeeping()
    uncorrex_items = tracker.get_uncorrected_list() #this creates a list of filenames based on a .csv file
    uncorrex_items.insert(0,'Select file')


class DataFrameEditorDemo(HasTraits): 

    err_correct = PostProcessAutoErrorCorrection() #a separate module for correcting the files

    myclass = Instance(My_List)
    highlighted_thing = Str    
    Calc = Button('Run Corrections')


    traits_view = View( 
                    Item('Calc', label='correct file'),                       
                    Item("highlighted_thing", editor= EnumEditor(name = 'object.myclass.uncorrex_items')),                      
                    title="MyEditor"                                               
                    )

    def _Calc_fired(self):
        if len(self.highlighted_thing) == 8:
            self.err_correct.correct_form(self.highlighted_thing) #this corrects the file selected from the dropdown list 
                   #AND it updates the .csv file so the file should be checked as complete and will not show up when "tracker.get_uncorrected_list()" is run again

标签: pythonuser-interfaceenthoughttraitsui

解决方案


好的,对于任何看到这个并想知道的人,我终于解决了我的问题。基本上必须创建一个取决于事件(按下按钮)的属性类。当按下按钮时,highlighted_thing更新和更正表单并更新 .csv 的功能运行

class DataFrameEditorDemo(HasTraits): 

    err_correct = PostProcessAutoErrorCorrection() #a separate module for correcting the files
    tracker = RecordKeeping() #a separate module for managing the .csv


    highlighted_thing = Property(List, depends_on = 'Calc')
    test = Str
    Calc = Button('Run Corrections')


    traits_view = View( 
                    Item('Calc', label='correct file'),                       
                    Item("test", editor= EnumEditor(name = 'highlighted_thing')),                         
                    title="MyEditor"                                               
                    )

    def _get_highlighted_thing(self):
        return tracker.get_uncorrected_list()


    def _Calc_fired(self):
        if len(self.test) == 8:
            self.err_correct.correct_form(self.test)

推荐阅读