Wxpython 窗口中的图形wx.clientDC不是相对于面板,而是相对于整个窗口
我正在使用wxPython为一个项目构建一个剪切优化器,我想为绘图区域布局一个简单的框架,并在下面布局一个小面板,用于操作和消息 我正在这样做:Wxpython 窗口中的图形wx.clientDC不是相对于面板,而是相对于整个窗口,wxpython,gdi,Wxpython,Gdi,我正在使用wxPython为一个项目构建一个剪切优化器,我想为绘图区域布局一个简单的框架,并在下面布局一个小面板,用于操作和消息 我正在这样做: topPanel = wx.Panel(self) # the intended drawing area, 800x600 self.planeArea = wx.Window(topPanel, -1, pos=(0, 0), size=(800, 600)) # a panel for showing messages and plac
topPanel = wx.Panel(self)
# the intended drawing area, 800x600
self.planeArea = wx.Window(topPanel, -1, pos=(0, 0), size=(800, 600))
# a panel for showing messages and placing some buttons
commandArea = wx.Panel(topPanel, -1, pos=(800, 0), size=(800, 200))
button = wx.Button(commandArea, -1, label='Optimize')
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.planeArea, 0, wx.EXPAND | wx.ALL)
box.Add(commandArea, 0, wx.EXPAND | wx.ALL)
topPanel.SetSizer(box)
在我的绘图方法中,我会:
# I'm using clientDC because I draw the shapes after the user chooses a file, Am I doing it wrong? I still haven't figured how or when to use paintDC and left that research for later
dc = wx.ClientDC(self.planeArea)
dc.Clear()
dc.SetDeviceOrigin(0, 0)
for polygon in plane.polygons:
dc.DrawPolygon(polygon.vertices)
如果我不使用SetDeviceOrigin,多边形将从窗口最左侧和最顶部的点绘制,但我希望从最左侧和最底部的点开始
问题是我的图形放错了位置,因为0,0是相对于整个窗口的,而不是相对于我的图形面板的
我一直在阅读文档和下面的示例,但我无法解决这个问题。有人知道我做错了什么吗
我在Mountain Lion和Python 2.7(PyDev和Eclipse)中使用WxPython 2.9-osx-cocoa-py2.7
非常感谢,
Rod我通常不使用原点等,而是调整图形,但我认为您需要类似于
dc.SetDeviceOrigin(0,dc.GetSize().height-1)
,这意味着默认坐标空间中的点(0,height-1)将是新的(0,0)。但是,该点上方的坐标仍将为负,该点下方的坐标仍将为正。要改变这一点,您需要调用SetAxisOrientation
我不确定是什么问题,但我也有同样的问题
我发现如果使用wx.ClientDC(event.GetEventObject())并将方法放在外部,它在osx(10.6.8 python 2.7 wxpython 2.9.4.0)上运行良好
这相对有效(相对于面板绘制框架和面板,然后绘制框):
这在windows上相对有效,但在osx上不起作用(在我的osx 10.6.8上,它在框架上绘制):
有人知道为什么吗?所以基本上我要做的是从绘图区域的左下顶点开始绘图,默认情况下从左上顶点开始。我相信这一定很简单,但我肯定错过了什么
import wx
class test(wx.Frame):
def __init__(self, *args, **kwargs):
super(test, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self, -1)
graphpanel = wx.Panel(panel, -1, pos=(50, 50), size=(200, 200))
graphpanel.Bind(wx.EVT_PAINT, PaintGraph)
self.SetSize ((1000,750))
self.Centre()
self.Show(True)
def PaintGraph(event):
graph = wx.ClientDC(event.GetEventObject())
graph.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID))
graph.DrawRectangle(0, 0, 100, 100)
def main():
ex = wx.App()
test(None)
ex.MainLoop()
if __name__ == '__main__':
main()
import wx
class test(wx.Frame):
def __init__(self, *args, **kwargs):
super(test, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self, -1)
graphpanel = wx.Panel(panel, -1, pos=(50, 50), size=(200, 200))
graphpanel.Bind(wx.EVT_PAINT, self.PaintGraph)
self.SetSize ((1000,750))
self.Centre()
self.Show(True)
def PaintGraph(self, event):
graph = wx.ClientDC(self)
graph.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID))
graph.DrawRectangle(0, 0, 100, 100)
def main():
ex = wx.App()
test(None)
ex.MainLoop()
if __name__ == '__main__':
main()