我是python / wxpython的新手。 我有一个垂直分割的wxpython框架 - 显示2个面板。在左侧面板上,我有一个显示窗口,通过单击添加到右侧面板上的按钮允许用户浏览文件来显示图像。加载图像工作正常,但我希望能够通过单击创建的“删除”按钮(右侧面板)来删除图像,以便用户可以选择加载另一个图像。我试图查找GetFocusedItem()并选择左侧的面板,但它返回错误“'panelLeft'对象没有属性'GetFocusedItem'”
有任何建议如何最好地解决这个问题吗?
非常感谢, 惠普
请找到以下代码。 我正在使用的图像是一个三维渲染图像,它是交互式的,可以移动和旋转,但我希望能够点击右侧面板上的事件按钮,即“庄稼图像”或“删除”{{3 }}
'裁剪图像'按钮有一个附加的绘制矩形功能,我想用来裁剪图像和'删除'按钮来删除加载的图像,但我似乎无法克服保持旋转的障碍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()
答案 0 :(得分:0)
您应该在wx.StaticBitmap
窗口小部件上有一个句柄,用于显示图像。您可以隐藏该窗口小部件或将其设置为wx.EmptyImage
。
我写了一个简单的图像查看器应用程序,您可以查看。它可能会帮助你解决这个问题:
import os
import wx
class PhotoCtrl(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
self.frame = wx.Frame(None, title='Photo Control')
self.panel = wx.Panel(self.frame)
self.PhotoMaxSize = 240
self.createWidgets()
self.frame.Show()
def createWidgets(self):
instructions = 'Browse for an image'
img = wx.EmptyImage(240,240)
self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY,
wx.BitmapFromImage(img))
instructLbl = wx.StaticText(self.panel, label=instructions)
self.photoTxt = wx.TextCtrl(self.panel, size=(200,-1))
browseBtn = wx.Button(self.panel, label='Browse')
browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.mainSizer.Add(wx.StaticLine(self.panel, wx.ID_ANY),
0, wx.ALL|wx.EXPAND, 5)
self.mainSizer.Add(instructLbl, 0, wx.ALL, 5)
self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
self.sizer.Add(self.photoTxt, 0, wx.ALL, 5)
self.sizer.Add(browseBtn, 0, wx.ALL, 5)
self.mainSizer.Add(self.sizer, 0, wx.ALL, 5)
self.panel.SetSizer(self.mainSizer)
self.mainSizer.Fit(self.frame)
self.panel.Layout()
def onBrowse(self, event):
"""
Browse for file
"""
wildcard = "JPEG files (*.jpg)|*.jpg"
dialog = wx.FileDialog(None, "Choose a file",
wildcard=wildcard,
style=wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
self.photoTxt.SetValue(dialog.GetPath())
dialog.Destroy()
self.onView()
def onView(self):
filepath = self.photoTxt.GetValue()
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
# scale the image, preserving the aspect ratio
W = img.GetWidth()
H = img.GetHeight()
if W > H:
NewW = self.PhotoMaxSize
NewH = self.PhotoMaxSize * H / W
else:
NewH = self.PhotoMaxSize
NewW = self.PhotoMaxSize * W / H
img = img.Scale(NewW,NewH)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
self.panel.Refresh()
if __name__ == '__main__':
app = PhotoCtrl()
app.MainLoop()
您可以阅读更多相关信息here