当我运行程序时,它会返回错误。关于错误的奇怪之处在于此代码完全可以运行其他人。我不确定如何解决它。我可能错了,但它可能是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()