Tkinter 回调中的异常仅发生在批处理运行中,而不发生在 Spyder 运行中

时间:2021-01-06 09:01:22

标签: tkinter callback spyder

我编写了一个代码来显示动态 3D 模拟的结果。选项之一是在特定时间通过 3D 数据制作随机横截面。我显示了某个水平幻灯片,并且可以滚动浏览不同深度的水平切片。之后,用户可以使用鼠标指定应沿其制作横截面的点。之后,绘制横截面并保存日期。关闭两个图形后,可以制作一个新的随机横截面。

如果我直接从 Spyder 运行我的脚本,一切正常。但是,如果我按如下方式启动我的脚本:

C:\WPy64-3860\python-3.8.6.amd64\python.exe C:\WPy64-3860\script.py

我收到以下错误代码:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\WPy64-3860\python-3.8.6.amd64\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\WPy64-3860\Eigen_scripts\hstout161220.py", line 768, in plot_and_save
    cross_sc=cross_section_linestring(da,profiel)
  File "C:\WPy64-3860\python-3.8.6.amd64\lib\site-packages\imod\select\cross_sections.py", line 403, in cross_section_linestring
    return _cross_section(data, linecoords)
  File "C:\WPy64-3860\python-3.8.6.amd64\lib\site-packages\imod\select\cross_sections.py", line 275, in _cross_section
    raise ValueError("Linestring does not intersect data")
ValueError: Linestring does not intersect data

这是出错的代码:

    elif plane_id in ['User defined']:
        for ind2 in range(len(time_selection)):
            # Create the start point and end point for the cross section
            Y=np.flipud(np.rot90(Data_4D[:,:,:,time_selection[ind2]]))  #3D data 1 time for scroll slices
            Data_3Dt=Data_4D[:,:,:,time_selection[ind2]] #3D data 1 time for vert cross section

            class IndexTracker(object):
                def __init__(self, f1, Y):
                    self.f1 = f1
                    
                    self.Y = Y
                    rows, cols, self.slices = Y.shape
                    self.ind = self.slices// 2
                    
                    self.im = f1.contour(xcoord, ycoord, self.Y[:, :, self.ind])
                    self.update()
                    
                def onscroll(self, event):
                    print("%s %s" % (event.button, event.step))
                    if event.button == 'up':
                        self.ind = (self.ind + 1) % self.slices
                    else:
                        self.ind = (self.ind - 1) % self.slices
                    self.update()
                            
                def update(self):
                    self.f1.clear()
                    self.f1.contour(xcoord, ycoord, self.Y[:, :, self.ind])
                    self.f1.grid(True)
                    self.f1.set_ylabel('Depth %s' % zcoord[self.ind])
                    self.im.axes.figure.canvas.draw()
                    
                def onclick(self,event):
                    fig.canvas.mpl_disconnect(cid)
            
            fig, f1 = plt.subplots(1, 1)   
            
            tracker = IndexTracker(f1, Y)
            fig.canvas.mpl_connect('scroll_event', tracker.onscroll) #update graph 
            
            xyprofiel = plt.ginput(n=0, show_clicks=True, mouse_add=1, mouse_pop=3, mouse_stop=2)
            #plt.show() 
        
            coords = {"x": xcoord, "y": ycoord, "z": zcoord}
            dims = ("x", "y", "z")
            da = xr.DataArray(Data_3Dt, coords, dims)
            profiel=sg.LineString(xyprofiel)
            
            cross_sc=cross_section_linestring(da,profiel)
            afstand=cross_sc.s
            diepte=cross_sc.z
            slice_data=cross_sc.values
            slice_data_rv=np.flipud(np.rot90(slice_data))
            #plt.close()
            
            fig2, f2 = plt.subplots(1,1)
            f2 = plt.contour(afstand,diepte, slice_data_rv)
            plt.grid(b=True, which='major', color='#666666', linestyle='-')
            f2.axes.set_aspect('equal')
            
            # plt.show()
            # prf = go.Figure(data = go.Contour(z=slice_data_rv, x=afstand, y=diepte))
            # prf.show()
            
            afstand, diepte =np.meshgrid(afstand, diepte)
            xx=afstand.flatten()
            yy=diepte.flatten()
            zz=slice_data_rv.flatten()
        
            np.savetxt(f'{dir_data}/random_slice_{selected_times_list[ind2]}Z_{Para}_{larfilename}.dat',np.c_[xx,yy,zz])
                               
            plt.show()

有人知道如何解决这个问题吗?我当然可以随时从 Spyder 运行它,但对我来说并不总是方便。

提前致谢, 尼克

0 个答案:

没有答案
相关问题