wxPython GetClientSize()返回(0,0)

时间:2011-01-12 08:47:08

标签: python wxpython

我正在从python书创建一个小型绘图应用程序“wxPython in Action”,它使用self.GetClientSize()来获取窗口的大小。由于某种原因,这是我的返回(0,0)而不是预期的值(800,600)。

当调用wx.EmptyBitmap时,程序崩溃,其中0,0为其参数。如果我放 wx.EmptyBitmap(800,600)整个程序运行正常,减去调整大小。

以下是相关方法

    def InitBuffer(self):
        size = self.GetClientSizeTuple()
        print size
        sys.exit(1)
        self.buffer = wx.EmptyBitmap(size.width, size.height)
        dc = wx.BufferedDC(None, self.buffer)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        self.DrawLines(dc)
        self.reInitBuffer = False

这是完整的代码

#!/usr/bin/arch -i386 /usr/bin/python2.6 -tt

import sys
import wx

class SketchWindow(wx.Window):
    def __init__(self, parent, ID):
        wx.Window.__init__(self, parent, ID)
        self.SetBackgroundColour("White")
        self.color = "Black"
        self.thickness = 1
        self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
        self.lines = []
        self.curLine = []
        self.pos = (0, 0)
        self.InitBuffer()
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.Bind(wx.EVT_IDLE, self.OnIdle)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def InitBuffer(self):
        size = self.GetClientSizeTuple()
        print size
        sys.exit(1)
        self.buffer = wx.EmptyBitmap(size.width, size.height)
        dc = wx.BufferedDC(None, self.buffer)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        self.DrawLines(dc)
        self.reInitBuffer = False

    def GetLinesData(self):
        return self.lines[:]

    def SetLinesData(self, lines):
        self.lines = lines[:]
        self.InitBuffer()
        self.Refresh()

    def OnLeftDown(self, event):
        self.curLine = []
        self.pos = event.GetPositionTuple()
        self.CaptureMouse()

    def OnLeftUp(self, event):
        if self.HasCapture():
            self.lines.append((self.color, self.thickness, self.curLine))
            self.curLine = []
            self.ReleaseMouse()

    def OnMotion(self, event):
        if event.Dragging() and event.LeftIsDown():
            dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
            self.drawMotion(dc, event)
        event.Skip()

    def drawMotion(self, dc, event):
        dc.SetPen(self.pen)
        newPos = event.GetPositionTuple()
        coords = self.pos + newPos
        self.curLine.append(coords)
        dc.DrawLine(*coords)
        self.pos = newPos

    def OnSize(self, event):
        self.reInitBuffer = True

    def OnIdle(self, event):
        if self.reInitBuffer:
            self.InitBuffer()
            self.Refresh(False)

    def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self, self.buffer)

    def DrawLines(self, dc):
        for (colour, thickness, line) in self.lines:
            pen = wx.Pen(colour, thickness, wx.SOLID)
            dc.SetPen(pen)
            for coord in line:
                dc.DrawLine(*coord)

    def SetColor(self, color):
        self.color = color
        self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)

    def GetColor(self):
        return self.color

    def SetThickness(self, thickness):
        self.thickness = thickness
        self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)


class SketchFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, 'Sketch Frame', size=(800, 600))
        self.sketch = SketchWindow(self, -1)


def main():
    app = wx.PySimpleApp()
    frame = SketchFrame(None)
    frame.Show(True)
    app.MainLoop()

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:1)

我不知道是否有更好的解决方案,但问题是当对象初始化时它还没有父级,所以它不知道应该是什么大小。因此,它是宽度0和高度0.但是,它需要初始化缓冲区。我为解决这个问题做了什么

if size == (0, 0):
    size.width = 1
    size.height = 1

一旦将其添加到框架中,它将获得新的大小并调整缓冲区的大小。所以我猜这有用!

我认为另一个解决方案是将size参数传递给 init 方法,但如果不需要,我不希望这样做。

如果您有其他解决方案,请发布其他解决方案=)

答案 1 :(得分:1)

这是因为您在GetSize方法中调用__init__() - 在此方法完成之前,窗口未完全创建。因此,它的宽度和高度没有正确设置。

您可以使用wx.CallAfter / CallLater推迟调用此函数,直到窗口创建完全完成。