【发布时间】:2016-06-17 10:19:35
【问题描述】:
我是 python/wxpython 的新手。 我有一个垂直拆分的 wxpython 框架 - 显示 2 个面板。在左侧面板上,我有一个显示窗口,它通过允许用户通过单击添加到右侧面板上的按钮来浏览文件来显示图像。图像的加载工作正常,但是我希望能够通过单击创建的“删除”按钮(右侧面板)来删除图像,以便用户可以选择加载另一个图像。我试图查找 GetFocusedItem() 并选择左侧的面板,但它返回错误“'panelLeft' 对象没有属性 'GetFocusedItem'”
任何建议如何最好地解决这个问题?
非常感谢, 惠普
请在下面找到代码。 我使用的图像是 3-D 渲染图像,它是交互式的,可以移动和旋转,但是我希望能够单击右侧面板上的事件按钮,即“裁剪图像”或“删除”To give you an idea of the display .
“裁剪图像”按钮有一个附加的绘制矩形功能,我想用它来裁剪图像和“删除”按钮来删除加载的图像,但我似乎无法克服保持旋转的障碍 3-左侧面板上的 D 图像(使用鼠标)并且仅在我想要裁剪或删除时锁定它。希望这是有道理的。
有什么建议吗?
import wx
import vtk
from vtk.wx.wxVTKRenderWindowInteractor import wxVTKRenderWindowInteractor
from wx.lib.dialogs import openFileDialog
from wx.lib import eventwatcher
class p1(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent)
self.widget = wxVTKRenderWindowInteractor(self, -1)
self.widget.Enable(1)
self.widget.AddObserver("ExitEvent", lambda o,e,f=self: f.Close())
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.widget, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.Layout()
self.ren = vtk.vtkRenderer()
self.filename=""
self.isploted = False
def renderthis(self):
self.widget.GetRenderWindow().AddRenderer(self.ren)
openFileDialog = wx.FileDialog(self,"Open STL file", "", self.filename,"*.stl",wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if openFileDialog.ShowModal() == wx.ID_CANCEL:
return
self.filename = openFileDialog.GetPath()
reader = vtk.vtk.vtkSTLReader()
reader.SetFileName(self.filename)
normals = vtk.vtkPolyDataNormals()
normals.SetInputConnection(reader.GetOutputPort())
normals.Update()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(normals.GetOutputPort())
if self.isploted:
actor = self.ren.GetActors().GetlastActor()
self.ren.RemoveActor(actor)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
if not self.isploted:
axes = vtk.vtkAxesActor()
self.marker = vtk.vtkOrientationMarkerWidget()
self.marker.SetInteractor( self.widget._Iren )
self.marker.SetOrientationMarker( axes )
self.marker.SetViewport(0.75,0,1,0.25)
self.marker.SetEnabled(1)
self.ren.ResetCamera()
self.ren.ResetCameraClippingRange()
cam = self.ren.GetActiveCamera()
cam.Elevation(10)
cam.Azimuth(70)
self.isploted = True
self.ren.Render()
class VTKFrame(wx.Frame):
pos1 = None
pos2 = None
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(1050,600), style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|
wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
self.sp = wx.SplitterWindow(self)
self.p1 = p1(self.sp)
self.p2 = wx.Panel(self.sp,style=wx.SUNKEN_BORDER)
self.sp.SplitVertically(self.p1,self.p2,870)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText("Click to load a STL file")
self.plotbut = wx.Button(self.p2,-1,"Browse for STL file ", size=(120,20),pos=(10,10))
self.plotbut.Bind(wx.EVT_BUTTON, self.plot)
self.Box = wx.BoxSizer(wx.VERTICAL)
self.button1 = wx.Button(self.p2,2, "Crop image ", size=(120,20),pos=(10,35))
self.button1.Bind(wx.EVT_BUTTON,self.OnPaint)
self.button2 = wx.Button(self.p2,3, "Remove", size=(120,20),pos=(10,60))
self.button2.Bind(wx.EVT_BUTTON, self.OnButton2)
self.button3 = wx.Button(self.p2,4,"Close", size=(120,20),pos=(10,85))
self.button3.Bind(wx.EVT_BUTTON, self.OnButton3)
def plot(self,event):
self.p1.renderthis()
self.SetTitle("STL File Viewer: "+self.p1.filename)
def OnButton2(self,event):
wx.EmptyImage(self.p1)
def OnButton3(self,event):
self.Close()
def OnPaint(self,event):
if self.pos1 is None or self.pos2 is None: return
dc = wx.PaintDC(self.p1)
dc.SetPen(wx.Pen('White', 1))
dc.SetBrush(wx.Brush(wx.Color(0,0,0), wx.RED))
dc.DrawRectangle(self.pos1.x, self.pos1.y, self.pos2.x - self.pos1.x, self.pos2.y -self.pos2.y)
def PrintPos(self,position):
return str(position.x) + "" + str(position.y)
app = wx.App(redirect=False)
frame = VTKFrame(None,"STL File Viewer")
frame.Show()
app.MainLoop()
【问题讨论】: