wxpython在向rectang绘制文本时返回奇怪的错误

2024-06-16 09:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行程序时,它返回一个错误。这个错误的奇怪之处在于,这个代码完全可以由其他人运行。我不太清楚到底怎么修。我可能错了,但这可能是wxpython的一个错误。这不是我原来的代码。我在这个网站上发布了我的原始代码,网站的一个成员修复了它,它在他的电脑上运行良好,但在我的电脑上失败了

Traceback (most recent call last):
  File "C:\Python27\client with gui.py", line 43, in <module>
    frame = WindowFrame(None, 'ChatClient')
  File "C:\Python27\client with gui.py", line 10, in __init__
    self.dc = wx.PaintDC(self.panel)  # <<< This was changed
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_gdi.py", line 5215, in __init__
    _gdi_.PaintDC_swiginit(self,_gdi_.new_PaintDC(*args, **kwargs))
PyAssertionError: C++ assertion "Assert failure" failed at ..\..\src\msw\dcclient.cpp(277) in wxPaintDCImpl::wxPaintDCImpl(): wxPaintDCImpl may be created only in EVT_PAINT handler!



import socket
import wx

class WindowFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, size=(500, 400))
        self.panel=wx.Panel(self)
        self.panel.SetBackgroundColour("#0B3861")
        self.control = wx.TextCtrl(self.panel, style = wx.TE_MULTILINE, size =(410, 28), pos=(0,329))
        self.dc = wx.PaintDC(self.panel)  # <<< This was changed
        # Sets up the socket connection
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "127.0.0.1"
        port = 6667
        self.s.connect((host,port))

        # creates send button and binds to event
        sendbutton=wx.Button(self.panel, label ="Send", pos =(414,325), size=(65,35))
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_BUTTON, self.SendPress, sendbutton )

        self.Centre()
        self.Show()

        #Draws white rectangle
    def OnPaint(self, event):
        self.dc.SetPen(wx.Pen('black'))
        self.dc.SetBrush(wx.Brush('white'))
        self.shapeRectangle=self.dc.DrawRectangle(20, 20, 444, 280)
        self.Show(True)


        # Sets the function of the send button
    def SendPress(self, event):
        self.sent = self.control.GetValue()
        self.s.send(self.sent)
        self.control.Clear()
        self.dc.DrawText(self.sent, 0, 300 )
        self.s.close()

if __name__=="__main__":
    app = wx.App(False)
    frame = WindowFrame(None, 'ChatClient')
    app.MainLoop()

Tags: 代码inpyselfinit错误linesocket