簡體   English   中英

Python Traits GUI遞歸深度

[英]Python Traits GUI recursion depth

我正在使用Enthought的Traits在python中開發GUI。 我一直得到一個“運行錯誤:cmp中超出了最大遞歸深度”如果我翻轉我的MainWindow類中使用“Item”標簽的順序,代碼執行正常。 我似乎無法找到有關為什么會發生這種情況的任何文檔。 這似乎與Chaco情節有關。 下面是我的測試代碼。

from chaco.api import ArrayPlotData, Plot
from traits.api import HasTraits, Instance, String, Float, Enum, Button, Str
from traitsui.api import Handler, View, Item, Group, HSplit, NoButtons, VGroup, VGrid
from pyface.api import GUI
from threading import Thread
from time import sleep
from enthought.enable.component_editor import ComponentEditor
from scipy import rand, indices, exp, sqrt, sum
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
from enthought.chaco.api import gray
from enthought.savage.traits.ui.svg_button import SVGButton


class User_Input_Panel(HasTraits):

    User = Str(name='User', label="User Name")
    Sample_Name = Str(name='Sample_Name',label="Sample Name")
    Path = Str(name='Path', label="Save Location", style = 'readonly')

    #I use this to create a folder icon on the button
    #~ Save_Folder_Button = SVGButton(label='Choose save Location', \
                            #~ filename=Folder-drag-accept.svg', \
                            #~ width=28, \
                            #~ height=28 \
                           #~ )



    #~ #Create the User Information panel
    User_Information_Panel = View(VGroup(
                                    VGrid(
                                          Item('User'), 
                                          Item('Sample_Name'), 
                                          Item('Path', width=.700, visible_when = 'Save_Visible == True'),                              
                                          #Item('Save_Folder_Button', show_label=False),
                                          ),
                                    show_border=True, label="User Information"
                                    ))

    def _Save_Folder_Button_fired(self, event):

        print("Pushed the Save Folder")
        #self.file, self.dir = wxOpenFile(multi=False)
        #fdir = GUI_tools.wxOpenFile()
        #fdir = GUI_tools.wxSavePath()


#I'm planning on setting up threading later
class MainWindowHandler(Handler):

    def close(self, info, is_OK):
        #~ if (info.object.user_input.acquisition_thread and \
            #~ info.object.user_input.acquisition_thread.isAlive()):
            #~ info.object.user_input.acquisition_thread.wants_abort = True
            #~ while info.object.user_input.acquisition_thread.isAlive():
                #~ sleep(0.1)
            #~ GUI.process_events()
        return True


class MainWindow(HasTraits):

    plot = Instance(Plot)
    plotdata = Instance(ArrayPlotData, ())
    user_input = Instance(User_Input_Panel, ())


    def _user_input_default(self):
        return User_Input_Panel(plotdata = self.plotdata)

    Save_Folder_Button = SVGButton(label='Choose save Location', \
                            filename='C:\Python27\Examples\Mill_GUI\Tescan_BatchScan\Folder-drag-accept.svg', \
                            width=28, \
                            height=28 \
                           )

    def _plot_default(self):
        self.plotdata = ArrayPlotData(imagedata=np.zeros((100,100)))
        plot = Plot(self.plotdata)
        plot.img_plot('imagedata')
        self.plot = plot
        return plot

    # If I flip the "Item('user_input'" with "Item('plot') the code will run...
    view = View(VGroup(Item('user_input', style = 'custom', show_label = False), 
                       Item('plot', editor = ComponentEditor(), dock = 'vertical'),
                       show_labels = False),
                       resizable = True, handler = MainWindowHandler(),
                       buttons = NoButtons)

if __name__ == '__main__':
    MainWindow().configure_traits()

有誰知道為什么你會用這段代碼得到遞歸錯誤? 我必須在user_input面板下方顯示繪圖,以便用戶可以看到他們正在收集的實時數據。

干杯,Shivels

您需要從_plot_default方法中刪除以下行: self.plot = plot為了在traits中發生這種情況,您只需要返回該繪圖對象,並且將通過Traits完成對self.plot的賦值。 發生遞歸錯誤是因為您嘗試訪問試圖設置它的方法內的對象的plot屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM